Day 30: Validation loops & metrics tracking
Watching the model learn — or fail to
Training loss alone tells you the model is memorizing; the validation loop tells you whether it's *learning*. After each epoch, switch to eval mode, disable gradients, run the whole validation set, and record loss and accuracy. Plotting training vs validation curves over epochs is the Day-16 overfitting diagnostic in motion — and the single most useful habit in this stage.
model.eval()
correct, total, val_loss = 0, 0, 0.0
with torch.no_grad():
for images, labels in val_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
val_loss += criterion(outputs, labels).item()
preds = outputs.argmax(dim=1)
correct += (preds == labels).sum().item()
total += labels.size(0)
print(f"val loss {val_loss/len(val_loader):.4f}, val acc {correct/total:.3f}")Early stopping
When validation loss stops improving (or starts rising) while training loss keeps falling, the model has begun overfitting. Early stopping halts training at the best validation epoch and keeps that checkpoint. It's the simplest, most reliable regularizer you have — no hyperparameters to tune, just 'stop when it stops helping'.
Log to something you can look at
Even a printed loss/acc per epoch is enough to start. Tools like TensorBoard or Weights & Biases make the curves live and comparable across runs. The Continuous-tracks discipline from later stages begins here: every change should be measured against the last, not judged by vibes.
Key terms
- Validation loop
- Evaluating the model on held-out data each epoch (in eval mode, no gradients) to track generalization.
- argmax
- Selecting the index of the highest score — turning class logits into a predicted class.
- Early stopping
- Halting training when validation performance stops improving, keeping the best checkpoint to prevent overfitting.
Training loss keeps dropping but validation loss has started rising for the last several epochs. What should you do?