Skip to content

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.

-20202468slope 4
The curve y = x squared, with its slope at x = 2 drawn as a short red line. The line rises 4 for each 1 across.

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.

-20202468slope -4
The same curve at x = minus 2. The red line falls to the right, so the slope is minus 4 and downhill is to the right.
xheightslopedownhill is to the
-39-6right
-24-4right
-11-2right
000neither: the bottom
112left
244left
396left
Seven places on the curve. Each slope is twice x, as questions 1 and 2 below work out. Its sign says which way is up, and downhill is the other way.

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.

-2020246810.5
Three steps downhill from 2, at step size 0.25. Each yellow dot is a place the walk stands. The numbers beside the first two jumps say how far each moved, 1 and 0.5. The third, too short for a number, moved 0.25. The ringed dot, at 0.25, is the last.
stepfrom xslopemovesto x
12411
2120.50.5
30.510.250.25
The same three steps as numbers. Each step moves 0.25 times the slope where it stands, towards the bottom.

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.

-2020246810.5
Three steps from minus 2, at step size 0.25. The slopes there are minus numbers, so the steps go right: minus 1, minus 0.5 and minus 0.25.

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.

-2020246810.5

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.

Four steps from 2, with the step size free to change. The number beside a jump is how far it moved. At 0.05 they end at 1.31, still far up. At 0.25 they end at 0.125, which the sentence above rounds to 0.13. At 0.5 the first lands on the bottom. Past 0.5 they jump from side to side. At 0.75 they end where 0.25 does, and at 1.05 they end at 2.93, where the curve stands at 8.57.
0246801234steps takenthe curve's heightstep size 0.05step size 0.25step size 1.05
The curve's height after each step from 2, to two decimal places, for three step sizes. After four steps the three stand at 1.72, 0.02 and 8.57. The smallest size creeps down, 0.25 drops fast, and the biggest climbs.

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 nn, 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.

question 1Stand at any xx on the curve and look 1 either side. Call the slope between those two points GG. What is GG?
  1. the move: the rise over the distance across

    the line:
    G=(x+1)2−(x−1)22G = \frac{(x + 1)^2 - (x - 1)^2}{2}

    why it is allowed: The curve stands at (x+1)2(x + 1)^2 on the right and (x−1)2(x - 1)^2 on the left. The rise is the first take away the second. The two points are 2 apart.

  2. the move: multiply out each square

    the line:
    G=(x2+2x+1)−(x2−2x+1)2G = \frac{(x^2 + 2x + 1) - (x^2 - 2x + 1)}{2}
    timesx1
    xx squaredx
    1x1
    x plus 1 times itself, box by box. x squared, two boxes of x, and 1.

    why it is allowed: x+1x + 1 times itself is x2x^2, two lots of xx, and 1. x−1x - 1 times itself is x2x^2, take away two lots of xx, plus 1.

  3. the move: take the second bracket away

    the line:
    G=4x2G = \frac{4x}{2}

    why it is allowed: The x2x^2 cancel and the 1s cancel. Two lots of xx, take away minus two lots, is four lots.

  4. the move: halve

    the line:
    G=2xG = 2x

    why it is allowed: Four lots of xx 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 xx: at minus 2 it gives minus 4.

try it with a small xx: at x=3x = 3, line 1 gives G=6G = 6, which you can work out by hand

question 2Stand at 2 and take the two points ww either side, instead of 1. Call the slope between them SS. Does SS change with ww?
  1. the move: the rise over the distance across

    the line:
    S=(2+w)2−(2−w)22wS = \frac{(2 + w)^2 - (2 - w)^2}{2w}

    why it is allowed: The curve stands at (2+w)2(2 + w)^2 on the right and (2−w)2(2 - w)^2 on the left. The two points are 2w2w apart.

  2. the move: multiply out each square

    the line:
    S=(4+4w+w2)−(4−4w+w2)2wS = \frac{(4 + 4w + w^2) - (4 - 4w + w^2)}{2w}
    times2w
    242 times w
    w2 times ww squared
    2 plus w times itself, box by box. 4, two boxes of 2 times w, and w squared.

    why it is allowed: 2+w2 + w times itself is 4, four lots of ww, and w2w^2. 2−w2 - w times itself is 4, take away four lots of ww, plus w2w^2.

  3. the move: take the second bracket away

    the line:
    S=8w2wS = \frac{8w}{2w}
    wheightsrise over acrossslope
    11 and 98 over 24
    0.52.25 and 6.254 over 14
    0.13.61 and 4.410.8 over 0.24
    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 w2w^2 cancel. Four lots of ww, take away minus four lots, is eight lots.

  4. the move: divide

    the line:
    S=4S = 4

    why it is allowed: 8w8w over 2w2w is 4 for any ww 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 xx in place of 2, each 4 becomes xx times xx, and they still cancel. Each square's four lots of ww were two boxes of 2 times ww, so they become two lots of xx times ww. Two lots, take away minus two lots, is four lots. Over 2w2w that is twice xx. At 3 the rise is twelve lots of ww, and the slope is 6.

try it with a small ww: at w=1w = 1, line 1 gives S=4S = 4, which you can work out by hand

question 3From any xx, take one step of size a quarter. Call the place it lands NN. Where is NN?
  1. the move: step against the slope

    the line:
    N=x−14×2xN = x - \frac{1}{4} \times 2x

    why it is allowed: A step takes the step size times the slope away from xx. Questions 1 and 2 found that the slope at xx is 2x2x.

  2. the move: take a quarter of the slope

    the line:
    N=x−x2N = x - \frac{x}{2}

    why it is allowed: A quarter of two lots of xx is half of xx.

  3. the move: take the half away

    the line:
    N=x2N = \frac{x}{2}

    why it is allowed: xx take away half of xx leaves half of xx. So each step of size a quarter halves xx: 2, 1, 0.5 and 0.25. Halving never reaches 0, but it gets as close as you like.

try it with a small xx: at x=2x = 2, line 1 gives N=1N = 1, which you can work out by hand

question 4Now take one step of size 1 from any xx. Call the place it lands MM. Where is MM?
  1. the move: step against the slope

    the line:
    M=x−1×2xM = x - 1 \times 2x

    why it is allowed: The same rule as question 3, with step size 1.

  2. the move: take the two lots away

    the line:
    M=−xM = -x

    why it is allowed: xx take away two lots of xx is minus xx: 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.

try it with a small xx: at x=2x = 2, line 1 gives M=−2M = -2, 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.

  1. At c = 4 the misses are minus 3 and minus 1, so the loss is 9 plus 1, which is 10.
  2. 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.
  3. Step against it. 0.125 times 8 is 1, so c goes from 4 to 3, and the loss falls from 10 to 4.
  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.
  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.
  6. 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.
0123450510152010.5
The flat line's squared loss for each height c. Here c runs across and the loss runs up. Three steps downhill from 4, at step size 0.125, land on 3, 2.5 and 2.25.

the trap

check yourself

  1. 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.

  2. 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.

  3. 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.