Debugging Training
Intuition
When training fails, the code usually runs without errors — it just does not learn. A few quick experiments narrow down the cause: can the model memorise one tiny batch? Is the loss exploding or flat? Are the gradients huge or zero?
Precise explanation
Overfit one batch. Train on 8–16 examples for a few hundred steps. A correct model and loss drive the loss close to zero. If they cannot, there is a bug (wrong labels, wrong loss, a detached graph) — more data will not help.
Read the loss curve.
Pattern Likely cause becomes nan/infor growslearning rate too high, exploding gradients barely moves learning rate too low, dead ReLUs, bug that blocks gradients falls on train, rises on validation overfitting — regularise or stop earlier Look at gradient norms. After
backward(),p.grad.norm()for each parameter. Values of 1e6 mean explosion; exactly 0 means no gradient reaches that layer.Clip gradients.
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)rescales all gradients together when their total norm exceedsmax_norm, betweenbackward()andstep().
Worked example
A linear model with lr = 1.0 on data with large inputs diverges to nan within ten steps. Clipping
the gradient norm to 1.0 keeps every step bounded, and the loss falls instead.
Common mistakes
- Tuning the architecture before checking that one batch can be overfitted.
- Clipping after
step()(too late) or beforebackward()(nothing to clip). - Treating a
nanloss as a numerical curiosity rather than a learning-rate problem.
Why this matters
These checks save hours. The training-diagnostics project idea after this step turns them into a notebook you can reuse.
References
- A Recipe for Training Neural Networks — Andrej Karpathy (
S111) - Optimizing Model Parameters (PyTorch tutorial) — PyTorch (
S106)