Gradient Descent

To minimize a function you can't solve in closed form, walk downhill. Turn a direction and watch the slope trace a cosine, steer descent across a narrow valley, compare convex and nonconvex landscapes against Newton's method, sample mini-batches, and see why the small eigenvalues of a quadratic set the speed of learning.

Follows S. Boyd and L. Vandenberghe, Convex Optimization (Cambridge University Press, 2004; free online), Chapter 9, and I. Goodfellow, Y. Bengio and A. Courville, Deep Learning (MIT Press, 2016), Chapter 8, for momentum and stochastic gradients. The questions and examples are written for this page. Gradient descent is the explicit Euler scheme for the gradient flows of the Gradient Flows page; its uses in machine learning are on the Neural Networks page. For how this subject connects to the others, see the roadmap.

InteractiveGoodfellow–Bengio–Courville, Ch. 8: Optimization for Training Deep Models

Gradient descent, momentum and mini-batches

Fitting a network means minimizing a loss over its weights. Gradient descent does it by walking downhill: from wherever you are, take a small step in the direction in which the loss decreases fastest. Which direction is that? The directional derivative answers it.

Start here: the directional derivative

Stand at a point pp on a surface f(x,y)f(x,y). In one dimension you can only go left or right, but here you can head off in any direction. Pick a unit vector vv. The directional derivative is the slope of ff in that direction:

Dvf(p)=limh→0⁡f(p+hv)−f(p)h=∇f(p)⋅v,D_vf(p)=\lim_{h\to0}\frac{f(p+hv)-f(p)}{h}=\nabla f(p)\cdot v,

the second equality by the chain rule applied to h↦f(p+hv)h\mapsto f(p+hv). Since ∇f⋅v=‖∇f‖‖v‖cos⁡ϕ\nabla f\cdot v=\|\nabla f\|\,\|v\|\cos\phi, where ϕ\phi is the angle between vv and the gradient, the slope as you turn vv all the way round is a cosine wave of height ‖∇f(p)‖\|\nabla f(p)\|:

  • it is largest, +‖∇f‖+\|\nabla f\|, at ϕ=0\phi=0: along the gradient, the steepest way up;
  • it is zero at ϕ=±π/2\phi=\pm\pi/2: along the contour line through pp;
  • it is smallest, −‖∇f‖-\|\nabla f\|, at ϕ=π\phi=\pi: along −∇f-\nabla f, the steepest way down.

That last line is the whole reason gradient descent steps along −∇f-\nabla f. Try it below: drag the direction round and find the minimum of the curve on the right.

Contours of f(x,y)=12x2+y2+13sin⁡(2x)sin⁡(y)f(x,y)=\tfrac12x^2+y^2+\tfrac13\sin(2x)\sin(y). Drag the point pp, and drag the tip of vv round the circle. ∇f(p)\nabla f(p), −∇f(p)-\nabla f(p).
The slope Dvf(p)D_vf(p) for every direction vv, by its angle. your vv. The lowest point is −∇f-\nabla f.

The idea

The update. To decrease a loss LL, step in its steepest downhill direction, −∇L-\nabla L, found above:

wt+1=wt−α∇L(wt).w_{t+1}=w_t-\alpha\,\nabla L(w_t).

The gradient says which way to go, not how far: the learning rate α\alpha is a choice. In machine learning L(θ)=1N∑i=1Nℓ(fθ(xi),yi)L(\theta)=\frac1N\sum_{i=1}^N\ell\big(f_\theta(x_i),y_i\big) is the average loss over the data, and computing its gradient through the network's layers is backpropagation.

