Code RoomTraining loss erratic
MediumPrep Room Coding #2252

Training loss erratic

Code reviewCode quality & reviewMid–Senior~18 min

Review this PyTorch training loop.

Training is unstable and the loss behaves erratically. What's missing?

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 18 min
Mark a line and say what kind of problem it is.0 findings
1import torch
2import torch.nn as nn
3 
4def train(model, loader, epochs=10, lr=1e-3):
5 opt = torch.optim.Adam(model.parameters(), lr=lr)
6 loss_fn = nn.CrossEntropyLoss()
7 for epoch in range(epochs):
8 for xb, yb in loader:
9 logits = model(xb)
10 loss = loss_fn(logits, yb)
11 loss.backward()
12 opt.step()
13 print(f"epoch {epoch} loss {loss.item():.4f}")
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.