Paul’s learnings

Learnings › Neural networks › Gradient descent

Gradient descent: how a network walks downhill

A network learns by adjusting its weights so that it makes smaller mistakes. Gradient descent is the rule that says which way to adjust them and by how much. Almost every network trained since the 1980s has learned this way.

Millions of dials, one score

A network's behaviour is set by its weights: plain numbers, from a few dozen in a toy model to hundreds of billions in a large language model. A loss function turns the network's mistakes on the training data into a single number. Training means finding weights that make that number small.

Searching by trial and error is hopeless. Try just ten values for each of a million weights and there are 101,000,000 combinations. Gradient descent never looks at the whole landscape. It only asks which way is downhill from where it stands right now.

Picture standing on a hillside in thick fog. You cannot see the valley, but you can feel which way the ground slopes under your feet. Take a step that way, feel again, step again. The height is the loss; your position is the weights.

Slope, derivative, gradient

With a single weight w, the slope is the derivative: how much the loss changes for a tiny nudge in w. You can measure it by nudging both ways:

dLdw ≈ L(w+h)−L(w−h) 2h

This is how the tests for this guide check every derivative the pages use.

If the derivative is positive, increasing w increases the loss, so you should decrease w. If it is negative, increase it. Either way, you move against the sign of the slope.

With many weights, collect one slope per weight into a vector. That vector is the gradient, written ∇L:

∇L= ( ∂L∂w1 , ∂L∂w2 ,…, ∂L∂wn )

The gradient points in the direction in which the loss rises fastest, so its negative points straight downhill. Working out all those slopes cheaply, even for billions of weights, is the job of backpropagation, the next chapter.

The update rule

Gradient descent repeats one line:

wt+1 = wt −η∇L(wt)

η (eta) is the learning rate: how big a step to take. It is the most important number in training, and the easiest to get wrong. Too small and training crawls. Too large and each step overshoots the valley floor, lands higher on the other side, and the next step overshoots further.

How big can a step be?

For a loss shaped like a simple bowl you can work out the exact limit. Take L(w)=12λw2, where λ measures how sharply the bowl curves. The gradient is λw, so one step gives:

wt+1 =wt−ηλwt =(1−ηλ)wt

Every step multiplies the weight by the same factor, 1−ηλ. The weight shrinks towards the minimum at 0 only while that factor lies between −1 and 1:

|1−ηλ|<1 ⟺ 0<η<2λ

Between 1λ and 2λ the factor is negative, so the weight flips sign on every step: it zig-zags but still converges. Past 2λ each zig-zag is bigger than the last, and the weights blow up.

Real losses curve differently in different directions, and the sharpest direction sets the limit. The first landscape below curves ten times more steeply in y (λ=10) than in x (λ=1), so plain gradient descent only survives for η<0.2. Press Stability to run η = 0.19 and η = 0.21 side by side.

Even at the safe 0.19, the gentle x direction shrinks by only 0.81 per step. Steep directions force small steps, and then flat directions take forever. Almost every improvement in the rest of this chapter is a way round that.

Race six optimisers across a loss landscape

Race all six runs every optimiser from the same start, each with a learning rate chosen for that landscape. Run one uses your own settings. Click or tap the map to move the start and run again.

start (−2.50, 2.50)

Contour map of the loss landscape with each optimiser's path. The results table lists where each path ends.
Darker is lower loss. White ring: the minimum. Black-ringed dot: the start.
Loss above the true minimum against step, on a log scale.
Loss above the true minimum against step, on a log scale. A line that drops off the bottom has reached the minimum.

OptimiserηLoss above minimumEnds atOutcome

What the race shows

No single learning rate suits every optimiser, so each gets its own, picked by a grid search on each landscape for a 300-step run. Momentum's rates come out about ten times smaller than SGD's: with μ = 0.9 its velocity builds up to about η / (1 − μ), ten times a single step.

Elongated bowl. All six reach the minimum. SGD's rate, 0.15, sits between 1/λ and 2/λ for the steep direction, so its path zig-zags across the bowl on the way down.

Rosenbrock valley. The minimum at (1, 1) lies at the bottom of a long, curved valley. SGD and momentum end closest to it. Adam and AdamW end on the valley floor but still short of the minimum. RMSProp ends further away, and AdaGrad makes little progress: it divides each step by the running total of every squared gradient it has seen, so a few steep gradients early on shrink all its later steps. Near (1, 1) the valley curves about 2,500 times more sharply across than along, which puts SGD's 0.002 almost exactly on its stability limit. At 0.005 it blows up.

Tilted double well. The start is on the slope of a shallow well; a deeper one lies on the far side of a hump. Only momentum carries enough speed over the hump to reach the deeper well. Every other optimiser ends in the shallow one, where the gradient is zero, so nothing pushes it further.

These are two-dimensional toys, chosen so you can see a path. The learning rates that work here say nothing about the right ones for a real network: see what these pictures leave out.

