AI / ML Theory
Chapter 3 · 20 min

3.3 — Momentum and Nesterov accelerated gradient

Goal: Derive the momentum update, see why it accelerates in valleys and damps oscillations, then upgrade to Nesterov's look-ahead variant.

1. Why this session exists

Plain SGD treats every gradient as a fresh truth: θt+1=θt−αgt\theta_{t+1} = \theta_t - \alpha g_t. If the loss surface is an elongated valley — large curvature in some directions, small in others — SGD oscillates across the steep walls and crawls down the gentle floor. The fix is one of the oldest ideas in numerical optimisation: don't move along the instantaneous gradient, move along an average of recent gradients.

This is momentum. It uses the EMA of §3.2 directly. Nesterov's variant adds one cosmetic-looking but provably better twist: evaluate the gradient at the look-ahead point.


2. The classical momentum update

Maintain a velocity vt∈Rpv_t \in \mathbb{R}^p, initialised to v0=0v_0 = 0. At each step:

 vt=β vt−1+(1−β) gt,θt+1=θt−α vt. \boxed{\ \begin{aligned} v_t &= \beta\, v_{t-1} + (1 - \beta)\, g_t, \\ \theta_{t+1} &= \theta_t - \alpha\, v_t. \end{aligned} \ }

This is the EMA of §3.2 applied to the gradient, followed by a step in −vt-v_t. Typical β=0.9\beta = 0.9, so vtv_t averages the last ∼10\sim 10 gradients.


3. Why this helps: the elongated-valley intuition

Consider the quadratic L(θ)=12(λ1θ12+λ2θ22)L(\theta) = \frac{1}{2}(\lambda_1 \theta_1^2 + \lambda_2 \theta_2^2) with λ1≫λ2\lambda_1 \gg \lambda_2 — a long, narrow valley along the θ2\theta_2 axis. The gradient is g=(λ1θ1,λ2θ2)g = (\lambda_1 \theta_1, \lambda_2 \theta_2).

Plain SGD. Step is −αg-\alpha g. To make progress along the slow direction (θ2\theta_2), α\alpha has to be large. But large α\alpha in the fast direction (θ1\theta_1) overshoots: θ1(t+1)=(1−αλ1)θ1(t)\theta_1^{(t+1)} = (1 - \alpha \lambda_1) \theta_1^{(t)} flips sign if αλ1>1\alpha \lambda_1 > 1, oscillating. For stable progress in θ1\theta_1, α<2/λ1\alpha < 2/\lambda_1; in θ2\theta_2, the effective rate is then αλ2=λ2/λ1\alpha \lambda_2 = \lambda_2 / \lambda_1 — tiny. The condition number κ=λ1/λ2\kappa = \lambda_1 / \lambda_2 caps how fast SGD can move.

With momentum. Along θ1\theta_1 the gradient alternates sign on consecutive steps (because of the oscillation). The EMA averages those out — successive contributions cancel. Along θ2\theta_2 the gradient has consistent sign, so the EMA accumulates. Net effect: the optimiser damps motion across the steep walls and accelerates along the slow floor.

Quantitatively (Polyak, 1964; proof on a quadratic)[1], the optimal momentum coefficient is

β⋆=(κ−1κ+1)2,\beta^\star = \left(\frac{\sqrt{\kappa} - 1}{\sqrt{\kappa} + 1}\right)^2,

and the convergence rate improves from 1−1/κ1 - 1/\kappa (plain GD) to 1−1/κ1 - 1/\sqrt{\kappa} (momentum). A square-root speedup in condition number. For κ=104\kappa = 10^4 (entirely realistic for deep nets), that is a 100×100\times acceleration on the worst directions.[3, 4]


4. The damped-oscillator analogy

Polyak called this method heavy-ball. The update

θt+1=θt−α gt+β (θt−θt−1)\theta_{t+1} = \theta_t - \alpha\, g_t + \beta\,(\theta_t - \theta_{t-1})

(the un-normalised form, after substituting vtv_t) reads like the discretisation of the second-order ODE

mθ¨+γθ˙+∇L(θ)=0,m \ddot\theta + \gamma \dot\theta + \nabla L(\theta) = 0,

i.e. a ball with mass mm rolling on the loss surface, subject to friction γ\gamma. The mass smooths short-term gradient fluctuations (high curvature directions); friction prevents endless oscillation. The discrete coefficients (α,β)(\alpha, \beta) correspond to a time-step and mass/friction ratio.

For Luan: this is the same kind of damped-oscillator analysis you do in classical mechanics. The "tuning" of (α,β)(\alpha, \beta) is the same as picking a critically-damped friction in a mass-spring system — you want enough damping to avoid ringing but not so much that the ball gets stuck.


5. Nesterov accelerated gradient

Nesterov (1983)[2] noticed: if we are about to move to θt−αβvt−1\theta_t - \alpha \beta v_{t-1} anyway (because of momentum), then the next gradient we care about is the one there, not the one at θt\theta_t. Evaluate the gradient at the look-ahead point.

 θ~t=θt−α β vt−1,vt=β vt−1+(1−β) ∇L(θ~t),θt+1=θt−α vt. \boxed{\ \begin{aligned} \tilde\theta_t &= \theta_t - \alpha\, \beta\, v_{t-1}, \\ v_t &= \beta\, v_{t-1} + (1 - \beta)\, \nabla L(\tilde\theta_t), \\ \theta_{t+1} &= \theta_t - \alpha\, v_t. \end{aligned} \ }