Quadratics, one direction at a time. For L(w)=12(w−w⋆)⊤H(w−w⋆)L(w)=\tfrac12(w-w^\star)^\top H(w-w^\star) with HH symmetric positive definite, ∇L=H(w−w⋆)\nabla L=H(w-w^\star), so
wt+1−w⋆=(I−αH)(wt−w⋆).w_{t+1}-w^\star=(I-\alpha H)(w_t-w^\star).
Along an eigenvector of HH with eigenvalue λ\lambda, the error is multiplied by 1−αλ1-\alpha\lambda each step. Steep directions (large λ\lambda) need α<2/λmax⁡\alpha<2/\lambda_{\max} or the iterates blow up; flat directions (small λ\lambda) then crawl at rate 1−αλmin⁡1-\alpha\lambda_{\min}. The ratio κ=λmax⁡/λmin⁡\kappa=\lambda_{\max}/\lambda_{\min} sets the speed: about κ\kappa steps per factor of ee.
Momentum (heavy ball). Keep part of the previous step:
wt+1=wt−α∇L(wt)+β(wt−wt−1).w_{t+1}=w_t-\alpha\nabla L(w_t)+\beta\,(w_t-w_{t-1}).
In a flat valley the steps all point the same way and add up; across a narrow valley they alternate and cancel. Well tuned, it needs about κ\sqrt\kappa steps instead of κ\kappa.
Mini-batches. With millions of data points, use the average gradient over a random batch BtB_t of |B||B| of them: gt=1|B|∑i∈Bt∇ℓig_t=\frac1{|B|}\sum_{i\in B_t}\nabla\ell_i. It is unbiased, 𝔼[gt]=∇L\mathbb E[g_t]=\nabla L, but noisy, with variance shrinking like 1/|B|1/|B|. The noise is the price of cheap steps, and it keeps the iterates jittering near the minimum unless α\alpha is decreased.

Explore: descent on a valley

Contours of L(w)=12(w12+κw22)L(w)=\tfrac12(w_1^2+\kappa w_2^2); the minimum is at the centre. Drag the start. the first 60 steps.

Worked example

L(w)=12(w12+10w22)L(w)=\tfrac12(w_1^2+10w_2^2), so H=diag(1,10)H=\mathrm{diag}(1,10). Start at w0=(1,1)w_0=(1,1).

  1. With α=0.1\alpha=0.1: the per-step factors in each coordinate, and wtw_t.

    1−0.1⋅1=0.91-0.1\cdot1=0.9 and 1−0.1⋅10=01-0.1\cdot10=0. So w1=(0.9,0)w_1=(0.9,0): the steep coordinate is solved in one step, and wt=(0.9t,0)w_t=(0.9^t,0) after that. Reaching |w1|<10−3|w_1|<10^{-3} takes t≥66t\ge66 steps.

  2. With α=0.25\alpha=0.25?

    Factors 0.750.75 and 1−2.5=−1.51-2.5=-1.5. The second coordinate flips sign and grows by 1.5 each step: divergence, since α>2/λmax⁡=0.2\alpha>2/\lambda_{\max}=0.2.

  3. What is the best fixed α\alpha, and how fast is it?

    Balance the two factors: 1−α=−(1−10α)1-\alpha=-(1-10\alpha), so α=2/11\alpha=2/11 and both have size 9/11=κ−1κ+1≈0.829/11=\frac{\kappa-1}{\kappa+1}\approx0.82. About 35 steps for three digits. Momentum improves this to roughly κ−1κ+1≈0.52\frac{\sqrt\kappa-1}{\sqrt\kappa+1}\approx0.52 per step.

Check your understanding

Why step against the gradient rather than along it?

  1. ∇L⋅v=‖∇L‖‖v‖cos⁡ϕ\nabla L\cdot v=\|\nabla L\|\|v\|\cos\phi is smallest at ϕ=π\phi=\pi: the loss falls fastest opposite to the gradient.
  2. Along the gradient the step would be too long.Length is set by α\alpha; direction is the issue.
The gradient points uphill, in the direction of fastest increase. Gradient ascent (+α∇L+\alpha\nabla L) maximizes, as in policy-gradient methods that maximize expected return.

On an ill-conditioned quadratic (κ=100\kappa=100) plain gradient descent is slow because:

  1. α\alpha must be below 2/λmax⁡2/\lambda_{\max} for the steep direction, which makes progress in the flat direction tiny.
  2. The gradient points away from the minimum.It points downhill, just not at the minimum: it zigzags across the valley.
One learning rate serves all directions. Momentum, preconditioning and adaptive methods (Adam) are different ways around this.

Doubling the batch size |B||B| changes the mini-batch gradient's standard deviation by a factor of:

  1. 1/21/2That is the variance, not the standard deviation.
  2. 1/21/\sqrt2
  3. 11: the noise comes from the data, not the batch.Averaging more independent samples reduces it.
The average of |B||B| independent gradients has variance σ2/|B|\sigma^2/|B|. Twice the work per step buys only 2\sqrt2 less noise, which is one reason small batches are popular.

Practice

The gradient of a quadraticThree lines

For L(w)=12w⊤Hw−q⊤wL(w)=\tfrac12w^\top Hw-q^\top w with HH symmetric, show ∇L=Hw−q\nabla L=Hw-q, find the minimizer, and rewrite gradient descent as wt+1−w⋆=(I−αH)(wt−w⋆)w_{t+1}-w^\star=(I-\alpha H)(w_t-w^\star).

