Lesson 4.8core3 h

Training dynamics and debugging

Read loss curves like an engineer and find the bug before it costs a GPU-week

Most deep-learning time is spent not designing models but figuring out why a run is not working — and the failure is silent: the code runs, the loss moves, the model is garbage. This lesson turns Karpathy's recipe and a decade of folklore into a procedure: verify the data, compute the expected initial loss, overfit one batch, read the curves, inspect gradient and activation statistics, and handle numerical precision. It is the skill that separates people who train models from people who run scripts, and it pays off in every project from 5.10 Project: train and deploy an image classifier to 8.13 Project: fine-tune a small LLM with LoRA and ship a chat app.

You'll be able to

  • Follow the recipe: inspect data → baseline → overfit one batch → regularize → tune → squeeze
  • Compute the expected initial loss and use it to catch initialization bugs
  • Diagnose divergence, plateaus, noise, over- and under-fitting from loss curves
  • Monitor gradient norms, update-to-weight ratios, activation statistics and dead units
  • Explain fp16 vs bf16, loss scaling and the sources of NaN, and make runs reproducible

Key ideas

  1. Follow the recipe in order: inspect data → skeleton with baselines → overfit → regularize → tune → squeeze; each step makes the next debuggable.
  2. Expected initial loss for K classes is ln K (2.30 for 10 classes, ≈10.8 for a 50k vocab); a much higher value means over-confident initialization.
  3. If a model cannot overfit a single batch, it has a bug — not a regularization problem.
Show 4 more key ideas
  1. Loss-curve signatures: early NaN → LR/init/precision; flat → LR too low or dead gradients; spikes → bad batches/LR; train↓ val↑ → overfit.
  2. Log the pre-clip gradient norm, per-layer update/weight ratio (~1e-3), activation std and dead-unit fraction, and the actual LR.
  3. fp16 (max 65,504) needs loss scaling; bf16 has fp32's range and needs none; do reductions (softmax, norms) in fp32.
  4. Seed everything, log config + commit, checkpoint optimizer/scheduler/RNG state, and report variance across seeds.

Suggested path through this lesson