Foundations to Frontier
Tests
E. Deep Learning › E0 Core Concepts (concept pages)

Debugging Training: Loss Curves, Overfitting a Single Batch, Sanity Checks

Neural network training fails quietly: a buggy pipeline often still runs and produces numbers, just worse ones. A small set of cheap checks catches most problems before expensive runs: the initial loss should match the uniform prediction (ln k for k classes); the model should be able to drive the loss on a tiny batch close to zero; analytic gradients should match finite differences; validation accuracy should beat a trivial baseline; and the shape of training and validation curves tells over- from underfitting and bad learning rates apart. This page explains each check and what each failure means, how to read loss curves, and a debugging workflow from data inspection to scaling up. In the exercises you implement four sanity checks around a small network, then inject six classic bugs (gradient ascent, softmax over the wrong axis, a missing ReLU derivative, unscaled inputs, shuffled labels and a 100× learning rate) and find out which checks catch which bugs.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Pre-flight checks for training runs

Pilots run through a checklist before every flight, even though the plane usually works. Training a neural network deserves the same habit. A broken pipeline rarely crashes: it trains, prints losses, and quietly produces a worse model. A few minutes of sanity checks catch most bugs before hours of compute are wasted.

Read the curves: what is wrong with this training run?
ln 3 (uniform guess)step 0 → 400 · loss 0 to 2.5 (clipped) · orange train, blue validation
final training loss 0.081 · final validation loss 0.090
Both curves fall from ln 3 and level off together: the model learns and generalises.
Why engineers care

For safety-relevant models, such as perception for landing or anomaly detection on telemetry, a silently buggy training run is worse than a crash. Systematic checks and careful reading of training curves are part of a defensible engineering process. They are also the fastest way to stop wasting expensive GPU time.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A buggy training pipeline usually…
  2. 2. Training loss near zero with high validation loss means…
  3. 3. A loss that jumps up to NaN usually means…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The checks by hand

1. Initial loss. With small output weights, a classifier predicts nearly uniform probabilities, so the cross-entropy should start near ln k. That is 2.30 for k = 10 and 6.91 for k = 1000. A start of 11.5 for 10 classes means badly scaled outputs; 0.5 suggests labels leaking into the inputs.

2. Overfit one batch. Take 10–20 examples and train on them alone. A working model and optimiser drive the loss close to 0. If not, something in the model, loss or optimiser is broken.

3. Gradient check. Compare backprop with central finite differences in float64 (E0-3). The relative error should be around 10⁻⁷ or smaller.

4. Beat a baseline. After training, validation accuracy must clearly exceed chance (1/k), or the majority class, or a simple model.

5. Read the curves. Smooth with a moving average (for example, a 3-point moving average of 1, 2, 3, 4, 5 gives 2, 3, 4).

pattern likely cause
both losses high and flat learning rate too low, a bug, or too small a model
loss explodes or becomes NaN learning rate too high or a numerical bug
train ≪ validation, growing gap overfitting
validation near the baseline no signal, often misaligned data and labels
📝 Quick check (Level 1)
0/3 answered
  1. 1. Expected initial loss for 4 classes?
  2. 2. If a model cannot overfit 16 examples, suspect…
  3. 3. The generalisation gap is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Running the checks

Exercise 1 (a 2-layer ReLU network on 3-arm spirals, 600 training and 600 validation points):

check result
initial loss ≈ ln 3 pass (1.095 vs 1.099)
overfit 16 examples pass (loss 0.0159)
gradient check pass (3.2 × 10⁻⁹)
beats chance on validation pass (0.997)

A backward pass that forgot the ReLU derivative failed the gradient check, with a relative error of 0.59.

A debugging workflow (after Karpathy, 2019):

  1. Look at the data. Check the labels, preprocessing and augmentation on a few examples.
  2. Start with a simple model and a simple loss, and check the initial loss.
  3. Overfit one batch.
  4. Train on the full data and get a validation baseline.
  5. Only then scale up: regularise, tune and enlarge.

Change one thing at a time, fix the random seeds, and log everything: losses, gradient norms, learning rate, and a few predictions.

model.train(); xb, yb = next(iter(loader))
for step in range(500):                         # overfit a single batch
    loss = F.cross_entropy(model(xb), yb); opt.zero_grad(); loss.backward(); opt.step()
