← Algorithms & ML

How Gradient Descent Finds Minima

Training a model means making its loss small. Gradient descent does it by repeatedly stepping downhill, and the step size and choice of optimizer shape the path it takes.

Imagine standing blindfolded on hilly ground, trying to reach the lowest point. You can feel the slope under your feet, so you take a small step downhill, feel again, and repeat. That is gradient descent.

In machine learning the ground is a loss surface: a function that measures how wrong your model is, over axes that are the model's parameters. Training means finding parameters where the loss is as small as possible.

The update rule. At each step, we nudge the parameters a small distance η against the loss gradient.

The Loss Landscape

With two parameters, a loss surface reads like a topographic map, with contour lines joining points of equal loss. Where a walker starts matters. Some reach the global minimum; others settle into a local minimum with a deeper valley past the ridge.

0 walkers
Click or tap the contour map to drop walkers, or press Random start
Figure 1. A 2D loss surface with multiple minima. Each walker follows the negative gradient from its starting point. Color encodes loss (viridis: bright = low, dark = high).

Each walker stops where the ground is flat. The update rule sees only the local slope, so it cannot tell a local minimum from the global one.

How Far to Step

The gradient gives the direction of a step but not its length. That length is the learning rate η. Too small and the walk takes hundreds of steps; too large and it overshoots, bouncing across the valley or flying off.

Small rate Medium rate Large rate
0.020
0.500
2.500
Figure 2. Gradient descent for 80 steps on one loss curve, a learning rate per panel; the words above are read off each run. At the defaults the small rate is still creeping (it needs about 300 steps), the medium settles in 10, the large bounces forever.

How large is too large depends on how sharply the loss bends. Near a minimum the loss is a bowl, and along each of its principal directions a step multiplies the distance to the bottom by 1 − ηλ, with λ the curvature there. That shrinks the distance only while η < 2/λ, so the steepest direction sets the limit. Figure 2's curve has curvature 1 at the bottom and about half that up its walls, so rates between 2 and 4 bounce forever: unstable at the bottom, stable up the walls.

20
1.0
rates one parameter alone could take
same data, centred
edge found by descent alone
Figure 3. Gradient descent on a least-squares loss with 20 parameters (synthetic data), 100 steps at each rate. The loss blows up past 2/λmax, with λmax the largest Hessian eigenvalue computed in the page; the triangle marks where descent alone finds it.

The inputs in Figure 3 share an offset of 1, which makes one direction very steep: all parameters moving together. Each parameter alone could take a rate above 0.4; together they diverge past 0.046. Centring the inputs raises the limit about tenfold.

Momentum and Adam

In a long, narrow valley the steep walls cap the learning rate, and at that rate progress along the shallow floor is slow. Momentum keeps a fraction β of the previous step as a velocity: across the valley the gradient flips sign and the pushes partly cancel, along the floor they add up. Adam steps each parameter by a running average of its gradient divided by a running root-mean-square of it, so every parameter moves at a pace set by the learning rate rather than by its own steepness.

Momentum carries a running velocity v through the gradient field; β sets how much of the previous step to keep.
Gradient descent (rate 0.158)
Momentum (rate 0.158, β = 0.5)
Adam (rate 0.25)
Click or tap the map (or focus it and use the arrow keys) to set the starting point, or use Race to compare all three
Figure 4. Three optimizers, 100 steps each from the open circle, on a tilted quadratic valley with its minimum at the dark cross. Gradient descent swings between the walls and ends far down the valley; momentum's swing dies within about ten steps. Adam loops: its per-parameter scaling ignores the tilt.

How much momentum buys depends on the valley's condition number κ, its steepest curvature over its shallowest. At its best rate gradient descent shrinks the error by (κ − 1)/(κ + 1) per step, so its step count grows like κ. Tuned momentum shrinks it by (√κ − 1)/(√κ + 1), and its count grows like √κ.

120
Gradient descent, η = 2/(λmax + λmin)
Momentum, tuned η and β
rate model
Figure 5. Left (top on a phone): distance to the minimum by step on a quadratic with condition number κ (default 120, Figure 4's valley). Right: steps to shrink it a millionfold, measured (dots) and from the per-step rate (lines). Momentum's error decays like t rt, hence the gap.

Learning from Mini-Batches

Everything above computes the gradient from the entire dataset. Real datasets are large, so in practice it is estimated from a small random subset, a mini-batch, which makes the method stochastic gradient descent (SGD). Figure 6 fits a line, ŷ = w x + b, to 64 simulated data points, so the parameters are the slope w and the intercept b.

Mean squared error over a mini-batch B: the loss compares each true value against the model's prediction.
1 / 64
-
Loss contours
Walker path (w, b)
Least-squares minimum
Figure 6. Mean squared error over (w, b) for 64 simulated points around w = 0.7, b = 1.2; the green dot is the least-squares minimum. The walker takes 50 steps at rate 0.1, each on a fresh mini-batch: zigzagging at batch 1, straight in at 64.

Near the minimum the noisy steps never settle. The walker keeps wandering at a distance set by the batch size and the learning rate, and quadrupling the batch only about halves it.

0.10
simulated SGD
exact steady-state model
model, batches drawn with replacement
Figure 7. How far SGD on Figure 6's line fit wanders from the minimum once settled: root-mean-square distance over 40 seeded runs per batch size (dots), against the exact steady-state model (line). Batches drawn without replacement stop wandering at 64, when each is the whole dataset.

The noise can also help, by bouncing the optimizer out of a shallow local minimum. Keskar et al. (2017) found that small-batch training tends to land in flatter minima that generalize better. Whether flatness explains it is debated: Dinh et al. (2017) showed that a network can be reparametrized to make a minimum arbitrarily sharp without changing what it computes.

Every step above needs the gradient with respect to each parameter. For the line in Figure 6 that is two partial derivatives. A neural network has many layers of weights, and Part 2 shows how backpropagation computes the gradient for all of them.