Lesson 4.5core3 h⚡ live demo

Optimizers and learning-rate schedules

From SGD to AdamW, and the schedule that matters more than the optimizer

The optimizer and its learning-rate schedule are the knobs that most often decide whether a run converges, diverges or quietly underperforms. Adam's update rule, its bias correction and the decoupled weight decay of AdamW are used verbatim to train every modern language model (7.8 Build GPT from scratch (nanoGPT walkthrough), 8.1 The LLM lifecycle and model landscape); warmup, cosine decay and gradient clipping are in every training config you will ever read. Knowing why each term exists lets you tune in minutes instead of days (4.8 Training dynamics and debugging).

You'll be able to

  • Write the update equations of SGD with momentum, Nesterov, AdaGrad, RMSProp, Adam and AdamW
  • Derive Adam's bias correction and explain what goes wrong without it
  • Explain why decoupled weight decay differs from L2 regularization under Adam
  • Choose a learning-rate schedule (warmup, cosine, one-cycle, WSD) and run a learning-rate range test
  • Apply gradient clipping and the linear scaling rule when changing batch size

Key ideas

  1. Momentum averages past gradients: consistent directions amplify up to 1/(1−β) ×, oscillating ones cancel; Nesterov evaluates the gradient at the look-ahead point.
  2. Adam normalizes each coordinate's step by its recent RMS gradient, so η sets the per-step movement independent of loss scale.
  3. Adam's bias correction divides m_t and v_t by (1−β₁ᵗ) and (1−β₂ᵗ); without it the first steps are ~3× too large.
Show 4 more key ideas
  1. AdamW decouples weight decay from the adaptive step; coupled L2 under Adam decays large-gradient weights less — use AdamW.
  2. Warmup protects the early, poorly-estimated adaptive steps; cosine (or WSD) decay delivers the final loss drop.
  3. Clip the global gradient norm (c ≈ 1) to prevent loss spikes; log the pre-clip norm as a diagnostic.
  4. When batch size grows k×, scale the LR (≈k for SGD, ≈√k for Adam) up to the critical batch size; beyond it, bigger batches stop helping.

Suggested path through this lesson