You stand on a hillside in fog and want to reach the valley floor. You can only feel the slope under your feet, so you take a step downhill, feel again, and step again. Below, a ball does that on a map of a loss, four times, with four step sizes. Before you play it: which run reaches the bottom first?
Step downhill, and choose the step size
The loss (θ₁ − 2)² + 5(θ₂ − 1)², from (−2, 3); four learning rates.
Contours of the loss, from 1 at the centre to 36; the bottom is at (2, 1). The ball starts at (−2, 3), where the loss is 36.
Describe this picture
Two panels. The first is a contour map of the loss , with across from −4 to 8 and up from −3.5 to 5.5 at equal scales: six ellipses, where the loss is 1, 4, 9, 16, 25 and 36, round a small cross at the bottom, . The second plots the loss against the step, from 0 to 30 across and from 0 to 40 up; a loss above 40 is drawn on the top edge with a small upward arrow. In the clip the contours draw outward and a ball appears at , where the loss is 36. An arrow from the ball shows the gradient, , pointing uphill; a second arrow shows the step, the opposite way, which does not point at the bottom. Then four runs, one after another, at 6 steps a second, each leaving a trail that dims and stays: learning rate 0.02 (tiny steps, and after 30 the loss is still 1.38), 0.08 (it glides in, loss 0.0005 after 30 steps), 0.17 (big steps overshoot the valley floor, yet this run reaches the bottom fastest) and 0.21 (each swing is larger than the last, and it leaves the map at step 9). The last frame lights the cross: the runs that settle all end on it, and an arrow from the end of the 0.02 run, still on its way, points to it. Readouts give the learning rate, the step and the loss, or “off the scale” and “off the map”. After the clip a slider, “Learning rate, η”, runs from 0.01 to 0.25 in steps of 0.01 and starts at 0.21; each change replays the run from the start. With reduced motion the four runs are shown as small maps side by side.
Walking downhill
The picture is a map seen from above. The two numbers a model adjusts are and , its parameters; on these pages is a parameter, never an angle. I write the pair as . The loss measures how wrong the model is with those parameters, and each ellipse joins the points with one value of the loss. This loss is a bowl, lowest at .
The slope under the ball is the gradient, from Partial derivatives and the gradient: the arrow of the two slopes,
which points straight uphill. At the start, , it is . To go down, step the other way. One step is
Here counts the steps taken, not time, and the bold is where the ball stands after steps; the plain and stay its two entries. The positive number (eta) is the learning rate: how far to go per unit of slope. Repeating the step is gradient descent.
Now the answer to the question. The third run, at 0.17, zig-zags across the valley and still reaches the bottom first. The first crawls and is still short after 30 steps, the second glides in, and the fourth swings wider each time and leaves the map. Every run that settles ends on the same cross. The step size changes the path, never the bottom. Drag from 0.01 up and find where a run first leaves the map: 0.21.
Watch out
A bigger learning rate is not always faster. Past a limit set by the bowl, each step makes things worse.
One direction first
To see why, take a bowl with one parameter, with : lowest at . Its slope is , and here is the one parameter after steps, so a step gives
Every step multiplies the distance from the bottom by the same factor, . If the factor is between −1 and 1, the distance shrinks to 0. That happens for . A negative factor means the step jumps past the bottom to the other side, an overshoot. At the factor is −1, and the ball bounces between the same two points for ever. Beyond it the factor is below −1, and each swing is larger than the last: the run diverges.
One parameter, four step sizes
The loss (θ − 2)², from θ = −1; five steps at each learning rate.
η = 0.25: the factor 1 − 2η is 0.5. Each step goes against the slope of the tangent, by η times the slope.
Describe this picture
One panel: the curve , with across from −6 to 10. Under its axis is a lane of five rows, one per step, where each step is drawn as an arrow from where it starts to where it lands. The clip plays all four learning rates in turn, 0.25, 0.75, 1 and 1.25, five steps each from : at each step the tangent to the curve appears, then the step, and the point lands on the curve. Each run replaces the one before. Readouts give the learning rate, the factor , the step and . After the clip a four-way choice, “Learning rate, η”, replays any one of the runs from . With reduced motion the four runs are shown side by side. At 0.25 the distance to the bottom halves each step. At 0.75 every step overshoots by half the distance. At 1 the point bounces between −1 and 5 for ever. At 1.25 the swings grow: the run leaves the frame at step 3, at , and the caption says that by step 5 it would be at 24.8.
Doubling the loss doubles , so the largest safe learning rate halves. The step size and the steepness of the loss always go together.
Two directions, one step size
The bowl on the map is two one-direction bowls added together: along and along . The gradient’s two entries do not mix, so each step multiplies
Both factors must lie between −1 and 1. Along that allows , along only . The more sharply curved direction sets the limit: , where is the larger of the two. The run at 0.21 is a little past it: its factor along is −1.1, and each swing across the valley is 10 % larger than the last.
No single is perfect in both directions, because the two curvatures differ by a factor of 5. The best I can do is make the two factors equally small: gives , with factor along and along . The run at 0.17 is close to that: factors 0.66 and −0.7. Its zig-zag across the valley is the price of shrinking the slow direction fast. The run at 0.08 never zig-zags, but its slow factor, 0.84, leaves it behind.
The step arrow at the start also misses the bottom. The gradient is steep along , where the bowl is steep, so the step points mostly across the valley rather than along it to the bottom. Steepest down is not straight to the bottom unless the bowl is round or the ball sits on one of its axes.
In linear algebra · the bowl and its eigenvalues
The bowl here is the quadratic form of a positive definite matrix: its matrix of second derivatives, the Hessian, is . For any such bowl the safe step is , with the largest eigenvalue of the Hessian; here . Iterative solvers runs steepest descent on the same kind of bowl, and Conditioning names the ratio of the curvatures that causes the zig-zag.
When to stop
Gradient descent does not know it has arrived. Three stopping rules are common, and programs often use more than one:
- the gradient is small: falls below a chosen tolerance, since at the bottom of a bowl the gradient is zero;
- the loss has stopped falling: it changes by less than a tolerance between steps;
- a budget: a fixed number of steps, whatever happens.
On a bowl there is one bottom, and every run that settles finds it. A loss with several valleys is different: the bottom a run reaches can depend on where it starts. This page keeps to bowls.
Worked example
Take the map’s bowl with . The factors are along and along .
Step 0. The ball starts at , where and .
Step 1. It moves to , where .
Step 2. There , so the ball moves to , where .
Step 3. There , so it moves to , where .
The factor rule checks every step: and . So zig-zags about 1, its distance halving each time, while creeps up towards 2.
How many steps? For to come within 0.01 of 2, I need , that is , so : 17 steps. By then is within 0.0001 of 1.
The angle the step misses by. The first step points along ; the bottom lies along from the start. The cosine of the angle between them is
so the step points 41.6° (0.727 rad) away from the bottom.
One direction. With from : at the points are −1, 0.5, 1.25, 1.625, and the distance halves; at they are −1, 5, −1, 5, and the ball bounces; at they are −1, 6.5, −4.75, 12.125, −13.1875, 24.78, and the distance is multiplied by 1.5 each step with its sign flipping, .
Try it yourself. With on the map’s bowl, the factor along is 0: the first step lands on at once. With it is −1, and bounces between 3 and −1 for ever.
Where you’ll meet this
Fitting models. Nearly every model in this course is fitted by some form of gradient descent, from logistic regression to networks with many layers. The loss is rarely a perfect bowl, but the learning rate behaves as it does here.
Large least-squares problems. When a system is too large to solve by elimination, steepest descent and its relatives walk down the bowl of squared error, one matrix-vector product per step.
Adaptive filters. An echo canceller adjusts its taps a little at every sample, a small step down the bowl of squared error.
In signal processing · step size of an adaptive filter
Adaptive filters: LMS in the signal processing course takes one small downhill step per sample, with for the step size where this page writes . Its too-big step diverges for the same reason as the run at 0.21.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Update | the learning rate, the step | |
| One-direction bowl | for | |
| Safe step | bounces at , diverges beyond | |
| Two directions | the most sharply curved direction sets it | |
| General bowl | : largest eigenvalue of the Hessian | |
| Best single step | , | for |
| Stopping | small , flat loss, or a step budget | |
| Rule | the step size changes the path, not the bottom | on a bowl |