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

Gradient descent

The algorithm behind almost all of machine learning, analyzed exactly: why the learning rate has a hard ceiling, why ill-conditioning forces zig-zags, and where local minima enter the story.

Gradient descent is three lines of code and a lifetime of behaviour. This lesson does what most treatments skip: it solves the algorithm’s dynamics exactly on the one problem where that is possible — the quadratic — and shows that everything you observe on real losses (divergence past a learning-rate threshold, zig-zagging across ravines, the slow crawl along flat directions) is already present, and provable, there.

The update, and where it comes from

Around the current iterate xtx_t, first-order Taylor says f(xt+δ)f(xt)+f(xt)δf(x_t + \delta) \approx f(x_t) + \nabla f(x_t)^\top \delta. Among all steps of a fixed small length, the one that decreases this linear model fastest is δf(xt)\delta \propto -\nabla f(x_t): the negative gradient is the direction of steepest descent. Committing to it with a step size (learning rate) η\eta:

xt+1  =  xtηf(xt).x_{t+1} \;=\; x_t - \eta\, \nabla f(x_t).

For an LL-smooth function, the smoothness upper bound from the previous lesson turns this from a heuristic into a guarantee. Plugging the step into the quadratic upper bound gives the descent lemma:

f(xt+1)    f(xt)η(1Lη2)f(xt)2,f(x_{t+1}) \;\le\; f(x_t) - \eta\Big(1 - \tfrac{L\eta}{2}\Big)\lVert \nabla f(x_t) \rVert^2 ,

which is strictly decreasing whenever 0<η<2/L0 < \eta < 2/L. There is the learning-rate ceiling, derived rather than tuned: step sizes are bounded by the sharpest curvature of the loss.

def gradient_descent(grad, x0, lr, steps):
    x = x0.copy()
    for _ in range(steps):
        x -= lr * grad(x)        # the entire algorithm
    return x

Exact dynamics on a quadratic

Take f(x)=12xHxf(x) = \tfrac{1}{2} x^\top H x with symmetric H0H \succ 0 (any smooth loss looks like this near a minimum — this is not a toy, it is the local model of every basin). The gradient is HxHx, so the update is linear:

xt+1=(IηH)xtxt=(IηH)tx0.x_{t+1} = (I - \eta H)\, x_t \qquad\Longrightarrow\qquad x_t = (I - \eta H)^t x_0 .

Diagonalize H=QΛQH = Q \Lambda Q^\top and write the error in the eigenbasis, zt=Qxtz_t = Q^\top x_t. The coordinates decouple — each curvature direction evolves independently:

zt+1(i)=(1ηλi)zt(i).z_{t+1}^{(i)} = \big(1 - \eta \lambda_i\big)\, z_t^{(i)} .

Every question about gradient descent on this problem reduces to the scalar factor 1ηλi\lvert 1 - \eta\lambda_i \rvert:

  • Convergence requires 1ηλi<1\lvert 1 - \eta\lambda_i\rvert < 1 for all ii, i.e. η<2/λmax\eta < 2/\lambda_{\max}. Exceed it and the stiffest direction oscillates with growing amplitude — the characteristic explode-in-one-axis divergence you can reproduce in the figure below.
  • Speed is set by the slowest factor. With λ\lambda ranging over [μ,L][\mu, L], the best constant step balances the two extremes:
η=2μ+L,rate  =  maxi1ηλi  =  κ1κ+1,κ=Lμ.\eta^\star = \frac{2}{\mu + L}, \qquad \text{rate} \;=\; \max_i \lvert 1 - \eta^\star \lambda_i \rvert \;=\; \frac{\kappa - 1}{\kappa + 1}, \qquad \kappa = \frac{L}{\mu}.

For κ=100\kappa = 100 the rate is 0.980.98: each iteration removes 2% of the remaining error, and you need roughly κln(1/ε)\kappa \ln(1/\varepsilon) iterations — iteration count scales linearly with the condition number. That sentence is why feature standardization, batch normalization and second-order-ish methods exist.

