Gradient descent
How to use the gradient to drive the score down, iteration after iteration.
In chapter 8, backpropagation handed us, in a single backward pass, the slope of the score with respect to every weight of the network. We hold the full compass. But a compass never moved anyone: knowing where to descend is not descending.
The question of this chapter is therefore very concrete: how do we turn this gradient into a step, and how long should that step be? Too short, and we never arrive. Too long, and we leap over the valley, and may even end up higher than where we started. We will see that this intuition can be proven, and that it yields a numerical rule.
Act 1: the hiker in the fog
Picture yourself in the mountains, caught in thick fog. You want to reach the valley, but you cannot see two metres ahead. Only one piece of information is within reach: the slope of the ground under your feet. The sensible strategy is obvious. You feel which way the terrain drops most steeply, you take a step that way, and you start again.
That is exactly what gradient descent does, already announced in chapter 4. Chapter 7 established that the gradient points towards the steepest rise of the score. To go down, we therefore step in the opposite direction, . The update rule reads:
It reads: the new weight vector is the old one, minus the gradient of the score at that point, multiplied by a small positive number . This number is the learning rate from chapter 4. It sets the length of the step. Weight by weight, the same rule is written
and backpropagation supplies precisely each of these partial derivatives. One descent step is therefore: a forward pass, a backward pass, then this subtraction on every weight. We repeat, and that is all.
You have already met a rule of this form. The perceptron rule, in chapter 4, was . Same structure, a weight corrected by a step proportional to . The difference runs deep: the perceptron corrected by projection, with no slope, whereas here every correction is dictated by the derivative of the score. That is what makes it possible to train a network with differentiable activations, hidden layer included.
Act 2: the right dose of , proven on a parabola
To understand the role of with nothing hidden, let us take the simplest score that has a dip: a parabola with a single weight,
Its minimum is at , and its derivative is . The number measures the curvature: the larger it is, the narrower and steeper the bowl.
One descent step. We apply the rule:
We factor out :
This line says it all. At each step, the weight is multiplied by the same number . After steps, it is therefore
This is a geometric sequence, and you already know how it behaves: it tends to if and only if . We translate this condition into a condition on . The inequality is equivalent to , that is,
The sign of refines the behaviour. We get five regimes, all exact:
- : . The weight glides towards without ever changing sign. A well-behaved descent, and the slower the smaller is.
- : . The first step lands exactly at the bottom. This is the ideal step for this bowl.
- : . The weight changes sign at every step: it jumps from one slope to the other, but each jump is shorter. It oscillates, and still converges.
- : . The weight bounces forever between and .
- : . Each jump is longer than the previous one. The score blows up: the descent diverges.
Above all, keep the lesson that runs deep: the threshold depends on the curvature . A narrow bowl demands a small step. The same can be cautious on one score and catastrophic on another.
Set in the lab and take steps. Start around , then cross , then .
Gradient descent lab
Three things to watch as you play:
- Between and , the trajectory zigzags from one slope to the other, but each bounce is lower than the previous one. Oscillating is not diverging.
- At exactly , a single step is enough. On a real score with millions of weights, this perfect step does not exist, because the curvature changes from one direction to another.
- Beyond , the score climbs at every step. A learning rate that is too large does not slow learning down: it destroys it.
Act 3: the descent lemma, for any smooth score
The parabola is a textbook case. A real network has a tormented score, with no simple formula. Can we still guarantee that a step lowers the score? Yes, on one condition: the slope must not change too abruptly.
Back to the hiker. If they know the slope of the terrain cannot change all at once, that the relief has no cliff or hidden step, then the slope measured under their feet stays reliable over a certain distance. They can take a step with confidence, provided that step is no longer than the zone where the slope stays reliable.
Definition. A differentiable function is called β-smooth β-smooth function A differentiable function whose slope never changes faster than a constant β: the gap between two slopes is at most β times the gap between the two points. This regularity guarantees that a gradient descent step of length at most 1/β always decreases the function (descent lemma). Source: Nesterov, 2004 , with , if its derivative never varies faster than :
We write the constant and not , so as not to confuse it with the score. On the parabola , we have : it is -smooth with .
Descent lemma. If is -smooth, then for all and ,
It reads: the function always stays below a parabola that touches its graph at , with the same slope. In other words, the tangent plus a curvature margin form a ceiling that the score can never break through.
Proof. We fix and study the gap between this ceiling and the function:
We must show that everywhere. First, at , all the terms vanish: . Next, we differentiate with respect to :
For , smoothness gives , hence : increases to the right of . For , it gives , hence : decreases to the left of . A function that decreases up to and then increases reaches its minimum at . This minimum equals , so for every . □
Consequence for a descent step. We apply the lemma to the step . The gap equals , so
We group the last two terms:
If , the parenthesis is at least , and we get the announced guarantee:
It reads: with a step at most equal to , each iteration lowers the score by at least half of times the square of the slope. As long as the slope is not zero, the score goes down, without exception.
Check the consistency with act 2. On the parabola, , so the guarantee holds for : this is exactly the zone of well-behaved descent, with no oscillation. More generally, the parenthesis stays positive as long as : this is exactly the convergence threshold found by hand. The lemma recovers the parabola, and it applies to many other scores.
What the lemma guarantees, and what it does not. Add up the guarantee over steps. The terms telescope, and since the score never goes below its lowest level (a squared error is never negative, so ), what remains is
The right-hand side is a fixed number, whereas the sum on the left grows at every step. The gradients encountered must therefore become as small as we like: the descent ends up stopping at a point where the slope is zero. But be careful about what this result says. A zero slope is not necessarily the bottom of the valley. It can be a secondary dip, a pass, a flat spot. Act 5 will go through them.
Act 4: batch, mini-batch, stochastic
So far, we have acted as if the score were a single function. In reality, it is an average over the training examples:
where is the score of example alone. Its gradient is the average of the individual gradients, and each one costs a forward pass and a backward pass. With images, a single descent step would therefore require backpropagations. This is full-batch gradient descent: exact, but terribly slow.
The idea that unlocks everything is not to wait until we have seen everything. We randomly draw a small batch of examples, a mini-batch Mini-batch A small random subset of the training set on which the gradient is computed to take one descent step. The resulting gradient is noisy but correct on average, and far cheaper than the gradient over the whole set. Typical sizes range from a few dozen to a few hundred examples. Source: Bottou, Curtis & Nocedal, 2018 of size , and we estimate the gradient on that batch alone:
This estimate is noisy: a batch of images never looks exactly like the whole dataset. But it is right on average. Let us check this in the extreme case . If we draw example with probability , the average of over all possible draws is
The same computation, carried out on each example of a randomly drawn batch, gives the same result for any . We say that is an unbiased estimator of the gradient: it is wrong at every draw, but its errors cancel out. The case is called stochastic gradient descent Stochastic gradient descent A variant of gradient descent that estimates the gradient on a single randomly drawn example (or, by extension, on a mini-batch) instead of the whole set. The estimate is noisy but unbiased: averaged over draws, it equals the true gradient. Often abbreviated SGD. Source: Robbins & Monro, 1951 , and the idea of moving forward with noisy measurements goes back to Robbins and Monro (1951). In practice, we take batches of a few dozen to a few hundred examples: enough to smooth the noise, few enough to take many steps.
A full pass over the data, batch after batch, is called an epoch Epoch One complete pass over the whole training set, mini-batch after mini-batch. With N examples and batches of size m, an epoch contains N/m descent steps. Training a network is often measured in number of epochs. . With examples and batches of , an epoch counts steps, where full batch makes only one. The noise even has an unexpected advantage: it shakes the trajectory, which can help it leave a flat spot or a pass (Bottou, Curtis and Nocedal, 2018).
| Variant | Examples per step | Steps per epoch () | Gradient |
|---|---|---|---|
| Full batch | exact, costly | ||
| Mini-batch | (e.g. ) | noisy, unbiased | |
| Stochastic | very noisy, unbiased |
Act 5: the traps of the terrain
The lemma guarantees that we stop where the slope vanishes. Here are the places where that can happen without our being at the bottom.
Local minima. A local minimum Local minimum (optimisation) A point where a function is lower than everywhere in its immediate neighbourhood, without being its lowest value overall. The slope there is zero, so gradient descent may stop at it. A convex function has no local minimum other than its global one. Source: Boyd & Vandenberghe, 2004 is a dip lower than all its close neighbours, but higher than the true bottom. The hiker who falls into one sees only climbs around and stops. There is a family of scores without this trap: convex functions, shaped like a bowl with no bump. For a differentiable convex function, the tangent always stays below the curve (Boyd and Vandenberghe, 2004): , so a point where satisfies for every : it is necessarily the global minimum. Unfortunately, the score of a network with a hidden layer is not convex in general.
Saddle points. A saddle point Saddle point A point where the gradient is zero but which is neither a minimum nor a maximum: the function rises in some directions and falls in others, like a mountain pass. Classic example: w₁² − w₂² at the origin. In high dimension they are argued to be far more common than local minima, and they slow gradient descent down. Source: Dauphin et al., 2014 is a pass: a minimum in some directions, a maximum in others. The typical example is at the origin. The gradient is zero there, yet the score still goes down as soon as we move along . Dauphin and his co-authors (2014) argue, with arguments from statistical physics and experiments on networks, that in very high dimension the critical points with a high score are mostly passes, not local minima. The real danger is not getting trapped, it is slowing down for a long time around them.
Plateaus. On a flat spot, the slope is tiny without being zero, and the step is tiny too. We advance at an ant’s pace. This is the cousin of the vanishing gradient from chapter 8: when sigmoids saturate, their derivatives are squashed and the score becomes flat.
Ravines. Take the score from chapter 7 again, in a more pronounced version: . The bowl is ten times steeper along than along . According to act 2, each direction has its own threshold: along , but along . With a single for everyone, we are stuck. Take it large and we zigzag from one wall to the other. Take it small and we crawl along the bottom. Switch the lab to ravine mode to see it.
Gradient descent lab
Three things to watch as you play:
- At , the coordinate is multiplied by at every step: it changes sign, hence the zigzag. The coordinate , for its part, is multiplied only by : it advances slowly.
- Lower : the zigzag disappears, but progress along the bottom becomes endless.
- Go above : the steep direction diverges, whereas the flat direction could have handled a step ten times larger. Chapter 12 will settle this conflict by giving each direction its own memory and its own scale.
Exercises
Grab something to write with. The solutions are deliberately detailed, line by line.
Exercise 1. In chapter 8, on the toy network, we found for a weight equal to . Take one descent step with . Does the weight increase or decrease, and why?
Exercise 2. On , starting from , compute and for , then , then . Say each time whether the descent converges.
Exercise 3. For the score , give the rate that reaches the minimum in a single step, then the largest rate for which the descent still converges.
Exercise 4. On , we start from with . What does the descent lemma guarantee about the drop in the score? Compare with the actual drop.
Exercise 5. A dataset contains examples. How many descent steps do we take per epoch with mini-batches of examples? And with mini-batches of ?
In one sentence
Gradient descent takes a step at each iteration, and the descent lemma guarantees that the score goes down as long as the step stays below , where measures how fast the slope can change.
Quiz
1. What does a gradient descent step do?
2. On L(w) = a w², for which rates does the descent converge?
3. What does the descent lemma guarantee for a β-smooth score and η ≤ 1/β?
4. Why is a mini-batch gradient useful even though it is noisy?
5. Why does the descent zigzag in a ravine like L = w₁² + 10 w₂²?
Towards chapter 10
We now know how to drive the score down, step by step, with a proven guarantee. But which score are we lowering? The one on the training examples, and only that one. Yet what matters is performance on examples the network has never seen. Think of a curve that must pass through ten measurement points. A degree-nine polynomial can go through all of them exactly, with a zero score, and oscillate wildly between them. It has learned the points by heart, not the trend. A large enough network can do exactly the same thing, and gradient descent will help it with all its might. Chapter 10 teaches how to descend without learning by heart: that is regularisation.
Sources
- Cauchy, A. (1847). « Méthode générale pour la résolution des systèmes d’équations simultanées. » Comptes rendus hebdomadaires des séances de l’Académie des sciences 25, 536-538. Reprinted in the Œuvres complètes, series 1, volume 10, 399-402.
- Robbins, H. & Monro, S. (1951). “A Stochastic Approximation Method.” The Annals of Mathematical Statistics 22(3), 400-407. DOI 10.1214/aoms/1177729586
- Nesterov, Y. (2004). Introductory Lectures on Convex Optimization: A Basic Course. Kluwer. Lemma 1.2.3 (descent lemma).
- Boyd, S. & Vandenberghe, L. (2004). Convex Optimization. Cambridge University Press. Section 3.1.3 (first-order condition). https://web.stanford.edu/~boyd/cvxbook/
- Dauphin, Y. N., Pascanu, R., Gulcehre, C., Cho, K., Ganguli, S. & Bengio, Y. (2014). “Identifying and attacking the saddle point problem in high-dimensional non-convex optimization.” NeurIPS 2014. arXiv:1406.2572
- Bottou, L., Curtis, F. E. & Nocedal, J. (2018). “Optimization Methods for Large-Scale Machine Learning.” SIAM Review 60(2), 223-311. DOI 10.1137/16M1080173
- Goodfellow, I., Bengio, Y. & Courville, A. (2016). Deep Learning. MIT Press. Sections 4.3 (gradient-based optimization) and 8.1 to 8.3 (learning, challenges, SGD). https://www.deeplearningbook.org