the gradient, which way is downhill
learning from examples
what it is
A curve's slope at a point says how steep it is there, and which way is up. Downhill is the other way. Step against the slope, again and again, and you walk to the bottom.
Take the curve whose height is x times x, written y = x squared. At x = 2 it stands at 4. How steep is it there? Look 1 either side. At x = 1 it stands at 1, and at x = 3 at 9. That is 8 up over 2 across, so 4 up for each 1 across. The slope at 2 is 4. Closer points give the same slope, as question 2 below shows.
A slope has a sign. At x = minus 2 the curve stands at 4 again. Going right, from minus 3 to minus 1, it falls from 9 to 1. That is 8 down over 2 across, so the slope at minus 2 is minus 4. A plus slope climbs to the right, so downhill is to the left. A minus slope falls to the right, so downhill is to the right. At the bottom, where x is 0, the slope is 0: the curve is flat there.
| x | height | slope | downhill is to the |
|---|---|---|---|
| -3 | 9 | -6 | right |
| -2 | 4 | -4 | right |
| -1 | 1 | -2 | right |
| 0 | 0 | 0 | neither: the bottom |
| 1 | 1 | 2 | left |
| 2 | 4 | 4 | left |
| 3 | 9 | 6 | left |
Books call the slope at a point the gradient, or the derivative. A line's loss has two settings to tune: how steep the line is and how high it sits. Move one setting alone, the other held still: how far the loss rises for each 1 the setting moves is the loss's slope for that setting. The gradient is the list of both.
To go downhill, step against the slope: multiply the step size by the slope, and take that away from x. The step size is a small number you choose, here 0.25. At 2 the slope is 4, so take away 0.25 times 4, which is 1: you land on 1. There the slope is 2, so the next step moves 0.5, to 0.5. The third moves 0.25, to 0.25. The steps shrink as the curve flattens.
| step | from x | slope | moves | to x |
|---|---|---|---|---|
| 1 | 2 | 4 | 1 | 1 |
| 2 | 1 | 2 | 0.5 | 0.5 |
| 3 | 0.5 | 1 | 0.25 | 0.25 |
From the left it works the same way. At minus 2 the slope is minus 4, and taking away a minus number adds. Minus 2, take away 0.25 times minus 4, is minus 2 plus 1: minus 1. The step goes right, downhill again.
The step size decides the walk. At 0.05, four steps only crawl from 2 to 1.31. At 0.5 one step lands on the bottom: 2 take away 0.5 times 4 is 0. Bigger sizes jump past the bottom. At 0.75 the steps land on minus 1, then 0.5, still closing in. At 1 they bounce between 2 and minus 2 for ever. At 1.05 each jump lands higher than the last, and the walk climbs out of the bowl.
With step size 0.25, 4 steps take x from 2 to 0.13, where the curve stands at 0.02.
With scripts on, a slider sets the step size. The steps and this line change with it.
Walking downhill like this is called gradient descent. Machine learning books call the step size the learning rate. There the curve is a loss and x is a setting to tune, so the bottom is the setting that fits best. k-means, another page, sorts points into groups and gives each group a centre, one point standing for them all. It goes downhill with no step size to choose. Moving a centre to the middle of its group, the mean, lands on the bottom of that group's squared loss at once.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: the rise over the distance across
the line:why it is allowed: The curve stands at on the right and on the left. The rise is the first take away the second. The two points are 2 apart.
the move: multiply out each square
the line:times x 1 x x squared x 1 x 1 x plus 1 times itself, box by box. x squared, two boxes of x, and 1. why it is allowed: times itself is , two lots of , and 1. times itself is , take away two lots of , plus 1.
guess it, thenthe move: take the second bracket away
the line:why it is allowed: The cancel and the 1s cancel. Two lots of , take away minus two lots, is four lots.
guess it, thenthe move: halve
the line:why it is allowed: Four lots of over 2 is two lots. So the slope at 2 is 4 and the slope at 3 is 6. The working is the same for a minus : at minus 2 it gives minus 4.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: the rise over the distance across
the line:why it is allowed: The curve stands at on the right and on the left. The two points are apart.
the move: multiply out each square
the line:times 2 w 2 4 2 times w w 2 times w w squared 2 plus w times itself, box by box. 4, two boxes of 2 times w, and w squared. why it is allowed: times itself is 4, four lots of , and . times itself is 4, take away four lots of , plus .
guess it, thenthe move: take the second bracket away
the line:w heights rise over across slope 1 1 and 9 8 over 2 4 0.5 2.25 and 6.25 4 over 1 4 0.1 3.61 and 4.41 0.8 over 0.2 4 Three widths at 2. The rise and the distance across shrink together, so the slope stays 4. why it is allowed: The 4s cancel and the cancel. Four lots of , take away minus four lots, is eight lots.
guess it, thenthe move: divide
the line:why it is allowed: over is 4 for any above 0. The points can be 1 apart or a hair apart: the slope between them is always 4. So 4 is the slope at 2 itself. With in place of 2, each 4 becomes times , and they still cancel. Each square's four lots of were two boxes of 2 times , so they become two lots of times . Two lots, take away minus two lots, is four lots. Over that is twice . At 3 the rise is twelve lots of , and the slope is 6.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: step against the slope
the line:why it is allowed: A step takes the step size times the slope away from . Questions 1 and 2 found that the slope at is .
the move: take a quarter of the slope
the line:why it is allowed: A quarter of two lots of is half of .
guess it, thenthe move: take the half away
the line:why it is allowed: take away half of leaves half of . So each step of size a quarter halves : 2, 1, 0.5 and 0.25. Halving never reaches 0, but it gets as close as you like.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: step against the slope
the line:why it is allowed: The same rule as question 3, with step size 1.
the move: take the two lots away
the line:why it is allowed: take away two lots of is minus : just as far out, on the other side. The curve is as high there, since a minus number times itself is a plus number. So the walk bounces and never gets lower.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Two points stand at heights 1 and 3. A flat line guesses one height, c, for both. Its squared loss adds the squares of its two misses, 1 minus c and 3 minus c. Find the best c by walking downhill from 4, with step size 0.125.
- At c = 4 the misses are minus 3 and minus 1, so the loss is 9 plus 1, which is 10.
- One either side, the loss is 4 at c = 3 and 20 at c = 5. That is 16 up over 2 across, so the slope is 8. Half either side, at 3.5 and 4.5, it is 6.5 and 14.5. That is 8 up over 1 across, still 8.
- Step against it. 0.125 times 8 is 1, so c goes from 4 to 3, and the loss falls from 10 to 4.
- One either side of 3, the loss is 2 at c = 2 and 10 at c = 4. That is 8 up over 2 across, so the slope is 4. 0.125 times 4 is 0.5, so c goes to 2.5.
- One either side of 2.5, the loss is 2.5 at c = 1.5 and 6.5 at c = 3.5. That is 4 up over 2 across, so the slope is 2. 0.125 times 2 is 0.25, so c goes to 2.25.
- The steps close in on 2, the mean of 1 and 3. There the loss is 1 plus 1, which is 2. One either side it is 4 and 4, so the slope is 0. That is the bottom.
the trap
check yourself
On y = x squared, measure the slope at x = 3 with the two points 2 either side. Is it still 6?
the answer
At 5 the curve stands at 25, and at 1 at 1. That is 24 up over 4 across, which is 6. The width does not change it.
From x = minus 3 with step size 0.25, where does one step land? Which way did it go?
the answer
The slope at minus 3 is minus 6. Minus 3, take away 0.25 times minus 6, is minus 3 plus 1.5: minus 1.5. The step went right, downhill.
From x = 1 with step size 1.5, where do two steps land, and how high is the curve there?
the answer
At 1 the slope is 2: 1 take away 1.5 times 2 is minus 2, where the curve stands at 4. At minus 2 the slope is minus 4: minus 2 plus 6 is 4, where it stands at 16. The step size is too big, so the walk climbs.