Solution

∂k12∑ijHijwiwj=12∑j(Hkj+Hjk)wj=(Hw)k\partial_k\tfrac12\sum_{ij}H_{ij}w_iw_j=\tfrac12\sum_j(H_{kj}+H_{jk})w_j=(Hw)_k by symmetry, and ∂kq⊤w=qk\partial_kq^\top w=q_k. The minimizer solves Hw⋆=qHw^\star=q. Then wt+1=wt−α(Hwt−q)=wt−αH(wt−w⋆)w_{t+1}=w_t-\alpha(Hw_t-q)=w_t-\alpha H(w_t-w^\star); subtract w⋆w^\star.

Momentum on a lineThe characteristic equation

Apply heavy ball to L(w)=λ2w2L(w)=\tfrac\lambda2w^2. Show that et=wte_t=w_t satisfies et+1=(1+β−αλ)et−βet−1e_{t+1}=(1+\beta-\alpha\lambda)e_t-\beta e_{t-1}, and find when it converges for β=0.5\beta=0.5, λ=1\lambda=1.

Solution

Substitute ∇L=λw\nabla L=\lambda w. The roots of z2−(1+β−αλ)z+β=0z^2-(1+\beta-\alpha\lambda)z+\beta=0 have product β=0.5\beta=0.5; the iteration converges iff both have modulus below 1, which (Jury's test) means |1+β−αλ|<1+β|1+\beta-\alpha\lambda|<1+\beta: 0<α<30<\alpha<3. Momentum enlarges the stable range from α<2\alpha<2 to α<2(1+β)\alpha<2(1+\beta).

InteractiveBoyd–Vandenberghe, Ch. 9: Unconstrained minimization (§§9.1–9.3, 9.5)

Convexity and convergence rates

How fast gradient descent converges, and whether it finds the best point at all, depends on the shape of the function. Three shapes cover most of what happens: strongly convex (fast and sure), convex but flat at the bottom (sure but slow), and nonconvex (fast, but maybe to the wrong place).

The idea

Convex. ff is convex if the chord lies above the graph: f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)f(\theta x+(1-\theta)y)\le\theta f(x)+(1-\theta)f(y) for θ∈[0,1]\theta\in[0,1]. For differentiable ff, equivalently, every tangent lies below: f(y)≥f(x)+∇f(x)⊤(y−x)f(y)\ge f(x)+\nabla f(x)^\top(y-x). Consequence: if ∇f(x)=0\nabla f(x)=0, then f(y)≥f(x)f(y)\ge f(x) for all yy. Every stationary point is a global minimum.
Smooth and strongly convex. Bounds on the curvature: mI⪯∇2f(x)⪯LImI\preceq\nabla^2f(x)\preceq LI everywhere, with 0<m≤L0<m\le L. LL caps how steep the slope can change (it limits the step size); m>0m>0 means the function curves up at least like m2‖x‖2\tfrac m2\|x\|^2 (strongly convex). Their ratio κ=L/m\kappa=L/m is the condition number.
Rates for gradient descent with step 1/L1/L (Boyd–Vandenberghe §9.3, and standard convex-optimization texts).
  • Strongly convex: f(xt)−f⋆≤(1−mL)t(f(x0)−f⋆)f(x_t)-f^\star\le\big(1-\tfrac mL\big)^t\big(f(x_0)-f^\star\big). Linear convergence: a fixed number of steps per extra digit, about κln⁡10\kappa\ln10.
  • Convex and LL-smooth only: f(xt)−f⋆≤L‖x0−x⋆‖22tf(x_t)-f^\star\le\dfrac{L\|x_0-x^\star\|^2}{2t}. Sublinear: ten times more steps for one more digit.
  • Nonconvex, LL-smooth: mins<t⁡‖∇f(xs)‖2≤2L(f(x0)−f⋆)t\min_{s<t}\|\nabla f(x_s)\|^2\le\dfrac{2L(f(x_0)-f^\star)}t. Gradients get small, so the iterates approach some stationary point, which can be a local minimum that is not global.
Newton's method (Boyd–Vandenberghe §9.5). Step xt+1=xt−∇2f(xt)−1∇f(xt)x_{t+1}=x_t-\nabla^2f(x_t)^{-1}\nabla f(x_t): minimize the local quadratic model exactly instead of taking a small step downhill. Near a minimum with ∇2f≻0\nabla^2f\succ0, it converges quadratically (the number of correct digits roughly doubles per step) and ignores the condition number. Its weaknesses show in the figure: where the curvature is zero it slows down, and where it is negative it heads for a maximum.

