Gradient descent, explained with a draggable loss curve
Gradient descent is how most machine-learning models are trained: measure how wrong the model is, work out which direction makes it less wrong, and take a small step that way. Repeat thousands of times. The size of that step, the learning rate, decides whether it converges, crawls or blows up.
On this page
The setting
A model has parameters (weights) . A loss function measures how badly the model with those parameters fits the training data: the average squared error of a regression, say, or the cross-entropy of a classifier. Training means finding that makes as small as possible. Real models have millions or billions of parameters, so we can’t search exhaustively, but we can compute the slope.
The update rule
The derivative is the slope of the loss at . If it is positive, the loss increases to the right, so we should move left; if negative, move right. In both cases we step against the slope:
The constant (eta) is the learning rate. With many parameters the derivative becomes the gradient , the vector of partial derivatives, which points in the direction of steepest increase, and the rule is the same: . Near the minimum the slope shrinks, so the steps shrink automatically, even though is fixed.
Worked example: from with
The derivative is , and the minimum is at . Each step computes the slope, then moves.
| t | wₜ | L(wₜ) | L′(wₜ) | Update |
|---|---|---|---|---|
| 0 | 0.0000 | 9.0000 | −6.0000 | 0.0000 − 0.1 × (−6.0000) = 0.6000 |
| 1 | 0.6000 | 5.7600 | −4.8000 | 0.6000 − 0.1 × (−4.8000) = 1.0800 |
| 2 | 1.0800 | 3.6864 | −3.8400 | 1.0800 − 0.1 × (−3.8400) = 1.4640 |
| 3 | 1.4640 | 2.3593 | −3.0720 | 1.4640 − 0.1 × (−3.0720) = 1.7712 |
| 4 | 1.7712 | 1.5099 | −2.4576 | 1.7712 − 0.1 × (−2.4576) = 2.0170 |
Every step closes 20% of the remaining gap to 3. In fact , so : after 20 steps is within 0.04 of the minimum.
Try it
Drag the orange starting point anywhere on the curve, set the learning rate, and step through. The pink line is the tangent whose slope is being used. On the bowl, try the four learning-rate buttons; on the “two valleys” curve, notice that where you start decides which valley you end up in.
Gradient descent on a loss curveDrag the orange dot to choose where to start. Change η and watch the path.
Pseudocode
- w ← w₀
- for t = 1, 2, …:
- g ← L′(w) (the slope at w)
- w ← w − η · g (step downhill)
- stop when |g| is tiny, or w blows up
Now
Recent iterations
| t | w | L(w) | L′(w) |
|---|
When it converges and when it diverges
The worked example generalises. For and any learning rate,
So everything depends on the factor :
- : the factor is in , and slides steadily towards 3 from one side. At exactly it lands on 3 in one step.
- : the factor is negative but smaller than 1 in size, so overshoots, landing on alternate sides of 3, but still converges.
- : the factor is , and bounces between 0 and 6 forever.
- : each overshoot is bigger than the last, and diverges.
For a general smooth loss the same picture holds near a minimum, with the curvature in place of 2: steps are stable when . A sharply curved valley needs a small learning rate; a flat one tolerates a large one. That is why the shallow bowl preset needs in the tens before it misbehaves.
NaN, the learning rate is almost certainly too high. If it falls but painfully slowly, it is probably too low. In practice people try values spaced by factors of about 3 or 10 (0.1, 0.03, 0.01, …) and keep the largest that trains stably.
Local minima and saddle points
Gradient descent only ever sees the local slope, so on a non-convex loss it finds a minimum, not necessarily the lowest one. On the two-valleys curve, starting on the right with a small learning rate gets stuck in the shallower right-hand valley, while starting on the left finds the deeper one. With a larger step the run can jump between valleys, which hints at why noisy updates sometimes help. In very high dimensions, poor local minima turn out to matter less than saddle points and flat regions, where the gradient is small in every direction and progress stalls.
What practitioners actually run
- Stochastic and mini-batch gradient descent. Computing the exact gradient over a whole dataset is expensive, so each step estimates it from a small random batch of examples. The estimate is noisy but cheap, and many cheap noisy steps beat a few exact ones.
- Momentum. Keep a running average of recent gradients and step along it. Oscillations across a narrow valley cancel out, while consistent movement along the valley builds up speed.
- Adaptive methods. Adam scales each parameter’s step by running estimates of the gradient’s first and second moments, so parameters with small, consistent gradients still move. It is the default optimiser for training many neural networks.
- Learning-rate schedules. Start with a short warm-up, then decay the rate over training, so early steps explore and late steps settle.
References
- S. Ruder, “An overview of gradient descent optimization algorithms”, 2016. arXiv:1609.04747
- D. P. Kingma and J. Ba, “Adam: a method for stochastic optimization”, ICLR 2015. arXiv:1412.6980
- I. Goodfellow, Y. Bengio and A. Courville, Deep Learning, MIT Press, 2016. Chapters 4 and 8.
- 3Blue1Brown, Neural networks, a visual series whose second chapter covers gradient descent.