SafeZone AI Learn
Learn/ Mathematical Foundations/ Optimization · lesson 5 of 7

Momentum and adaptive methods

Heavy ball, Nesterov, RMSProp and Adam — each derived, each raced on the surfaces that break plain gradient descent, ending at AdamW and why decoupled weight decay matters.

The gradient-descent lesson ended with a diagnosis: it pays for ill-conditioning linearly — κ\kappa ten times worse, ten times the iterations — and wastes most of its motion zig-zagging across ravines. This lesson is the treatment arc: first give the iterate memory (momentum), then give each coordinate its own learning rate (adaptive methods), then combine both and fix the resulting interaction with weight decay (AdamW). By the end, the optimizer line in every modern training script should read as a sequence of deliberate design decisions rather than folklore.

Momentum: the heavy ball

Polyak’s 1964 idea: keep an exponentially-decaying memory of past gradients — a velocity — and move along it:

vt+1=βvtηf(xt),xt+1=xt+vt+1,β[0,1).v_{t+1} = \beta\, v_t - \eta\, \nabla f(x_t), \qquad x_{t+1} = x_t + v_{t+1}, \qquad \beta \in [0, 1).

Two complementary readings:

As physics. These are the discretized equations of a ball with mass rolling on the loss surface with friction 1β1-\beta. Along the ravine floor, where the gradient points the same way step after step, velocity accumulates: the geometric series gives a terminal speed of ηf/(1β)\eta \lVert\nabla f\rVert / (1-\beta) — an effective learning rate of η/(1β)\eta/(1-\beta), so β=0.9\beta = 0.9 is a 10× amplifier in consistent directions (0.990.99 is 100×, which is why cranking β\beta without dropping η\eta diverges). Across the ravine, where the gradient flips sign each step, successive contributions cancel inside the velocity — the zig-zag is averaged away. One mechanism, both benefits.

As mathematics. On the quadratic, the two-step recurrence has spectral radius minimized at β=(κ1κ+1)2\beta^\star = \big(\tfrac{\sqrt{\kappa}-1}{\sqrt{\kappa}+1}\big)^2 with optimal η\eta, giving

rate  =  κ1κ+1vs.κ1κ+1   for GD.\text{rate} \;=\; \frac{\sqrt{\kappa} - 1}{\sqrt{\kappa} + 1} \qquad\text{vs.}\qquad \frac{\kappa - 1}{\kappa + 1} \;\text{ for GD.}

The condition number enters through its square root: at κ=100\kappa = 100, GD needs ~100 iterations per digit, momentum ~10. This is not a constant-factor tweak; it is a different complexity class, and (Nesterov proved) the best possible one for first-order methods.

Nesterov’s variant evaluates the gradient at the lookahead point xt+βvtx_t + \beta v_t rather than at xtx_t — the ball measures the slope where it is about to be, and brakes before overshooting instead of after. Same cost, provably optimal O(1/t2)O(1/t^2) rate on smooth convex problems, and a slightly better-damped trajectory you can see in the race below.

Adaptive methods: a learning rate per coordinate

Momentum still applies one global η\eta to every parameter. But real models mix parameters with wildly different gradient scales — embeddings of rare tokens receive sparse, tiny gradients while bias terms receive dense, large ones. The adaptive family normalizes each coordinate by a running estimate of its own gradient magnitude.

RMSProp (Hinton, famously published as a lecture slide) keeps an exponential moving average of squared gradients and divides by its root:

st=β2st1+(1β2)gt2,xt+1=xtηst+ε  gt(all operations element-wise).s_{t} = \beta_2 s_{t-1} + (1 - \beta_2)\, g_t^{2}, \qquad x_{t+1} = x_t - \frac{\eta}{\sqrt{s_t} + \varepsilon}\; g_t \qquad\text{(all operations element-wise).}

Coordinates with persistently large gradients get their steps shrunk; quiet coordinates get boosted. On a quadratic this is implicit preconditioning — it equalizes the effective curvature across axes, attacking κ\kappa directly rather than tolerating it as momentum does.

Adam = momentum on the first moment + RMSProp on the second + a correction for the zero-initialization of both EMAs:

mt=β1mt1+(1β1)gt,st=β2st1+(1β2)gt2,m_t = \beta_1 m_{t-1} + (1-\beta_1) g_t, \qquad s_t = \beta_2 s_{t-1} + (1-\beta_2) g_t^2, m^t=mt1β1t,s^t=st1β2t,xt+1=xtηm^ts^t+ε.\hat m_t = \frac{m_t}{1 - \beta_1^t}, \qquad \hat s_t = \frac{s_t}{1 - \beta_2^t}, \qquad x_{t+1} = x_t - \eta\, \frac{\hat m_t}{\sqrt{\hat s_t} + \varepsilon}.