Explore

Function
Method
Drag the start along the curve. the first 40 iterates. the global minimum.
f(xt)−f⋆f(x_t)-f^\star on a log scale. A straight line sloping down is linear convergence; a curve that flattens is sublinear; a line that stops falling has stalled away from the global minimum.

Worked example

f(x)=12(x12+10x22)f(x)=\tfrac12(x_1^2+10x_2^2), so m=1m=1, L=10L=10, κ=10\kappa=10, f⋆=0f^\star=0. Gradient descent with α=1/L=0.1\alpha=1/L=0.1.

  1. What does the strongly convex rate promise?

    f(xt)≤0.9tf(x0)f(x_t)\le0.9^t f(x_0). To gain six digits, 0.9t≤10−60.9^t\le10^{-6}: t≥6ln⁡10−ln⁡0.9≈132t\ge\frac{6\ln10}{-\ln0.9}\approx132 steps.

  2. What actually happens? (Do each coordinate separately.)

    x2x_2 is multiplied by 1−0.1⋅10=01-0.1\cdot10=0: gone after one step. x1x_1 is multiplied by 0.90.9 per step, so f(xt)=120.81tx0,12f(x_t)=\tfrac12\,0.81^t\,x_{0,1}^2: six digits after about 66 steps. The bound holds, with room to spare (it assumes the worst direction for every step).

  3. Now take f(x)=x4/4f(x)=x^4/4 from x0=1x_0=1 with α=0.5\alpha=0.5. Is the convergence linear?

    xt+1=xt−0.5xt3x_{t+1}=x_t-0.5x_t^3. Near 0 the step shrinks with xt3x_t^3: from x=0.1x=0.1 the next iterate is 0.09950.0995. The curvature 3x23x^2 vanishes at the minimum, so there is no m>0m>0, and f(xt)f(x_t) decays like 1/t21/t^2 rather than geometrically: sublinear.

Check your understanding

Gradient descent on a convex function stops at a point with ∇f=0\nabla f=0. Is it the global minimum?

  1. Yes: for convex ff, f(y)≥f(x)+∇f(x)⊤(y−x)=f(x)f(y)\ge f(x)+\nabla f(x)^\top(y-x)=f(x) for every yy.
  2. Not necessarily; it could be a local minimum.That happens only without convexity.
The tangent-plane inequality is the whole reason convex problems are “easy”: there are no bad stationary points to get stuck in.

On a plot of log⁡(f(xt)−f⋆)\log(f(x_t)-f^\star) against tt, linear convergence looks like:

  1. A straight line sloping down.
  2. A curve that keeps flattening.That is sublinear convergence, like 1/t1/t.
f(xt)−f⋆≈cqtf(x_t)-f^\star\approx c\,q^t gives log⁡(f−f⋆)≈log⁡c+tlog⁡q\log(f-f^\star)\approx\log c+t\log q, a straight line with slope log⁡q<0\log q<0.

In the figure, Newton's method on the nonconvex function sometimes moves uphill. Why?

  1. Where f′′<0f''<0, the Newton step −f′/f′′-f'/f'' points uphill: it heads for the stationary point of a downward-curving quadratic, a maximum.
  2. The step size is too large.Newton's method has no step size here; the direction itself is wrong.
Newton's method finds stationary points, not minima. Practical versions check the curvature, use a line search, or regularize the Hessian.

Practice

The descent lemmaWhy the step is 1/L

If ∇2f⪯LI\nabla^2f\preceq LI, show f(y)≤f(x)+∇f(x)⊤(y−x)+L2‖y−x‖2f(y)\le f(x)+\nabla f(x)^\top(y-x)+\frac L2\|y-x\|^2, and deduce that a gradient step with α=1/L\alpha=1/L decreases ff by at least 12L‖∇f(x)‖2\frac1{2L}\|\nabla f(x)\|^2.

Solution

Taylor's theorem with remainder: f(y)=f(x)+∇f(x)⊤(y−x)+12(y−x)⊤∇2f(z)(y−x)f(y)=f(x)+\nabla f(x)^\top(y-x)+\tfrac12(y-x)^\top\nabla^2f(z)(y-x) for some zz on the segment, and the last term is at most L2‖y−x‖2\frac L2\|y-x\|^2. Put y=x−1L∇f(x)y=x-\frac1L\nabla f(x): f(y)≤f(x)−1L‖∇f‖2+12L‖∇f‖2f(y)\le f(x)-\frac1L\|\nabla f\|^2+\frac1{2L}\|\nabla f\|^2. Summing these decreases over tt steps gives the nonconvex rate.

