Walking downhill
Lesson 4.4.1 found minima by solving . Usually that equation has no formula, so the computer walks downhill instead. Lesson 4.2.1 showed that is the direction in which falls fastest.
Rule: Gradient descent
Start at a point and repeat the step. The step size (eta), a positive number, sets how far each step goes; in machine learning it is called the learning rate. At a critical point the gradient is , so the steps stop there.
has the units that make a distance in the units of . Doubling doubles every step.
Take of lesson 4.4.1, with minimum and gradient . From the gradient is . With the first step goes to , where .
| step | distance to | ||
|---|---|---|---|
| 0 | 1 | 2.8284 | |
| 1 | 1.9799 | ||
| 2 | 1.3859 | ||
| 3 | 0.9702 | ||
| 4 | 0.6791 | ||
| 5 | 0.4754 |
falls at every step, towards . Each distance is 0.7 times the one before; the next section explains that number. After 42 steps, is below .
Try it
The loop prints each step, and the distance to , then takes the step.
descend repeats the step until and returns the point and the number of steps.
How large a step?
One variable
Take , with its minimum at 0 and slope . One step gives : each step multiplies by the same factor . From :
| factor | |||||
|---|---|---|---|---|---|
| 0.1 | 0.6 | 0.6 | 0.36 | 0.216 | 0.1296 |
| 0.25 | 0 | 0 | 0 | 0 | 0 |
| 0.45 | 0.64 | 0.4096 | |||
| 0.55 | 1.44 | 2.0736 |
A small step creeps towards 0 from one side. At 0.25 one step lands on the minimum.
At 0.45 every step overshoots to the other side but still gets closer. At 0.55 each overshoot is larger than the last: the method diverges.
The steps close in on 0 exactly when the factor lies between and 1, that is, when ; past they overshoot. The 4 in is , the curvature (lesson 2.4.2). So a curve that bends more allows only a smaller step: the limit is .
Several variables: one factor per eigenvector
For a quadratic with minimiser , lesson 4.4.1 gave . Write for the error. Then one step is
Along a unit eigenvector of , with eigenvalue , (lesson 3.6.1). So the part of the error along each eigenvector is multiplied by its own factor at every step, as in one variable.
Rule: Step size for a quadratic
Here is positive definite and is its largest eigenvalue. The factor furthest from 0 in size sets the speed. Near the minimum of any smooth , the Hessian there plays the part of .
For , has eigenvalue 3 along and 1 along (lesson 3.6.3). So the limit is . From the error lies along , which is why it shrank by exactly per step.
From the error has a part along each eigenvector. Steps until :
| factor along | factor along | steps | |
|---|---|---|---|
| 0.1 | 0.7 | 0.9 | 139 |
| 0.3 | 0.1 | 0.7 | 41 |
| 0.5 | 0.5 | 22 | |
| 0.6 | 0.4 | 66 | |
| 0.66 | 0.34 | 722 | |
| 0.7 | 0.3 | never: diverges |
Too small is slow, because the factor along is close to 1. Too large is slow too, because the factor along is close to , and past it diverges. The best fixed step, , makes the two factors the same size.
Gradient descent with two step sizes on the same bowl, and in a narrow valley. A step too large for the steep direction makes the path bounce across it. In the narrow valley the bouncing goes on while the path creeps along the floor.
At the part of the error along goes , 0.7778, , 0.9412: it grows by 1.1 each step and flips sign. first falls to , then climbs, reaching after seven steps, and goes on rising.
Try it
descend is the loop of the first cell. The first loop is the table: steps from for each step size, or None when the path runs off.
The last loop follows : the step, , and the part of the error along .
Long narrow valleys
The ratio of a positive definite is its condition number (lesson 3.3.2). It measures how stretched the bowl is: the level curves are ellipses whose axes have lengths in the ratio (lesson 3.7.1). With the best fixed step, the slowest factor is
which is 0.5 for , 0.8182 for , and close to 1 for a large .
Take , with Hessian and . The limit is . From :
| factor for | factor for | steps | |
|---|---|---|---|
| 0.01 | 0.98 | 0.8 | 787 |
| 0.05 | 0.9 | 0 | 151 |
| 0.09 | 0.82 | 81 | |
| 0.095 | 0.81 | 160 | |
| 0.1 | 0.8 | never: bounces forever | |
| 0.105 | 0.79 | never: diverges |
At , flips sign at every step, 1, , 0.64, , while creeps from to , , . The path zig-zags across the valley, as in the figure. The steep direction caps the step size, and the gentle direction pays for it.
For the round bowl , with , the step reaches the minimum from any start in one step. Rescaling by would turn into that bowl. Lesson 4.4.3's Newton's method does such a rescaling automatically.
A curved valley
The function has a long valley along the curve , with its minimum at . There the Hessian is , with eigenvalues 0.3875 and 51.6125, so . Near the minimum, a step must stay below .
From with , 1688 steps bring below . After 50 steps the point is still at , crawling round the bend.
With , just above the limit, the path gets near quickly but never settles. It ends up jumping between two points, 0.0539 and 0.055 from the minimum. With the steps blow up within six steps.
The explorer starts on this valley at with step size 0.02. Set 50 steps and watch the path zig-zag across the curved floor as it creeps along. Then try 0.04, and 0.05, when the steps grow without limit.
Pick the tilted bowl , move the start to , and compare 0.3 with 0.6. The Newton's method switch is for lesson 4.4.3.
Explore
Each step moves against the gradient, a distance set by the step size. Try a small and a large step size, then switch to Newton's method, which uses the Hessian too.
- Point
- (−1.5, 2.5), f = 6.5625
- Gradient descent, step size 0.02
- after 5 steps: (−1.508, 2.3287), f = 6.305
Try it
The first lines are the valley's gradient and its Hessian at , with the eigenvalues, and the limit .
Then 50 steps with , and with the two points the path ends up jumping between, after 10,000 and 10,001 steps.
When to stop
The steps rarely land exactly on the minimum, so the code needs a rule for when to stop. Three tests are common, and each can mislead.
Rule: Stopping rules
A small gradient means the ground is nearly flat; a small step means the point has stopped moving. A third test stops when stops falling. Whatever the test, cap the number of steps and report when the cap is hit.
A small gradient in a flat valley. For a quadratic, the distance to the minimum is at most . Take at : , a small gradient, yet the minimum is 1 away. Scale the tolerance to the problem.
A small step with a small . Each step has length . With on the bowl , the first step from moves only 0.000283, far from the minimum. A step test then stops at once.
Backtracking: a step size without
In practice is unknown and changes from point to point. Backtracking chooses afresh at each step, by trial.
The tangent plane (lesson 4.2.2) says a step lowers by about . Accept the step if really falls by at least half of that; otherwise halve and try again.
Rule: Backtracking
Then take the step with that . A step that overshoots fails the test, so falls at every accepted step. Each trial costs one evaluation of .
On the curved valley from , backtracking needs 442 steps, against 1688 with the fixed . The first accepted step size is , and later ones are often or . Its 2017 halvings in all each cost one evaluation of , so the saving is real but smaller than the step counts suggest.
Try it
backtrack takes gradient steps with the rule above. It returns the point, the number of steps and the number of halvings.
The two lines are the curved valley from and from .
Lesson 4.4.3 replaces the fixed direction with Newton's step, which bends towards the minimum in a narrow valley. Practice set 4.4.4 asks you to write the backtracking method.
Common mistakes
- Too large a step diverges. Past the error grows each step; watch that falls.
- Too small a step only looks safe. The path creeps, and a step-length test may stop it far from the minimum.
- The limit comes from the steepest direction. It is , set by the largest eigenvalue, not by an average curvature.
- A small gradient is not a small error in a flat valley: has at , a distance of 1 from the minimum.
- A zig-zag is bad scaling, not a bug. It comes from a large condition number; rescale the variables or use Newton's method.
- Gradient descent stops at any critical point it reaches, including a local minimum that is not global (lesson 4.4.1).
- Always cap the number of steps, and report when the cap is reached.
Gradient descent
0 of 11 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- Gradient descent: , a step against the gradient of size set by .
- In one variable each step multiplies the error by about : it converges when , overshooting when .
- For a quadratic with positive definite , the error along each eigenvector is multiplied by ; it converges from every start exactly when .
- A large condition number means a narrow valley: the best factor is , and the path zig-zags.
- Stop on a small gradient or a small step, scaled to the problem, and always cap the steps. A small gradient can still be far from the minimum.
- Backtracking halves from 1 until , so falls at every step without knowing .