Where it came from

  1. 1847Augustin-Louis Cauchy describes the method of steepest descent, to solve the systems of equations that arise in astronomy.
  2. 1944Haskell Curry analyses steepest descent for nonlinear problems.
  3. 1951Herbert Robbins and Sutton Monro prove that noisy steps still converge if the step size shrinks at the right rate: the theory under stochastic gradient descent.
  4. 1960Bernard Widrow and Ted Hoff train ADALINE with the least-mean-squares rule, stochastic gradient descent built in hardware.
  5. 1964Boris Polyak adds momentum: the “heavy ball” method.
  6. 1983Yurii Nesterov's accelerated gradient reaches the best possible rate for smooth convex problems.
  7. 1986Rumelhart, Hinton and Williams train hidden layers with backpropagation and gradient descent with momentum.
  8. 2011Duchi, Hazan and Singer's AdaGrad gives each weight its own step size.
  9. 2012Geoffrey Hinton presents RMSProp in a Coursera lecture. It was never written up as a paper.
  10. 2014Diederik Kingma and Jimmy Ba publish Adam.
  11. 2017Ilya Loshchilov and Frank Hutter separate weight decay from Adam's update: AdamW.

Stochastic gradient descent

The true gradient averages over every example in the training set. With millions of examples, computing it for every step is far too slow. Stochastic gradient descent estimates it from a small random batch and steps on that estimate. Each step is noisy, but right on average.

Robbins and Monro showed in 1951 that this still converges, provided the step sizes shrink neither too fast nor too slowly:

∑t=1∞ ηt=∞ ∑t=1∞ ηt2<∞

The first condition lets the weights travel as far as they need to; the second makes the noise average out. Modern schedules rarely meet these conditions exactly, but they keep the lesson: lower the learning rate as training goes on. The noise may also help. Keskar and colleagues found in 2017 that very large batches, which are less noisy, tend to land in sharper minima that generalise worse, though that explanation is still debated.

The race above uses exact gradients, so its “SGD” is plain full-batch gradient descent.

Momentum

Plain gradient descent has no memory. In a narrow valley it bounces from wall to wall instead of running along the floor. Polyak's 1964 fix gives the weights a velocity, like a heavy ball rolling downhill:

vt+1= μvt−η∇L(wt) wt+1= wt+vt+1

With μ = 0.9, pushes that keep pointing the same way add up to about ten times a single step, while pushes that alternate across the valley cancel out. The built-up speed also carries the ball over small bumps, which is how momentum reaches the deeper well in the race. Nesterov's 1983 variant measures the gradient at the point the velocity is about to carry you to, and reaches the best possible convergence rate for smooth convex problems.

A step size for every weight

Some weights see large gradients on every step; others see small or rare ones. AdaGrad (2011) gives each weight its own step size by dividing by the square root of the sum of all its past squared gradients gt:

st=st−1+gt2 wt+1=wt− ηst+εgt

That helps on sparse problems such as text, where rare words deserve big updates. But the sum only grows, so every step shrinks forever, which is why AdaGrad stalls in the Rosenbrock valley. RMSProp (Hinton, 2012) replaces the sum with a decaying average that forgets old gradients:

st=ρst−1+(1−ρ)gt2

Adam (Kingma and Ba, 2014) keeps two decaying averages: one of the gradient, as momentum does, and one of its square, as RMSProp does. It corrects both for starting at zero. (Here v is the average of squared gradients, not momentum's velocity.)

mt=β1mt−1+(1−β1)gt vt=β2vt−1+(1−β2)gt2 wt+1=wt−η mt/(1−β1t) vt/(1−β2t)+ε

Its defaults (β1 = 0.9, β2 = 0.999, ε = 10−8) work across a very wide range of problems, which is a large part of why Adam became the default choice.

AdamW: weight decay done properly

Weight decay shrinks every weight a little on each step, which discourages a network from leaning on a few huge weights. With plain SGD, adding an L2 penalty λ2‖w‖2 to the loss and shrinking the weights directly are the same thing. Loshchilov and Hutter pointed out that inside Adam they are not: the penalty's gradient is divided by Adam's per-weight scaling too, so weights with large gradients barely decay. AdamW applies the decay outside the adaptive step:

wt+1= wt−η·(Adam step)−ηλwt

Here λ is the decay rate, not a curvature.

In the race, Adam and AdamW trace almost the same paths. The difference shows when the gradient is zero: Adam stops, while AdamW keeps shrinking the weights by a factor of 1−ηλ per step. This guide's tests check exactly that.

What these pictures leave out

  • Real networks have millions of dimensions, not two. Dauphin and colleagues argued in 2014 that most flat points there are saddle points, which slow training down, rather than bad local minima that trap it.
  • With mini-batches, the landscape itself changes slightly on every step.
  • The learning rates in the race were tuned for three toy surfaces and 300 steps. A real network needs its own, usually found with short trial runs.

What people use now

Transformers are usually trained with AdamW, a short warm-up during which the learning rate rises from near zero (the original Transformer used 4,000 warm-up steps), and then a slow decay, often along a cosine curve. SGD with momentum is still competitive for many image models. New optimisers keep arriving. Lion (2023), found by an automated search, uses only the sign of a momentum-like update. Muon (2024) orthogonalises the update for each weight matrix and has been used to train large language models.

Try it in the workbench

The workbench's MLP trainer trains a small network on 2-D data with the optimiser you choose, and redraws its decision boundary as it learns.

Sources