Type at least 2 letters. Enter opens the first result, Esc closes.

    Optimization · Lesson 4 · Momentum and noise

    Momentum and Nesterov acceleration

    Intermediate 75 min 4 interactive figures 3 videos Colab notebook

    Before this lesson: lesson 03 (the factor ρ(η)\rho(\eta) of gradient descent on a quadratic and the best step 2/(μ+L)2/(\mu + L)), and linear algebra 05 (eigenvalues of a 2×22 \times 2 matrix, complex eigenvalues). For the folded part on conjugate gradients: linear algebra 14 (Krylov subspaces and CG). Time: about 75 minutes, plus the notebook.


    Why this matters #

    Every optimizer used to train large models has a momentum term: SGD with momentum=0.9, and Adam's β1=0.9\beta_1 = 0.9 (lesson 06). On an ill-conditioned problem gradient descent needs about κ\kappa steps per digit of accuracy, and momentum about κ\sqrt\kappa. Nesterov's method is the version with a guarantee for all smooth convex functions, and no method that only uses gradients can be faster in the worst case. It comes back in FISTA for the lasso (lesson 09). Conjugate gradients push the same idea to the limit on quadratics: they solve the large sparse linear systems of PDE solvers and the inner systems of Newton-type methods.

    1. Predict first #

    2. The picture #

    The zig-zag problem again. In a narrow valley the steep direction (curvature LL) forces a step of about 1/L1/L, or gradient descent diverges. Along the flat direction (curvature μ\mu) such a step makes progress of only about μ/L=1/κ\mu/L = 1/\kappa of the distance per step.

    Heavy ball. Polyak's heavy-ball method adds a fraction β\beta of the last step:

    xk+1=xk−η∇f(xk)+β (xk−xk−1),x−1=x0.x_{k+1} = x_k - \eta\nabla f(x_k) + \beta\,(x_k - x_{k-1}), \qquad x_{-1} = x_0 .

    Here η>0\eta > 0 is the step size and 0≤β<10 \le \beta < 1 is the momentum coefficient. With β=0\beta = 0 it is gradient descent. Think of a heavy ball rolling in the valley. Across the valley the gradient flips sign from step to step, so the added parts cancel. Along the valley the gradient keeps its direction, so they add up.

    The effective step. Suppose the gradient is a constant gg. Write Δk=xk−xk−1\Delta_k = x_k - x_{k-1} for the step. Then Δk+1=βΔk−ηg\Delta_{k+1} = \beta\Delta_k - \eta g. The steps approach the fixed point Δ=βΔ−ηg\Delta = \beta\Delta - \eta g, that is,

    Δ=−η1−β g.\Delta = -\frac{\eta}{1 - \beta}\,g .

    For β=0.9\beta = 0.9 the step becomes 10ηg10\eta g: ten times the gradient-descent step.

    Momentum as a filter. Unroll the step: Δk+1=−η(gk+βgk−1+β2gk−2+…)\Delta_{k+1} = -\eta\big(g_k + \beta g_{k-1} + \beta^2 g_{k-2} + \dots\big). The step is a weighted sum of all past gradients, with weights that decay like βj\beta^j. A part of the gradient that keeps its sign adds up to about 1/(1−β)1/(1 - \beta) times its size. A part that flips sign at every step adds up to about 1/(1+β)1/(1 + \beta) times its size. So momentum is a low-pass filter: it keeps the slow, consistent part of the gradient and damps the fast, oscillating part.

    Interactive figure: loading…

    Press Play and watch the three paths: gradient descent (blue), heavy ball (orange) and Nesterov (violet), each with its theory parameters. The chart shows ∥xk−x⋆∥/∥x0−x⋆∥\|x_k - x^\star\|/\|x_0 - x^\star\|; the dashed lines are the theory factors ρk\rho^k. Heavy ball first overshoots far across the valley, then wins by a wide margin. Increase κ\kappa and see the gap grow. With same η and β all three share one step (the slider η⋅L\eta \cdot L is the step in units of 1/L1/L) and one β\beta. Then choose Rosenbrock: heavy ball with the parameters that are best near the minimizer diverges, while Nesterov converges.

    Quick check

    The gradient stays equal to gg for many steps. Heavy ball uses η=0.01\eta = 0.01 and β=0.9\beta = 0.9. About how long is one step after many steps?

    One eigen-direction: a damped oscillator. Take f(x)=12λx2f(x) = \tfrac12\lambda x^2. Then ∇f(xk)=λxk\nabla f(x_k) = \lambda x_k, and heavy ball becomes

    xk+1=(1+β−ηλ) xk−β xk−1.x_{k+1} = (1 + \beta - \eta\lambda)\,x_k - \beta\,x_{k-1}.

    This is a second-order recurrence: the new value depends on the two last values. Such a recurrence behaves like a damped spring. Look for solutions xk=zkx_k = z^k. They must satisfy the characteristic polynomial

    z2−(1+β−ηλ) z+β=0.z^2 - (1 + \beta - \eta\lambda)\,z + \beta = 0 .

    Its two roots z1,z2z_1, z_2 decide everything: xk=az1k+bz2kx_k = a z_1^k + b z_2^k (or (a+bk)zk(a + bk)z^k for a double root).

    • Over-damped (too little momentum for this ηλ\eta\lambda): two real positive roots. xkx_k creeps toward 0 without oscillating, like gradient descent with a small step.
    • Critically damped: a double root. The fastest decay without oscillation.
    • Under-damped (much momentum): complex roots. xkx_k oscillates around 0 and the oscillation decays.
    Interactive figure: loading…

    Move ηλ\eta\lambda and β\beta, or use the presets. The orange dots are the heavy-ball iterates xkx_k, the faint blue line is gradient descent with the same ηλ\eta\lambda, and the dashed lines are ±ρk\pm\rho^k, where ρ\rho is the size of the largest root. In the under-damped case the roots are complex and their size is exactly β\sqrt\beta, whatever ηλ\eta\lambda is. Find the critically damped point for β=0.5\beta = 0.5: it is at ηλ=(1−0.5)2≈0.086\eta\lambda = (1 - \sqrt{0.5})^2 \approx 0.086.

    A quadratic in dd dimensions is dd such oscillators, one per eigenvalue of the Hessian. They share one η\eta and one β\beta. The best choice makes all of them decay at the same rate. Section 3 computes it.

    3. Definitions and main results #

    Throughout: f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with AA symmetric and its eigenvalues in [μ,L][\mu, L], μ>0\mu > 0. The minimizer is x⋆=A−1bx^\star = A^{-1}b, the error is ek=xk−x⋆e_k = x_k - x^\star, and κ=L/μ\kappa = L/\mu.

    Theorem 1 (heavy ball on a quadratic) #

    With

    η=4(L+μ)2,β=(κ−1κ+1)2,\eta = \frac{4}{(\sqrt L + \sqrt\mu)^2}, \qquad \beta = \Big(\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\Big)^2,

    every eigen-component ckc_k of the heavy-ball error satisfies ∣ck∣≤(a+bk) ρk|c_k| \le (a + bk)\,\rho^k, with constants a,b≥0a, b \ge 0 that depend on the start, and

    ρ=β=κ−1κ+1.\rho = \sqrt\beta = \frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}.

    These are Polyak's parameters.

    Why this is true, in one sentence. The product of the two roots of z2−(1+β−ηλ)z+βz^2 - (1 + \beta - \eta\lambda)z + \beta is always β\beta; when the roots are complex, both have size β\sqrt\beta; so we choose η\eta and β\beta to make the roots complex for every λ∈[μ,L]\lambda \in [\mu, L], with the smallest possible β\beta.

    Proof.

    1. ∇f(xk)=Axk−b=Aek\nabla f(x_k) = Ax_k - b = Ae_k, so ek+1=ek−ηAek+β(ek−ek−1)e_{k+1} = e_k - \eta Ae_k + \beta(e_k - e_{k-1}).
    2. Write A=QΛQ⊤A = Q\Lambda Q^\top. In the eigenbasis the components do not interact. For one eigenvalue λ\lambda with component ckc_k:
    (ck+1ck)=Tλ(ckck−1),Tλ=(1+β−ηλ−β10).\begin{pmatrix} c_{k+1} \\ c_k \end{pmatrix} = T_\lambda \begin{pmatrix} c_k \\ c_{k-1} \end{pmatrix}, \qquad T_\lambda = \begin{pmatrix} 1 + \beta - \eta\lambda & -\beta \\ 1 & 0 \end{pmatrix}.

    TλT_\lambda is the iteration matrix: the second row only copies ckc_k down. 3. The eigenvalues zz of TλT_\lambda solve det⁡(Tλ−zI)=z2−(1+β−ηλ)z+β=0\det(T_\lambda - zI) = z^2 - (1 + \beta - \eta\lambda)z + \beta = 0. The product of the two roots is β\beta. Complex roots are conjugates, zz and zˉ\bar z, so then ∣z∣2=zzˉ=β|z|^2 = z\bar z = \beta and ∣z∣=β|z| = \sqrt\beta, whatever λ\lambda is. 4. The roots are complex (or double) when the discriminant is ≤0\le 0: (1+β−ηλ)2≤4β(1 + \beta - \eta\lambda)^2 \le 4\beta, that is, ∣1+β−ηλ∣≤2β|1 + \beta - \eta\lambda| \le 2\sqrt\beta. This is the band (1−β)2≤ηλ≤(1+β)2(1 - \sqrt\beta)^2 \le \eta\lambda \le (1 + \sqrt\beta)^2. 5. We want every λ∈[μ,L]\lambda \in [\mu, L] in the band, with the smallest possible β\beta. So we ask for equality at both ends: ημ=1−β\sqrt{\eta\mu} = 1 - \sqrt\beta and ηL=1+β\sqrt{\eta L} = 1 + \sqrt\beta. 6. Add the two equations: η (L+μ)=2\sqrt\eta\,(\sqrt L + \sqrt\mu) = 2, so η=4/(L+μ)2\eta = 4/(\sqrt L + \sqrt\mu)^2. Divide them: κ=(1+β)/(1−β)\sqrt\kappa = (1 + \sqrt\beta)/(1 - \sqrt\beta), so β=(κ−1)/(κ+1)\sqrt\beta = (\sqrt\kappa - 1)/(\sqrt\kappa + 1). 7. Now every TλT_\lambda has spectral radius ρ=β\rho = \sqrt\beta. At λ=μ\lambda = \mu and λ=L\lambda = L the root is double, and the component there is (a+bk)ρk(a + bk)\rho^k: the same rate, up to a factor kk. ■\blacksquare

    Interactive figure: loading…

    The roots in the complex plane. For many eigenvalues λ\lambda between μ\mu and LL, the figure draws both roots of z2−(1+β−ηλ)z+βz^2 - (1 + \beta - \eta\lambda)z + \beta as orange dots. The solid circle is the unit circle (outside it: divergence); the dashed circle has radius β\sqrt\beta. With Polyak's parameters every root lies on the dashed circle. Make β\beta smaller: the roots for λ\lambda near μ\mu leave the circle along the real axis, and the largest root size grows. Press β = 0 to see plain gradient descent, whose roots 1−ηλ1 - \eta\lambda all lie on the real axis.

    Compare with gradient descent. For heavy ball, ρ=(κ−1)/(κ+1)≈1−2/κ\rho = (\sqrt\kappa - 1)/(\sqrt\kappa + 1) \approx 1 - 2/\sqrt\kappa. For gradient descent with its best step, (κ−1)/(κ+1)≈1−2/κ(\kappa - 1)/(\kappa + 1) \approx 1 - 2/\kappa. Per digit of accuracy, gradient descent needs O(κ)O(\kappa) steps and heavy ball O(κ)O(\sqrt\kappa).

    Stability with momentum. Outside the band of step 4 one real root is larger than β\sqrt\beta in size. Heavy ball on a quadratic converges from every start exactly when

    0≤β<1and0<ηλmax⁡<2(1+β).0 \le \beta < 1 \quad\text{and}\quad 0 < \eta\lambda_{\max} < 2(1 + \beta).

    So momentum also raises the stability limit of the step, from 2/λmax⁡2/\lambda_{\max} to 2(1+β)/λmax⁡2(1 + \beta)/\lambda_{\max}. (These are the conditions for both roots of z2−(1+β−ηλ)z+βz^2 - (1 + \beta - \eta\lambda)z + \beta to lie inside the unit circle, for every eigenvalue.)

    Why the factor kk matters. At a double root the error is (a+bk)ρk(a + bk)\rho^k, not ρk\rho^k. The factor kk grows while ρk\rho^k shrinks, so the error can grow at first, and the measured count is larger than ln⁡(1/ε)/ln⁡(1/ρ)\ln(1/\varepsilon)/\ln(1/\rho). In the predict-first example this is 44 steps instead of 34.

    Interactive figure: loading…

    Steps against κ\kappa, on log-log axes. Move the marker. Gradient descent (best step) lies on a line of slope 1: steps ∝κ\propto \kappa. Heavy ball and Nesterov lie on lines of slope 1/21/2: steps ∝κ\propto \sqrt\kappa. The dashed lines are the formulas ln⁡106/ln⁡κ+1κ−1\ln 10^6/\ln\frac{\kappa + 1}{\kappa - 1} and ln⁡106/ln⁡κ+1κ−1\ln 10^6/\ln\frac{\sqrt\kappa + 1}{\sqrt\kappa - 1}. Conjugate gradient (grey) is never worse than heavy ball and is often better, because it adapts to the actual eigenvalues (folded part at the end of section 3).

    Quick check

    A quadratic has κ=100\kappa = 100. Gradient descent with its best step needs 691 steps for a factor 10610^6. Which count is closest for heavy ball with Polyak's parameters?

    Nesterov's accelerated gradient #

    Nesterov's method takes the gradient at an extrapolated look-ahead point yky_k instead of at xkx_k. Start with y1=x0y_1 = x_0. For k=1,2,…k = 1, 2, \dots:

    xk=yk−1L∇f(yk),yk+1=xk+βk (xk−xk−1).x_k = y_k - \tfrac1L\nabla f(y_k), \qquad y_{k+1} = x_k + \beta_k\,(x_k - x_{k-1}).

    So x1=x0−1L∇f(x0)x_1 = x_0 - \tfrac1L\nabla f(x_0) is a plain gradient step. Two choices of βk\beta_k:

    • for convex ff: t1=1t_1 = 1, tk+1=(1+1+4tk2)/2t_{k+1} = \big(1 + \sqrt{1 + 4t_k^2}\big)/2, βk=(tk−1)/tk+1\beta_k = (t_k - 1)/t_{k+1}. It starts at 0 and grows toward 1;
    • for μ\mu-strongly convex ff: the constant β=(κ−1)/(κ+1)\beta = (\sqrt\kappa - 1)/(\sqrt\kappa + 1).

    Compare with heavy ball: heavy ball computes the gradient at xkx_k and then adds the momentum. Nesterov first moves with the momentum, then computes the gradient where it lands. This look-ahead corrects the step before the overshoot happens.

    Theorem 2 (Nesterov's rates) #

    For every LL-smooth convex ff with a minimizer x⋆x^\star (not only quadratics), the convex choice of βk\beta_k gives

    f(xk)−f⋆≤2L ∥x0−x⋆∥2(k+1)2f(x_k) - f^\star \le \frac{2L\,\|x_0 - x^\star\|^2}{(k + 1)^2}

    (Beck and Teboulle 2009, Thm 4.4). If ff is also μ\mu-strongly convex, the constant β\beta gives

    f(xk)−f⋆≤μ+L2 ∥x0−x⋆∥2 e−k/κf(x_k) - f^\star \le \frac{\mu + L}{2}\,\|x_0 - x^\star\|^2\, e^{-k/\sqrt\kappa}

    (Bubeck, Thm 3.18). Since e−1/κ≈1−1/κe^{-1/\sqrt\kappa} \approx 1 - 1/\sqrt\kappa, this is about (1−1/κ)k(1 - 1/\sqrt\kappa)^k. Gradient descent gets only O(L∥x0−x⋆∥2/k)O(L\|x_0 - x^\star\|^2/k) and (1−1/κ)k(1 - 1/\kappa)^k (lesson 03). The proofs use estimate sequences or a potential function (Bubeck, §3.7); here we only state the results.

    On a quadratic you can see the 1−1/κ1 - 1/\sqrt\kappa. Per eigenvalue λ\lambda, Nesterov's recursion with step 1/L1/L is ck+1=(1−λ/L)[(1+β)ck−βck−1]c_{k+1} = (1 - \lambda/L)\big[(1 + \beta)c_k - \beta c_{k-1}\big]. At λ=μ\lambda = \mu its characteristic polynomial has a double root z=1−1/κz = 1 - 1/\sqrt\kappa. For larger λ\lambda the roots are complex with ∣z∣2=β(1−λ/L)|z|^2 = \beta(1 - \lambda/L), and at λ=L\lambda = L both roots are 0. So on quadratics the factor is exactly 1−1/κ1 - 1/\sqrt\kappa (Lessard, Recht and Packard, Prop. 1). This is slower than heavy ball's 1−2/(κ+1)1 - 2/(\sqrt\kappa + 1), but it comes with a proof for every smooth strongly convex ff.

    The loss is not monotone. Gradient descent with step 1/L1/L decreases ff at every step. Nesterov's method does not: f(xk)f(x_k) can go up for a while. Example: the convex choice of βk\beta_k on f(x)=12(0.01x12+x22)f(x) = \tfrac12(0.01x_1^2 + x_2^2) from (1,1)(1, 1) gives f(x37)≈8.1×10−8f(x_{37}) \approx 8.1 \times 10^{-8} and f(x38)≈2.2×10−6f(x_{38}) \approx 2.2 \times 10^{-6} (checked in the notebook). The bound of Theorem 2 still holds at every step; only the path to it has ripples.

    Quick check

    You run Nesterov's method (convex choice of βk\beta_k) and see f(xk+1)>f(xk)f(x_{k+1}) > f(x_k) at some step. What does this tell you?

    Lower bounds: why these rates are the best possible #

    Take any method whose iterates satisfy xk∈x0+span⁡{∇f(x0),…,∇f(xk−1)}x_k \in x_0 + \operatorname{span}\{\nabla f(x_0), \dots, \nabla f(x_{k-1})\}: it moves only along gradients it has seen. Gradient descent, heavy ball, Nesterov and CG all do. Bubeck (§3.5) proves:

    • for k≤(n−1)/2k \le (n - 1)/2 there is an LL-smooth convex quadratic on Rn\mathbb{R}^n with min⁡0≤i<kf(xi)−f⋆≥3L∥x0−x⋆∥2/(32(k+1)2)\min_{0 \le i < k} f(x_i) - f^\star \ge 3L\|x_0 - x^\star\|^2/\big(32(k + 1)^2\big) (Thm 3.14);
    • there is an LL-smooth, μ\mu-strongly convex function with f(xk)−f⋆≥μ2(κ−1κ+1)2k∥x0−x⋆∥2f(x_k) - f^\star \ge \tfrac{\mu}{2}\big(\tfrac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\big)^{2k}\|x_0 - x^\star\|^2 (Thm 3.15). Bubeck numbers the start x1x_1; we write both bounds with the start x0x_0.

    Why. The hard function is a "chain" quadratic with a tridiagonal AA. A point that is nonzero only in its first jj coordinates has a gradient that is nonzero only in the first j+1j + 1 coordinates. So after kk gradients, the iterate is still zero in all later coordinates, but x⋆x^\star is not. Information travels one coordinate per step.

    So Nesterov's method is optimal up to constants: in the convex case both bounds are of order L∥x0−x⋆∥2/k2L\|x_0 - x^\star\|^2/k^2 (their ratio tends to 64/3≈2164/3 \approx 21), and in the strongly convex case both need Θ(κlog⁡(1/ε))\Theta(\sqrt\kappa\log(1/\varepsilon)) steps.

    Why heavy ball has no global guarantee #

    The proof of Theorem 1 uses one fixed matrix TλT_\lambda per eigenvalue. On a function that is not quadratic, the curvature changes from step to step, so the error is multiplied by a different matrix each time. Each matrix has spectral radius at most ρ<1\rho < 1, but the matrices are not symmetric, and a product of such matrices can grow.

    The counterexample of Lessard, Recht and Packard (2016, §4.6). A convex 1D function with μ=1\mu = 1 and L=25L = 25 (its second derivative is 25, then 1, then 25):

    f′(x)={25xx<1x+241≤x<225x−24x≥2.f'(x) = \begin{cases} 25x & x < 1 \\ x + 24 & 1 \le x < 2 \\ 25x - 24 & x \ge 2. \end{cases}

    With Polyak's parameters for κ=25\kappa = 25, η=1/9\eta = 1/9 and β=4/9\beta = 4/9, and a start x0∈[3.07,3.46]x_0 \in [3.07, 3.46], heavy ball falls into a cycle and never converges: it repeats 2.116→0.647→−1.8022.116 \to 0.647 \to -1.802 forever (section 5 checks it). Nesterov's method with η=1/25\eta = 1/25 and β=2/3\beta = 2/3 converges on the same function, as Theorem 2 promises.

    Goujaud, Taylor and Dieuleveut (2023) prove more: for every choice of constant parameters, heavy ball either does not accelerate on quadratics or fails to converge on some smooth strongly convex function. Locally it is fine: if ff is twice differentiable and μI⪯∇2f(x⋆)⪯LI\mu I \preceq \nabla^2 f(x^\star) \preceq LI, heavy ball started close enough to x⋆x^\star converges at a rate close to its quadratic rate (Polyak 1964). This is one reason why it works well in practice.

    Quick check

    ff is smooth and strongly convex with μ=1\mu = 1 and L=25L = 25, but not quadratic. You run heavy ball with η=1/9\eta = 1/9, β=4/9\beta = 4/9. What can you say for sure?

    In PyTorch #

    torch.optim.SGD(params, lr=η, momentum=β) keeps a buffer vv and does

    vk+1=βvk+∇f(xk),xk+1=xk−η vk+1,v0=0.v_{k+1} = \beta v_k + \nabla f(x_k), \qquad x_{k+1} = x_k - \eta\,v_{k+1}, \qquad v_0 = 0 .

    This is heavy ball: xk+1−xk=−ηvk+1=−η∇f(xk)+β(−ηvk)=−η∇f(xk)+β(xk−xk−1)x_{k+1} - x_k = -\eta v_{k+1} = -\eta\nabla f(x_k) + \beta(-\eta v_k) = -\eta\nabla f(x_k) + \beta(x_k - x_{k-1}). With nesterov=True it uses xk+1=xk−η (∇f(xk)+βvk+1)x_{k+1} = x_k - \eta\,(\nabla f(x_k) + \beta v_{k+1}). This is Nesterov's method with a constant β\beta, written in the look-ahead variable: PyTorch's parameter after kk steps equals yk+1y_{k+1} above (checked in the notebook). We write the momentum as β\beta; PyTorch's documentation calls it μ\mu.

    The Krylov view: gradient descent, heavy ball and conjugate gradients

    Let r0=Ax0−b=∇f(x0)r_0 = Ax_0 - b = \nabla f(x_0) and Kk(A,r0)=span⁡{r0,Ar0,…,Ak−1r0}K_k(A, r_0) = \operatorname{span}\{r_0, Ar_0, \dots, A^{k-1}r_0\}, the Krylov subspace. Write ∥e∥A=e⊤Ae\|e\|_A = \sqrt{e^\top Ae} for the AA-norm.

    1. Gradient descent, heavy ball and Nesterov add to the current point a gradient (AekAe_k, or A(yk−x⋆)A(y_k - x^\star) for Nesterov) and a multiple of the last step. By induction, xk−x0∈Kk(A,r0)x_k - x_0 \in K_k(A, r_0).
    2. Hence ek=pk(A) e0e_k = p_k(A)\,e_0 for a polynomial pkp_k of degree at most kk with pk(0)=1p_k(0) = 1. Gradient descent: pk(λ)=(1−ηλ)kp_k(\lambda) = (1 - \eta\lambda)^k. Heavy ball: p0=1p_0 = 1, p1=1−ηλp_1 = 1 - \eta\lambda, pk+1=(1+β−ηλ)pk−βpk−1p_{k+1} = (1 + \beta - \eta\lambda)p_k - \beta p_{k-1}. This is the Chebyshev iteration with its coefficients frozen at their limit values, so pkp_k is close to the scaled Chebyshev polynomial, the polynomial with the smallest maximum on [μ,L][\mu, L].
    3. Expand e0=∑iciqie_0 = \sum_i c_i q_i in the eigenvectors qiq_i of AA. Then ∥p(A)e0∥A2=∑iλici2 p(λi)2\|p(A)e_0\|_A^2 = \sum_i \lambda_i c_i^2\,p(\lambda_i)^2.
    4. Conjugate gradient (CG) chooses xk∈x0+Kk(A,r0)x_k \in x_0 + K_k(A, r_0) to minimize ∥ek∥A\|e_k\|_A (linear algebra 14). So it uses the best polynomial for this spectrum and this e0e_0. The heavy-ball polynomial is one of the candidates, so ∥ekCG∥A≤∥ekHB∥A\|e_k^{\text{CG}}\|_A \le \|e_k^{\text{HB}}\|_A for every kk. The same holds against gradient descent and Nesterov.
    5. Two consequences. CG has the same worst-case bound, ∥ek∥A≤2ρk∥e0∥A\|e_k\|_A \le 2\rho^k\|e_0\|_A with ρ=(κ−1)/(κ+1)\rho = (\sqrt\kappa - 1)/(\sqrt\kappa + 1), without knowing μ\mu and LL. And if AA has mm distinct eigenvalues, p(λ)=∏j=1m(1−λ/λj)p(\lambda) = \prod_{j=1}^m (1 - \lambda/\lambda_j) is zero on the whole spectrum, so CG is exact after mm steps.

    The CG figure in linear algebra 14 shows this on a 2D quadratic: CG reaches x⋆x^\star in 2 steps (two distinct eigenvalues), while steepest descent zig-zags.

    Why is CG not used to train neural networks? Its optimality needs exact gradients of one fixed quadratic. Minibatch gradients are noisy and the loss is not quadratic, so conjugacy is lost. Momentum keeps working with noisy gradients and changing curvature.

    Answers to section 1. (1) About 45: section 4 counts 44. The factor per step is 2/32/3 instead of 12/1312/13; the factor kk from the double roots costs about 10 extra steps. (2) Not always. The Lessard–Recht–Packard function has exactly μ=1\mu = 1 and L=25L = 25, and heavy ball with these parameters cycles on it. Nesterov's method converges on it.

    4. Worked example #

    f(x)=12(x12+25x22)f(x) = \tfrac12(x_1^2 + 25x_2^2), x⋆=0x^\star = 0, x0=(1,1)x_0 = (1, 1). So μ=1\mu = 1, L=25L = 25, κ=5\sqrt\kappa = 5. "Steps" means the first kk with ∥ek∥≤10−6∥e0∥\|e_k\| \le 10^{-6}\|e_0\|.

    Method Parameters Factor per step Steps
    gradient descent η=2/(μ+L)=1/13\eta = 2/(\mu + L) = 1/13 12/13≈0.92312/13 \approx 0.923 173
    heavy ball η=4/36=1/9\eta = 4/36 = 1/9, β=(4/6)2=4/9\beta = (4/6)^2 = 4/9 β=2/3\sqrt\beta = 2/3 44
    Nesterov η=1/25\eta = 1/25, β=4/6=2/3\beta = 4/6 = 2/3 1−1/5=0.81 - 1/5 = 0.8 73
    conjugate gradient none — 2

    Gradient descent. Each coordinate is multiplied by 1−λ/131 - \lambda/13: by 12/1312/13 and by 1−25/13=−12/131 - 25/13 = -12/13. So ∥ek∥=(12/13)k∥e0∥\|e_k\| = (12/13)^k\|e_0\|, and k≥ln⁡106/ln⁡(13/12)=172.6k \ge \ln 10^6/\ln(13/12) = 172.6.

    Heavy ball, by hand. Step 1 has no momentum yet (x−1=x0x_{-1} = x_0): x1=(1−19⋅1, 1−19⋅25)=(8/9, −16/9)x_1 = \big(1 - \tfrac19\cdot 1,\ 1 - \tfrac19\cdot 25\big) = (8/9,\ -16/9). The steep coordinate overshoots. Step 2:

    x2=x1−19∇f(x1)+49(x1−x0)=(89−881−481, −169+40081−10081)=(2027, 5227).x_2 = x_1 - \tfrac19\nabla f(x_1) + \tfrac49(x_1 - x_0) = \Big(\tfrac89 - \tfrac{8}{81} - \tfrac{4}{81},\ -\tfrac{16}{9} + \tfrac{400}{81} - \tfrac{100}{81}\Big) = \Big(\tfrac{20}{27},\ \tfrac{52}{27}\Big).

    For λ=1\lambda = 1: 1+49−19=431 + \tfrac49 - \tfrac19 = \tfrac43, so the polynomial is z2−43z+49=(z−23)2z^2 - \tfrac43z + \tfrac49 = (z - \tfrac23)^2. For λ=25\lambda = 25: 1+49−259=−431 + \tfrac49 - \tfrac{25}{9} = -\tfrac43, so it is z2+43z+49=(z+23)2z^2 + \tfrac43z + \tfrac49 = (z + \tfrac23)^2. Both roots are double, so each component is (a+bk)(±23)k(a + bk)(\pm\tfrac23)^k. The first two values fix aa and bb:

    ek,1=(1+k3)(23)k,ek,2=(1+5k3)(−23)k.e_{k,1} = \Big(1 + \frac k3\Big)\Big(\frac23\Big)^k, \qquad e_{k,2} = \Big(1 + \frac{5k}{3}\Big)\Big(-\frac23\Big)^k .

    Check at k=2k = 2: 53⋅49=2027\tfrac53\cdot\tfrac49 = \tfrac{20}{27} ✓ and 133⋅49=5227\tfrac{13}{3}\cdot\tfrac49 = \tfrac{52}{27} ✓. The norm first grows, to 1.46 ∥e0∥1.46\,\|e_0\| at k=2k = 2, then falls fast. Because of the factor kk we need 44 steps, not ln⁡106/ln⁡1.5=34.1\ln 10^6/\ln 1.5 = 34.1.

    Nesterov. The step 1/L1/L multiplies the steep coordinate by 1−25/25=01 - 25/25 = 0, so it is zero after one step and stays zero. The flat coordinate has the double root 0.80.8: ek,1=(1+0.2k) 0.8ke_{k,1} = (1 + 0.2k)\,0.8^k, so x1=(0.96,0)x_1 = (0.96, 0) and x2=(0.896,0)x_2 = (0.896, 0). That gives 73 steps.

    Conjugate gradient. There are two distinct eigenvalues, so the polynomial p2(λ)=(1−λ)(1−λ/25)p_2(\lambda) = (1 - \lambda)(1 - \lambda/25) is zero on both: x2=x⋆x_2 = x^\star exactly.

    The polynomials after two steps, at λ=1\lambda = 1 and λ=25\lambda = 25. Gradient descent, (1−λ/13)2(1 - \lambda/13)^2: 0.852 and 0.852. Heavy ball, 1−22λ/81+λ2/811 - 22\lambda/81 + \lambda^2/81: 0.741 and 1.926. CG: 0 and 0. Early on, heavy ball is even worse than gradient descent along the steep direction; CG is never worse than either.

    5. In code #

    NumPy checks the worked example and the counterexample. The notebook has these cells and the exercises.

    import numpy as np
    
    mu, L = 1.0, 25.0
    s = np.sqrt(L / mu)                                  # sqrt(kappa) = 5
    eta = 4 / (np.sqrt(L) + np.sqrt(mu)) ** 2            # 1/9
    beta = ((s - 1) / (s + 1)) ** 2                      # 4/9
    
    # 1) spectral radius of the 2x2 heavy-ball matrix, inside and outside [mu, L]
    for lam in [0.5, 1, 9, 25, 30]:
        T = np.array([[1 + beta - eta * lam, -beta], [1, 0]])
        print(lam, round(np.abs(np.linalg.eigvals(T)).max(), 4))
    # 0.5 0.8889 | 1 0.6667 | 9 0.6667 | 25 0.6667 | 30 1.6134
    
    # 2) steps until ||e_k|| <= 1e-6 ||e_0|| on diag(1, 25) from (1, 1)
    lam = np.array([mu, L])
    def steps(update):
        e, e_prev, y, k = np.ones(2), np.ones(2), np.ones(2), 0
        while np.linalg.norm(e) > 1e-6 * np.sqrt(2):
            e, e_prev, y = update(e, e_prev, y)
            k += 1
        return k
    
    gd = lambda e, ep, y: ((1 - 2 / (mu + L) * lam) * e, e, y)
    hb = lambda e, ep, y: (e - eta * lam * e + beta * (e - ep), e, y)
    def nag(e, ep, y, b=(s - 1) / (s + 1)):
        x = y - lam * y / L                              # gradient step at the look-ahead point
        return x, e, x + b * (x - e)
    print(steps(gd), steps(hb), steps(nag))              # 173 44 73
    
    # 3) the Lessard-Recht-Packard function: mu = 1, L = 25, not a quadratic
    def grad(x):
        return 25 * x if x < 1 else (x + 24 if x < 2 else 25 * x - 24)
    x, x_prev = 3.3, 3.3
    for k in range(1000):
        x, x_prev = x - eta * grad(x) + beta * (x - x_prev), x
    print([round(float(v), 3) for v in (x_prev, x)])    # [0.647, -1.802]: part of the cycle 2.116 -> 0.647 -> -1.802
    

    The cycle has length 3: the iterates repeat 2.116,0.647,−1.8022.116, 0.647, -1.802 forever. The notebook also runs Nesterov's method on this function and checks that it converges.

    6. Where it shows up #

    • Training neural networks. torch.optim.SGD(momentum=0.9) and Adam's β1=0.9\beta_1 = 0.9: along a consistent direction the step is η/(1−β)=10η\eta/(1 - \beta) = 10\eta. With noisy minibatch gradients the gain over gradient descent is smaller, because the noise, not κ\kappa, limits the progress (Goh, "Why Momentum Really Works", section on stochastic gradients; lesson 05). nesterov=True gives Nesterov's method.
    • Sparse and nonsmooth problems. FISTA (lesson 09) is Nesterov's method plus a proximal step. It is a standard solver for the lasso and for compressed sensing and imaging inverse problems.
    • Scientific computing. Preconditioned CG solves the sparse symmetric positive definite systems of discretized elliptic PDEs (Poisson, implicit diffusion) and the Newton systems of Newton-CG with Hessian-vector products (lesson 10). In weather forecasting, the inner loop of incremental 4D-Var minimizes a quadratic cost with a preconditioned CG or Lanczos method.

    7. Common mistakes #

    • Expecting the κ\sqrt\kappa rate of Polyak's parameters on a problem that is not quadratic. It is proven only for quadratics (and near a minimizer). For general functions heavy ball can cycle.
    • Taking Nesterov's gradient at xkx_k. That is heavy ball. Nesterov uses the look-ahead point yky_k.
    • Ignoring the factor kk. At double roots the error is (a+bk)ρk(a + bk)\rho^k. It can grow at first, and a rate measured over a short window looks worse than ρ\rho.
    • Keeping the gradient-descent stability limit. With momentum the limit on a quadratic is η<2(1+β)/λmax⁡\eta < 2(1 + \beta)/\lambda_{\max}, not 2/λmax⁡2/\lambda_{\max}. And η/(1−β)\eta/(1 - \beta), not η\eta, is the effective step along a consistent direction: when you add momentum 0.9, divide the learning rate by about 10 to keep the same effective step.
    • Expecting f(xk)f(x_k) to decrease at every step. Heavy ball and Nesterov are not monotone.
    • Calling CG "always better". It is optimal only for exact gradients of one fixed quadratic.

    8. Check yourself #

    1. The eigenvalues lie in [μ,L]=[4,100][\mu, L] = [4, 100]. Give Polyak's η\eta, β\beta and factor, and the gradient-descent factor with its best step.
      Answerκ=25\kappa = 25, L=10\sqrt L = 10, μ=2\sqrt\mu = 2: η=4/144=1/36\eta = 4/144 = 1/36, β=(4/6)2=4/9\beta = (4/6)^2 = 4/9, factor 2/32/3. Gradient descent: (25−1)/(25+1)=12/13(25 - 1)/(25 + 1) = 12/13. The factors depend only on κ\kappa; the step scales like 1/L1/L.
    2. Why is ∣z∣=β|z| = \sqrt\beta for every λ\lambda inside the band (1−β)2≤ηλ≤(1+β)2(1 - \sqrt\beta)^2 \le \eta\lambda \le (1 + \sqrt\beta)^2?
      AnswerThe product of the two roots of z2−(1+β−ηλ)z+βz^2 - (1 + \beta - \eta\lambda)z + \beta is β\beta. Inside the band the roots are complex conjugates (or a double root), so they have the same size, and ∣z∣2=zzˉ=β|z|^2 = z\bar z = \beta.
    3. You run heavy ball with η=1/9\eta = 1/9, β=4/9\beta = 4/9 (tuned for L=25L = 25), but the true largest eigenvalue is 30. What happens?
      Answerηλ=30/9≈3.33\eta\lambda = 30/9 \approx 3.33, which is above 2(1+β)=26/9≈2.892(1 + \beta) = 26/9 \approx 2.89. One real root has size 1.61>11.61 > 1, so heavy ball diverges along that eigenvector.
    4. Write one step of torch.optim.SGD(lr=0.1, momentum=0.9) from v=(1,0)v = (1, 0) with gradient (0,2)(0, 2). What is the change in the parameters?
      Answerv←0.9 (1,0)+(0,2)=(0.9,2)v \leftarrow 0.9\,(1, 0) + (0, 2) = (0.9, 2), and the parameters change by −0.1 v=(−0.09,−0.2)-0.1\,v = (-0.09, -0.2).
    5. A∈R1000×1000A \in \mathbb{R}^{1000 \times 1000} is symmetric positive definite with only 3 distinct eigenvalues. How many CG steps give the exact solution, and why does this not contradict the lower bounds?
      AnswerAt most 3: ∏j=13(1−λ/λj)\prod_{j=1}^3(1 - \lambda/\lambda_j) vanishes on the spectrum. The lower bounds hold for a specific hard function with many distinct eigenvalues (and k≤(n−1)/2k \le (n - 1)/2); they are worst-case statements, not statements about every quadratic.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Write the heavy-ball update.

    2. Heavy ball with β=0.9\beta = 0.9 and a constant gradient gg: what step does it approach?

    3. Polyak's parameters for heavy ball on a quadratic with eigenvalues in [μ,L][\mu, L]?

    4. Factor per step of heavy ball with Polyak's parameters?

    5. Why do the complex roots of z2−(1+β−ηλ)z+βz^2 - (1 + \beta - \eta\lambda)z + \beta have size β\sqrt\beta?

    6. Steps per digit of accuracy: gradient descent against momentum?

    7. Stability condition of heavy ball on a quadratic?

    8. Write Nesterov's method (step 1/L1/L).

    9. Nesterov's rate for an LL-smooth convex ff?

    10. Why has heavy ball no global guarantee?

    Open the review →

    9. Interview angle #

    • "Why does momentum speed up gradient descent?" — On a quadratic each eigen-direction becomes a two-term recurrence. The best parameters give all roots the size (κ−1)/(κ+1)(\sqrt\kappa - 1)/(\sqrt\kappa + 1), so the step count per digit drops from O(κ)O(\kappa) to O(κ)O(\sqrt\kappa). Intuition: momentum averages the gradients, so the zig-zag across the valley cancels and the motion along it adds up.
    • "Heavy ball or Nesterov?" — Gradient at xkx_k or at the look-ahead point. Heavy ball's acceleration is proven only for quadratics; Nesterov is optimal for all smooth (strongly) convex functions.
    • "What is the best possible first-order rate?" — Ω(1/k2)\Omega(1/k^2) for smooth convex functions, and Θ(κlog⁡(1/ε))\Theta(\sqrt\kappa\log(1/\varepsilon)) steps for smooth strongly convex ones; Nesterov reaches both up to constants.
    • "What does momentum 0.9 do to the step size?" — Up to 1/(1−β)=101/(1 - \beta) = 10 times larger along consistent directions, and the stability limit on a quadratic becomes η<2(1+β)/λmax⁡\eta < 2(1 + \beta)/\lambda_{\max}.

    10. Practice #

    The notebook for this lesson runs in Colab. It has four exercises, each with a check cell and a folded solution:

    1. Build the 2×22 \times 2 heavy-ball matrix TλT_\lambda and check its spectral radius against β\sqrt\beta inside the band and outside it.
    2. Implement heavy ball and Nesterov, and count steps against gradient descent for κ=10,100,1000\kappa = 10, 100, 1000.
    3. Run Nesterov's method (convex choice of βk\beta_k) on a convex least-squares problem; check the O(1/k2)O(1/k^2) bound and find the steps where ff goes up.
    4. Implement conjugate gradients and check that CG is never worse than heavy ball in the AA-norm.

    11. Watch and read #

    Strang derives the best momentum parameters for a quadratic from the 2×2 matrix, as in Theorem 1, and gets the factor with √κ.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The practical view: momentum as an exponentially weighted average of gradients that damps the oscillation across a valley.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A short animation of the rolling-ball picture.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play