The zig-zag, explained. In a ravine (κ1\kappa \gg 1), any η\eta large enough to make progress along the flat direction is close to the stability limit of the steep one, so the steep coordinate’s factor 1ηL1 - \eta L is close to 1-1: it overshoots and flips sign every step. The trajectory bounces wall-to-wall across the ravine while creeping along it. You are watching the two eigenvalues disagree about the learning rate.

The contour map is the loss surface (darker = lower); dots mark the global minimum, local minimum and saddle. The side panel plots loss per iteration on a log scale — linear convergence appears as a straight line, and its slope is exactly the rate (κ1)/(κ+1)(\kappa-1)/(\kappa+1) derived above.

Three experiments worth actually doing before reading on:

  1. Find the cliff. Quadratic, κ=10\kappa = 10. The theory says stability ends at η=2/L=0.2\eta = 2/L = 0.2 (here L=κL = \kappa after scaling: watch for the readout’s DIVERGED). Sweep η\eta and locate the cliff — it is sharp, not gradual.
  2. Feel the κ tax. Fix η\eta slightly below the cliff and raise κ\kappa from 1 to 60. The loss line on the sparkline tilts toward horizontal: same algorithm, same code, 60× the iterations.
  3. Meet your first local minimum. Switch to the two-wells surface and drag the start around x0.04x \approx 0.04 (the saddle). A few pixels left: the deep well. A few pixels right: the shallow one, and the loss curve flatlines at a higher value — gradient descent has converged, and it is not the answer. Which minimum you get is a property of the initialization, not the optimizer.

Beyond quadratics: what survives

On a general convex, LL-smooth ff with η=1/L\eta = 1/L, telescoping the descent lemma against the first-order convexity bound yields

f(xt)f    Lx0x22t— the O(1/t) rate,f(x_t) - f^\star \;\le\; \frac{L \lVert x_0 - x^\star \rVert^2}{2t} \qquad \text{— the } O(1/t) \text{ rate,}

and with μ\mu-strong convexity the geometric rate returns: xtx2(1μL)tx0x2\lVert x_t - x^\star \rVert^2 \le \big(1 - \tfrac{\mu}{L}\big)^t \lVert x_0 - x^\star \rVert^2. The quadratic analysis was not a special case after all — near any strict minimum it is the general case, with H=2f(x)H = \nabla^2 f(x^\star).

On non-convex ff, the descent lemma still guarantees minstf(xs)22L(f(x0)f)t\min_{s \le t} \lVert \nabla f(x_s) \rVert^2 \le \frac{2L\,(f(x_0) - f^\star)}{t} — you always find an (approximately) stationary point. What kind of stationary point, the lemma cannot say; that question owns the non-convex landscapes lesson.

Continuous-time picture

As η0\eta \to 0, gradient descent traces the ODE x˙=f(x)\dot{x} = -\nabla f(x) — water flowing downhill on the surface. This “gradient flow” view is more than a metaphor: finite η\eta discretizes the flow, and the divergence at η>2/L\eta > 2/L is precisely a too-coarse Euler discretization of a stiff ODE going unstable. Optimization inherits 50 years of numerical-ODE intuition through this door, including momentum-as-damped-oscillator in the momentum lesson.

Exercises

Work these before the next lesson

  1. Derive η=2/(μ+L)\eta^\star = 2/(\mu + L): minimize max{1ημ,1ηL}\max\{\lvert 1 - \eta\mu \rvert, \lvert 1 - \eta L \rvert\} over η\eta and show the optimum equalizes the two terms. Then verify the resulting rate is (κ1)/(κ+1)(\kappa - 1)/(\kappa + 1).
    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

  • Y. Nesterov, Lectures on Convex Optimization, 2nd ed., 2018 — §2.1 for the rates quoted here.
  • G. Goh, “Why Momentum Really Works”, Distill, 2017 — the eigenbasis decoupling used above, animated; also the bridge to the momentum lesson.
  • S. Boyd & L. Vandenberghe, Convex Optimization, §9.2–9.3 — exact line search and the classical zig-zag analysis.