Linear rate from strong convexityOne line more

Strong convexity implies ‖∇f(x)‖2≥2m(f(x)−f⋆)\|\nabla f(x)\|^2\ge2m(f(x)-f^\star). Combine with the previous problem to get f(xt+1)−f⋆≤(1−mL)(f(xt)−f⋆)f(x_{t+1})-f^\star\le(1-\frac mL)(f(x_t)-f^\star).

Solution

f(xt+1)−f⋆≤f(xt)−f⋆−12L‖∇f(xt)‖2≤f(xt)−f⋆−mL(f(xt)−f⋆)f(x_{t+1})-f^\star\le f(x_t)-f^\star-\frac1{2L}\|\nabla f(x_t)\|^2\le f(x_t)-f^\star-\frac mL(f(x_t)-f^\star). Iterate.

InteractiveGoodfellow–Bengio–Courville, §8.1.3 and §8.3.1

Mini-batches and stochastic gradients

The loss is an average over the whole dataset, and so is its gradient. With millions of data points, computing it exactly for every step is too expensive. Instead, pick a small random batch, average its gradients, and step. The step is now random, and that randomness is the whole story of this module.

The idea

With data (xi,yi)(x_i,y_i), i=1,…,Ni=1,\dots,N, and a per-example loss ℓi(θ)=ℓ(fθ(xi),yi)\ell_i(\theta)=\ell(f_\theta(x_i),y_i),

L(θ)=1N∑i=1Nℓi(θ),∇L(θ)=1N∑i=1N∇ℓi(θ).L(\theta)=\frac1N\sum_{i=1}^N\ell_i(\theta),\qquad\nabla L(\theta)=\frac1N\sum_{i=1}^N\nabla\ell_i(\theta).

One full gradient step costs NN per-example gradients.

Mini-batch SGD. At step tt, draw a batch BtB_t of |B||B| indices uniformly at random and step along
gt=1|B|∑i∈Bt∇ℓi(θt),θt+1=θt−αgt.g_t=\frac1{|B|}\sum_{i\in B_t}\nabla\ell_i(\theta_t),\qquad\theta_{t+1}=\theta_t-\alpha\,g_t.
Each step costs |B||B| gradients instead of NN.
Why it works, and what it costs. Where does the randomness come from? Only from which examples were picked; a different batch gives a different step. Averaged over the possible batches, 𝔼[gt]=∇L(θt)\mathbb E[g_t]=\nabla L(\theta_t): the step is right on average (unbiased). Its variance is σ2|B|\frac{\sigma^2}{|B|} when sampling with replacement, where σ2\sigma^2 is the variance of a single example's gradient (without replacement, multiply by N−|B|N−1\frac{N-|B|}{N-1}). So the noise shrinks like 1/|B|1/\sqrt{|B|} while the cost grows like |B||B|.

The consequence: with a fixed step size α\alpha, SGD does not converge to the minimizer. It reaches the neighbourhood quickly and then jitters around it, at a scale set by α\alpha and the noise. Decreasing α\alpha over time (a schedule) or increasing |B||B| shrinks the jitter.

Explore

100 data points. this step's batch and the current line y=ax+cy=ax+c.
Loss over (a,c)(a,c). full gradient descent mini-batch SGD minimizer.

Worked example

Fit fθ(x)=θxf_\theta(x)=\theta x to four points x=(1,2,3,4)x=(1,2,3,4), y=(2,3,7,8)y=(2,3,7,8) with ℓi=12(θxi−yi)2\ell_i=\tfrac12(\theta x_i-y_i)^2. Take θ=1\theta=1.

  1. Compute each ∇ℓi=(θxi−yi)xi\nabla\ell_i=(\theta x_i-y_i)x_i and the full gradient.

    Residuals θxi−yi=(−1,−1,−4,−4)\theta x_i-y_i=(-1,-1,-4,-4); gradients (−1,−2,−12,−16)(-1,-2,-12,-16). Full gradient −31/4=−7.75-31/4=-7.75.

  2. List the mini-batch gradients for all six batches of size 2.

    {1,2}:−1.5\{1,2\}:-1.5, {1,3}:−6.5\{1,3\}:-6.5, {1,4}:−8.5\{1,4\}:-8.5, {2,3}:−7\{2,3\}:-7, {2,4}:−9\{2,4\}:-9, {3,4}:−14\{3,4\}:-14. They range from −1.5-1.5 to −14-14.

  3. Average them.

    −46.5/6=−7.75-46.5/6=-7.75, the full gradient exactly. Unbiased, but any single step can be five times too short or nearly twice too long.