print(loss.item())                              # should approach 0
🐍 Exercise 1: four sanity checks
Implement a small MLP with switchable injected bugs, and four sanity checks: initial loss ≈ ln k, overfitting a tiny batch, a finite-difference gradient check, and beating chance on validation.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The first step of a debugging workflow is to…
  2. 2. Why change one thing at a time?
  3. 3. In exercise 1, the missing ReLU derivative was caught by…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory behind the symptoms

Why the initial loss is ln k. With output weights near 0, the logits are all about equal, so softmax gives 1/k for every class and the cross-entropy is −log(1/k) = ln k. A larger value means confidently wrong predictions at initialisation, which waste early training. A smaller one means the network already “knows” the answer, which is suspicious.

Memorisation. Zhang et al. (2017) showed that large networks can fit even random labels on image data. That is why overfitting a batch tests the machinery, not generalisation. Fitting is easiest when inputs are high-dimensional. In exercise 2, a small network on 2-D inputs could not memorise 16 shuffled labels, because nearby points carried conflicting labels.

Curve shapes.

  • Learning-rate problems show in the first hundred steps.
  • Overfitting shows as a growing gap.
  • Validation loss can rise while validation accuracy still improves: the predictions become overconfident (calibration), so watch both.

Other common silent bugs:

  • the model left in eval mode during training (dropout and batch norm off), or train mode during evaluation;
  • data leakage between splits;
  • the wrong normalisation statistics;
  • labels off by one;
  • forgetting to zero gradients;
  • a loss that expects logits receiving probabilities (double softmax).

Interview questions.

  • The loss goes down but accuracy stays at chance. What could be wrong? Label encoding or argmax over the wrong axis, a metric computed on the wrong split, or a loss and metric on different outputs.
  • Validation is better than training. Is that a bug? Not necessarily: dropout and augmentation are active only in training, and the training loss is averaged during the epoch while the model is still improving.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Zhang et al. (2017) showed large networks can…
  2. 2. Validation loss rising while accuracy improves suggests…
  3. 3. Passing softmax probabilities into a loss that expects logits causes…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Which check catches which bug?

History.

  • Y. LeCun et al. (1998, “Efficient BackProp”) collected practical training checks.
  • Y. Bengio (2012) wrote practical recommendations for gradient-based training.
  • C. Zhang et al. (2017) showed that networks memorise random labels.
  • A. Karpathy (2019) wrote the widely used “A recipe for training neural networks”.

Exercise 2. Each pipeline has one injected bug; all four checks are run.

bug initial loss overfit batch gradient check beats chance caught by
none pass pass pass pass —
gradient ascent (sign flipped) pass fail pass fail overfit, validation
softmax over the wrong axis fail fail fail fail all four
missing ReLU derivative pass fail fail pass overfit, gradient
inputs ×1000 (no normalisation) fail fail pass fail initial loss, overfit, validation
labels shuffled against inputs pass fail pass fail overfit, validation
learning rate ×100 pass fail pass fail overfit, validation
  • The clean pipeline passed everything, and every bug failed at least one check.
  • Overfitting a single batch caught all six bugs, the best value of any single check here. Shuffled labels were caught too, because this small network on 2-D data could not memorise contradictory labels.
  • The gradient check caught only backward-pass bugs. It is blind to data, optimiser and learning-rate errors.
  • Validation accuracy missed the broken ReLU gradient. The network still learned the task with a wrong gradient, so “it trains and validates fine” does not prove the code is correct.
  • No check is redundant. Run them all; each takes seconds.
🐍 Exercise 2 (research): a bug-catching matrix
Inject six classic bugs into the training pipeline one at a time and record which of the four sanity checks catches each.

Debugging training in AI, robotics and aerospace

  • Data-pipeline bugs: misaligned sensor timestamps or label files are the robotics version of “shuffled labels”. Validation against a simple baseline catches them.
  • Expensive runs: run the checks on a small slice of data before launching multi-day training on shared GPU clusters.
  • Assurance: recorded sanity checks, curves and seeds are evidence in a model’s verification and validation file.

References

  1. A. Karpathy (2019). “A recipe for training neural networks.” Blog post.
  2. Y. Bengio (2012). “Practical recommendations for gradient-based training of deep architectures.” In Neural Networks: Tricks of the Trade.
  3. C. Zhang, S. Bengio, M. Hardt, B. Recht, O. Vinyals (2017). “Understanding deep learning requires rethinking generalization.” Proc. ICLR.
  4. Y. LeCun, L. Bottou, G. Orr, K.-R. Müller (1998). “Efficient BackProp.” In Neural Networks: Tricks of the Trade.