The bias correction is not a detail: without it, m0=s0=0m_0 = s_0 = 0 makes the first steps systematically tiny (an EMA warm-starting from zero underestimates by the factor 1βt1-\beta^t), and with β2=0.999\beta_2 = 0.999 that shrinkage lingers for a thousand steps. Defaults β1=0.9\beta_1{=}0.9, β2=0.999\beta_2{=}0.999, ε=108\varepsilon{=}10^{-8} have survived a decade of scrutiny largely intact. A useful invariance to internalize: because gg appears homogeneously in numerator and denominator, Adam’s step size is insensitive to the overall scale of the gradient — rescale the loss by 100× and the update barely changes, which is much of why Adam is so forgiving to tune.

On the ravine (κ=60): GD zig-zags, momentum flies down the floor, the adaptive pair equalizes the axes and walks straight. On Rosenbrock, the curved valley punishes momentum’s inertia at the turn — watch it swing wide — while RMSProp/Adam corner tightly. On two wells, momentum’s speed is enough to coast through the shallow basin that traps GD from the same start. On the saddle, GD started on the axis stalls to a crawl; the others pass through. Drag starts, sweep η, break things — divergence is part of the syllabus.

AdamW: the weight-decay fix

For SGD, adding L2 regularization λ2x2\tfrac{\lambda}{2}\lVert x \rVert^2 to the loss and “weight decay” x(1ηλ)xx \leftarrow (1 - \eta\lambda)x are the same update. For Adam they are not: an L2 term enters through gtg_t and gets divided by s^t\sqrt{\hat s_t} like everything else, so heavily-updated parameters — precisely the ones you most want to regularize — receive the weakest effective decay. Loshchilov & Hutter’s fix is simply to take decay out of the gradient path:

xt+1=xtη(m^ts^t+ε+λxt)— decoupled decay, i.e. AdamW.x_{t+1} = x_t - \eta \Big( \frac{\hat m_t}{\sqrt{\hat s_t} + \varepsilon} + \lambda\, x_t \Big) \qquad\text{— decoupled decay, i.e. AdamW.}

The empirical gap this closes is why AdamW — not Adam — is the default in every serious transformer codebase, and why setting weight_decay in Adam-with-L2 mode silently does less than its name promises.

Choosing, in practice

SituationReach forWhy
Convex / classical ML (logistic, linear SVM)SGD or L-BFGSguarantees apply; adaptivity unneeded
CNNs on vision benchmarksSGD + momentum + cosine schedulestill edges out Adam on final accuracy in many settings
Transformers / LLMs, anything with embeddingsAdamW + warmupheterogeneous gradient scales make per-coordinate rates near-mandatory
RL policy gradientsAdam, small η\etanon-stationary objectives punish hand-tuned schedules
“It diverges immediately”lower η\eta 10×, add warmupyou are past the local 2/L2/L; curvature is largest at init

Starting points that respect the theory: SGD+momentum η101\eta \sim 10^{-1}10210^{-2} (effective rate is η/(1β)\eta/(1{-}\beta) — remember the 10×), AdamW η103\eta \sim 10^{-3}31043{\cdot}10^{-4}, λ102\lambda \sim 10^{-2}. Every one of these is a starting point for a sweep, not a conclusion — the SGD lesson’s noise-ball logic still sets the endgame, and schedules from that lesson compose with everything here.

One paragraph of honesty about “Adam vs SGD”

Adaptive methods win on trainability and robustness; carefully-tuned SGD+momentum sometimes wins on final generalization, and the literature has gone back and forth on why (Wilson et al. 2017 vs. much subsequent work). The stable takeaway for a practitioner: the gap, where it exists, is small next to the gap between either and a badly-chosen learning rate — tune η\eta first, argue metaphysics later.

Exercises

Work these before the next lesson

  1. Derive the η/(1β)\eta/(1-\beta) terminal velocity: for a constant gradient gg, show vtηg/(1β)v_t \to -\eta g / (1-\beta), and conclude why β1\beta \to 1 at fixed η\eta eventually violates the gradient-descent lesson’s stability ceiling.
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 4 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • B. T. Polyak, “Some methods of speeding up the convergence of iteration methods”, 1964 — heavy ball and the √κ rate.
  • Y. Nesterov, “A method for solving the convex programming problem with convergence rate O(1/k²)”, 1983.
  • G. Goh, “Why Momentum Really Works”, Distill 2017 — the interactive companion to the first half of this lesson.
  • T. Tieleman & G. Hinton, RMSProp — Coursera “Neural Networks for Machine Learning”, Lecture 6.5, 2012.
  • D. Kingma & J. Ba, “Adam: A Method for Stochastic Optimization”, ICLR 2015.
  • I. Loshchilov & F. Hutter, “Decoupled Weight Decay Regularization”, ICLR 2019 — AdamW.
  • A. C. Wilson et al., “The Marginal Value of Adaptive Gradient Methods in Machine Learning”, NeurIPS 2017 — the case for tuned SGD; read with its rebuttals.