Check your understanding

The randomness in a mini-batch gradient comes from:

  1. Noise in the labels yiy_i.The data are fixed once collected; full-batch descent on them is deterministic.
  2. Which examples happened to be drawn into the batch.
  3. Rounding errors.Those are tiny and not the point.
Whenever a quantity is random, ask what it depends on. Here the only random input is the choice of indices, made to avoid computing all NN gradients.

You quadruple the batch size. Per step, the gradient's standard deviation:

  1. halves, and each step costs four times as much.
  2. quarters, at four times the cost.That's the variance.
Standard deviation σ/|B|\sigma/\sqrt{|B|}. Diminishing returns: past some batch size, more examples per step buy little extra accuracy per unit of compute.

In the figure, with |B|=1|B|=1 and a large step, the SGD path ends in a cloud around the minimizer rather than at it. To land closer you could:

  1. Decrease the step size over time, or use bigger batches.
  2. Run more steps with the same settings.The cloud has a fixed size for fixed α and |B|: more steps just wander inside it.
With step α\alpha and noise variance σ2/|B|\sigma^2/|B|, the stationary jitter has size of order ασ2/|B|\sqrt{\alpha\sigma^2/|B|}. Robbins–Monro step sizes (∑αt=∞\sum\alpha_t=\infty, ∑αt2<∞\sum\alpha_t^2<\infty) drive it to zero.

Practice

UnbiasednessProve it

Let BB be |B||B| indices drawn independently and uniformly from {1,…,N}\{1,\dots,N\}. Show 𝔼[g]=∇L\mathbb E[g]=\nabla L and Var(g)=σ2/|B|\mathrm{Var}(g)=\sigma^2/|B| (coordinatewise), where σ2=1N∑i|∇ℓi−∇L|2\sigma^2=\frac1N\sum_i|\nabla\ell_i-\nabla L|^2 is computed per coordinate.

Solution

Each draw II has 𝔼[∇ℓI]=1N∑i∇ℓi=∇L\mathbb E[\nabla\ell_I]=\frac1N\sum_i\nabla\ell_i=\nabla L and variance σ2\sigma^2. gg is the average of |B||B| independent copies: same mean, variance divided by |B||B|.

EpochsCounting passes

With N=50,000N=50{,}000 examples and |B|=100|B|=100, how many SGD steps make one pass over the data (an epoch)? How many full-gradient steps cost the same?

Solution

500 SGD steps per epoch; the same compute buys one full-gradient step. Early in training, 500 noisy steps make far more progress than one exact one, which is why mini-batches are standard.

SimulationGoodfellow–Bengio–Courville, §8.2 and §8.3.2

Quadratics, spectra and power laws

On a quadratic loss, gradient descent splits into independent one-dimensional problems, one for each eigenvector of the Hessian. Each shrinks at its own rate, and the slow ones, with small eigenvalues, set the pace. When the eigenvalues follow a power law, so does the loss: Lt≈t−ζL_t\approx t^{-\zeta}.

The idea

The model. L(w)=12w⊤Hw−q⊤wL(w)=\tfrac12w^\top Hw-q^\top w. Check the objects: w,qw,q are column vectors, HH is a symmetric matrix, so w⊤Hww^\top Hw and q⊤wq^\top w are numbers and LL is a number. The gradient with respect to ww (the variable we choose; HH and qq are given) is Hw−qHw-q, so the minimizer has Hw⋆=qHw^\star=q.

Where HH comes from. For a linear model with squared loss, ℓ=12(⟨w,x⟩−y)2\ell=\tfrac12(\langle w,x\rangle-y)^2 averaged over data, the Hessian is H=𝔼[xx⊤]H=\mathbb E[xx^\top]. Note the order: x⊤xx^\top x is a number (inner product), xx⊤xx^\top is a matrix (outer product). HH is the covariance of the inputs, so its eigenvalues measure how much the data vary in each direction.