The gradient is evaluated after taking a "momentum-only" partial step. Intuition: it's the difference between driving toward a wall and braking after you hit it versus seeing the wall coming and starting to brake earlier. The correction is small per step but compounds.

For convex smooth problems, Nesterov's method achieves the optimal rate O(1/T2)O(1/T^2) for first-order methods — provably faster than plain GD's O(1/T)O(1/T) on the same class. Deep nets aren't convex, but Nesterov still tends to outperform classical momentum on many problems and is the default in some frameworks (torch.optim.SGD(nesterov=True)).


6. Implementation note: the "look-ahead trick"

The form in §5 is awkward to implement because θ~t\tilde \theta_t is not stored. A standard reparametrisation (Sutskever et al., 2013) keeps the iterate equal to θ~t\tilde\theta_t at all times, so the gradient is always evaluated at the iterate. Define θt′:=θt−αβvt−1\theta'_t := \theta_t - \alpha \beta v_{t-1}. Then the update can be rewritten as

vt=β vt−1+(1−β) ∇L(θt′),θt+1′=θt′−α(βvt+(1−β)∇L(θt′)).\begin{aligned} v_t &= \beta\, v_{t-1} + (1 - \beta)\, \nabla L(\theta'_t), \\ \theta'_{t+1} &= \theta'_t - \alpha\bigl(\beta v_t + (1 - \beta) \nabla L(\theta'_t)\bigr). \end{aligned}

This is what most deep-learning frameworks actually compute. No need to remember the previous θ\theta — the bookkeeping lives entirely in vv.


7. What momentum buys in deep learning

Three things, in practice.

(a) Better conditioning for free. Deep-net Hessians are notoriously ill-conditioned. Momentum's κ\sqrt{\kappa} rate is a real win.

(b) Variance reduction in mini-batch noise. The EMA averages out short-scale mini-batch noise (§3.1), so the optimiser sees a smoother trajectory than the raw gradient stream. This is one of the reasons momentum + SGD is competitive with adaptive methods on vision tasks.

(c) Cheap. One extra vector of memory (the same shape as θ\theta). One extra FMA per parameter per step. Negligible compared to the forward/backward cost.

Modern LLMs use Adam (§3.5), which is momentum + an adaptive per-parameter scale. Vision models often still use plain SGD with momentum because it generalises slightly better empirically. Both stories are real.


8. Takeaways

  • Momentum is just an EMA of gradients used as the descent direction.
  • On an elongated valley, it damps oscillations in steep directions and accumulates motion in shallow ones.
  • Optimal convergence rate on quadratics: 1−1/κ1 - 1/\sqrt{\kappa} vs 1−1/κ1 - 1/\kappa for GD — a square-root improvement in condition number.
  • Nesterov's look-ahead variant evaluates the gradient at θt−αβvt−1\theta_t - \alpha \beta v_{t-1}; the same EMA but on the look-ahead point.
  • Implementation: PyTorch's SGD(momentum=β, nesterov=True) ships both.

Self-test

(a) On L(θ)=12(λ1θ12+λ2θ22)L(\theta) = \tfrac{1}{2}(\lambda_1 \theta_1^2 + \lambda_2 \theta_2^2) with λ1=100\lambda_1 = 100, λ2=1\lambda_2 = 1, simulate 50 steps of plain GD with α=0.018\alpha = 0.018 from θ0=(1,1)\theta_0 = (1, 1). Then redo with momentum, β=0.9\beta = 0.9. Compare the trajectories.

(b) Derive Polyak's optimal rate ρ=1−2/(κ+1)\rho = 1 - 2/(\sqrt{\kappa} + 1) for momentum on a 1-D quadratic by writing the update as a 2×22 \times 2 linear recurrence on (θt,θt−1)(\theta_t, \theta_{t-1}), solving the characteristic polynomial, and finding the choice of (α,β)(\alpha, \beta) that minimises the spectral radius.

(c) Show that classical momentum and the un-normalised form vt=βvt−1+gtv_t = \beta v_{t-1} + g_t, θt+1=θt−αvt\theta_{t+1} = \theta_t - \alpha v_t are equivalent up to a learning-rate rescaling. What is the relation between the two LRs?

(d) Nesterov vs classical: on a strongly convex quadratic, both have rate ρ(κ)\rho(\kappa). For κ=1000\kappa = 1000, what is the asymptotic improvement of Nesterov over momentum? (Hint: rate 1−c/κ1 - c/\sqrt{\kappa} with a different constant cc.)


References

  1. [1]

    Polyak, B. T. (1964). "Some methods of speeding up the convergence of iteration methods". USSR Comp. Math., 4(5), 1–17. — Heavy-ball momentum (3.3).

    ↩
  2. [2]

    Nesterov, Y. (1983). "A method for solving the convex programming problem with convergence rate O(1/k2)O(1/k^2)". Soviet Math. Dokl., 27, 372–376. — Nesterov accelerated gradient (3.3).

    ↩
  3. [3]

    Rae, J. W., Borgeaud, S., Cai, T., et al. (2021). "Scaling language models: Methods, analysis & insights from training Gopher" (MassiveText). arXiv:2112.11446 — Heuristic data-filtering rules (7.1 §3.3).

    ↩
  4. [4]

    Narayanan, D., Harlap, A., Phanishayee, A., et al. (2019). "PipeDream: Generalized pipeline parallelism for DNN training". arXiv:1806.03377 — 1F1B pipeline schedule (7.9 §3.3).

    ↩