Spectral decomposition. Write H=∑kλkukuk⊤H=\sum_k\lambda_ku_ku_k^\top with orthonormal eigenvectors uku_k. “Decomposing” the error vector et=wt−w⋆e_t=w_t-w^\star means writing it in this basis, et=∑kck(t)uke_t=\sum_kc_k(t)u_k. Since et+1=(I−αH)ete_{t+1}=(I-\alpha H)e_t,
ck(t)=(1−αλk)tck(0),L(wt)−L(w⋆)=12∑kλk(1−αλk)2tck(0)2.c_k(t)=(1-\alpha\lambda_k)^t\,c_k(0),\qquad L(w_t)-L(w^\star)=\tfrac12\sum_k\lambda_k(1-\alpha\lambda_k)^{2t}c_k(0)^2.
Directions with αλk\alpha\lambda_k near 1 are fixed in a step or two; directions with tiny λk\lambda_k barely move.
Power laws. Suppose the error mass in small eigen-directions follows a power law: ∑k:λk≤λλkck(0)2≈λζ\sum_{k:\lambda_k\le\lambda}\lambda_kc_k(0)^2\approx\lambda^{\zeta} for small λ\lambda, with an exponent ζ>0\zeta>0 that is a property of the problem, not something you choose. Since (1−αλ)2t≈e−2αλt(1-\alpha\lambda)^{2t}\approx e^{-2\alpha\lambda t} is near 0 when λ≫1/(αt)\lambda\gg1/(\alpha t) and near 1 when λ≪1/(αt)\lambda\ll1/(\alpha t), after tt steps only the directions with λ≲1/(αt)\lambda\lesssim1/(\alpha t) remain:
L(wt)−L(w⋆)≈12∑k:λk≲1/(αt)λkck(0)2≈(αt)−ζ.L(w_t)-L(w^\star)\approx\tfrac12\sum_{k:\lambda_k\lesssim1/(\alpha t)}\lambda_kc_k(0)^2\approx(\alpha t)^{-\zeta}.
Larger ζ\zeta means less error hides in the flat directions, and faster learning. Power-law spectra like this are what is observed for real data and kernels, which is one reason training losses often fall like a power of the step count.

Momentum and acceleration. With constant α\alpha and β\beta, heavy ball behaves like gradient descent with step α/(1−β)\alpha/(1-\beta) on the slow directions: a constant-factor gain, the same exponent ζ\zeta. Schedules that vary αt,βt\alpha_t,\beta_t with tt (built from orthogonal polynomials such as Chebyshev or Jacobi polynomials) can roughly double the exponent to t−2ζt^{-2\zeta} on deterministic problems. With mini-batch noise these aggressive schedules can amplify the noise and diverge, which is an active research question.

Polynomials and contours. After tt steps of any such method the error is et=pt(H)e0e_t=p_t(H)e_0 for a polynomial ptp_t of degree tt with pt(0)=1p_t(0)=1 (for gradient descent, pt(λ)=(1−αλ)tp_t(\lambda)=(1-\alpha\lambda)^t). So the question “how fast can a method be?” becomes “how small can such a polynomial be on the spectrum?”. Functions of a matrix can be written with Cauchy's integral formula: for a closed curve Γ\Gamma in the complex plane enclosing all the eigenvalues,

f(H)=12πi∮Γf(z)(zI−H)−1dz,f(H)=\frac1{2\pi i}\oint_\Gamma f(z)\,(zI-H)^{-1}\,dz,
which for diagonalizable HH is the scalar formula f(λ)=12πi∮Γf(z)z−λdzf(\lambda)=\frac1{2\pi i}\oint_\Gamma\frac{f(z)}{z-\lambda}dz applied to each eigenvalue. Some recent work designs optimizers by choosing the contour Γ\Gamma instead of the polynomial, and complex numbers are natural there: the per-direction iteration of a momentum method is a 2×22\times2 matrix whose eigenvalues are complex when it oscillates.

Explore

A quadratic with 20,000 eigen-directions, λk=k−ν\lambda_k=k^{-\nu}, step α=1\alpha=1. Log–log axes. gradient descent heavy ball (constant β) slope −ζ-\zeta for reference.

Worked example

H=diag(1,0.01)H=\mathrm{diag}(1,\,0.01), w⋆=0w^\star=0, w0=(1,1)w_0=(1,1), step α=1\alpha=1.

  1. Per-step factors and wtw_t.

    1−αλ1=01-\alpha\lambda_1=0 and 1−αλ2=0.991-\alpha\lambda_2=0.99: w1=(0,0.99)w_1=(0,0.99), then wt=(0,0.99t)w_t=(0,0.99^t). The first direction is solved immediately.

  2. The loss, and how many steps to reduce it by a factor of 1000.

    Lt=12⋅0.01⋅0.992tL_t=\tfrac12\cdot0.01\cdot0.99^{2t}. Need 0.992t≤10−30.99^{2t}\le10^{-3}: t≥ln⁡10002ln⁡(1/0.99)≈344t\ge\frac{\ln1000}{2\ln(1/0.99)}\approx344. All of that time is spent on the flat direction.

  3. Could a larger step help?

    Only up to α<2/λmax⁡=2\alpha<2/\lambda_{\max}=2, where the steep factor is |1−α||1-\alpha|. At α=1.98\alpha=1.98 the flat factor is 0.98020.9802, about 175 steps, but the steep direction now oscillates with factor 0.980.98. The best fixed step balances the two at α=2/1.01\alpha=2/1.01, giving κ−1κ+1≈0.980\frac{\kappa-1}{\kappa+1}\approx0.980 for κ=100\kappa=100. With thousands of directions spread over many scales, there is no balancing point, and the power law takes over.

Check your understanding

For a column vector x∈ℝdx\in\R^d, the object xx⊤xx^\top is:

  1. The number |x|2|x|^2.That is x⊤xx^\top x.
  2. A d×dd\times d matrix of rank one.
A column times a row is a matrix (outer product); a row times a column is a number (inner product). Averaging the rank-one matrices xixi⊤x_ix_i^\top over the data gives the Hessian of the squared loss for a linear model.

“Spectral decomposition of the error vector ete_t” means:

  1. Computing the eigenvalues of ete_t.A vector has no eigenvalues; the spectrum is HH's.
  2. Writing ete_t as a combination of the eigenvectors of HH.
The spectrum belongs to the matrix. Decomposing a vector along it gives coordinates ckc_k, each of which gradient descent multiplies by its own factor 1−αλk1-\alpha\lambda_k.

In the figure, raising the momentum β\beta from 0 to 0.9 shifts the heavy-ball curve but leaves its slope at about −ζ-\zeta. Why?

  1. With constant α,β\alpha,\beta, slow directions behave like gradient descent with step α/(1−β)\alpha/(1-\beta): a constant-factor speed-up, same exponent.
  2. Momentum has no effect on quadratics.It moves the curve by a factor of about ten.
On a log-log plot, multiplying time by a constant shifts the curve sideways without changing its slope. Changing the exponent needs step sizes and momenta that change with tt.

Practice

From ν and κ to ζBuild a power law

Let λk=k−ν\lambda_k=k^{-\nu} and λkck(0)2=k−1−κ\lambda_kc_k(0)^2=k^{-1-\kappa}. Show that ∑λk≤λλkck(0)2≈λκ/ν\sum_{\lambda_k\le\lambda}\lambda_kc_k(0)^2\approx\lambda^{\kappa/\nu}, so ζ=κ/ν\zeta=\kappa/\nu.

Solution

λk≤λ\lambda_k\le\lambda iff k≥λ−1/νk\ge\lambda^{-1/\nu}. The tail ∑k≥k0k−1−κ≈k0−κ/κ\sum_{k\ge k_0}k^{-1-\kappa}\approx k_0^{-\kappa}/\kappa, and k0−κ=λκ/νk_0^{-\kappa}=\lambda^{\kappa/\nu}. This is how the figure builds its problem: for given ν\nu and ζ\zeta it sets κ=ζν\kappa=\zeta\nu.

Momentum per directionComplex eigenvalues

For one eigen-direction, heavy ball is ct+1=(1+β−αλ)ct−βct−1c_{t+1}=(1+\beta-\alpha\lambda)c_t-\beta c_{t-1}. Write it as (ct+1ct)=M(ctct−1)\begin{pmatrix}c_{t+1}\\c_t\end{pmatrix}=M\begin{pmatrix}c_t\\c_{t-1}\end{pmatrix}, and find when MM's eigenvalues are complex. What is their modulus then?

Solution

M=(1+β−αλ−β10)M=\begin{pmatrix}1+\beta-\alpha\lambda&-\beta\\1&0\end{pmatrix} with characteristic polynomial μ2−(1+β−αλ)μ+β\mu^2-(1+\beta-\alpha\lambda)\mu+\beta. The roots are complex when (1+β−αλ)2<4β(1+\beta-\alpha\lambda)^2<4\beta, and then both have modulus β\sqrt\beta (their product is β\beta). In that regime the coordinate spirals in at rate β\sqrt\beta per step, whatever λ\lambda is: this is how momentum speeds up the flat directions.