ENع
←Gradient descent
Lesson 06 · Foundations

Gradient descent

12 min9 steps5 questionsBeginner
About this lesson

Training a model means finding the weights that make the loss small. Gradient descent does it one step at a time: measure the slope of the loss where you stand, then step a little way downhill, and repeat. Watch a one-weight model learn from two examples, see why its steps shrink near the bottom, and see what a learning rate that is too small or too big does.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 6 · Interactive
01 / 09

One knob to tune

The model guesses y from x using one number, its weight w: guess = w × x. Two training examples: x = 1 should give 2, and x = 2 should give 4. Training must find a good w. We start at w = 0.

model      guess = w * x
data       x = 1 -> y = 2
           x = 2 -> y = 4
start      w = 0   (guesses 0 and 0: both wrong)
goal       find the w with the smallest loss
Loading figure…
or ← →
Read the lesson step by step
  1. One knob to tune

    The model guesses y from x using one number, its weight w: guess = w × x. Two training examples: x = 1 should give 2, and x = 2 should give 4. Training must find a good w. We start at w = 0.

    model      guess = w * x
    data       x = 1 -> y = 2
               x = 2 -> y = 4
    start      w = 0   (guesses 0 and 0: both wrong)
    goal       find the w with the smallest loss
  2. The loss for every w

    Loss is the average squared error. At w = 0 both guesses are 0, so the errors are 2 and 4: loss = (4 + 16) ÷ 2 = 10. The valley shows the loss at every w; lowest at w = 2.

    loss(w) = average of (guess - y) squared
    w = 0:  errors 0-2 = -2 and 0-4 = -4
            squares 4 and 16, average 10
    w = 1:  loss 2.5     w = 2:  loss 0 (the bottom)
    w = 4:  loss 10      (too big: guesses 4 and 8)
  3. Feel the slope

    The computer cannot see the whole valley. It can only measure the slope where it stands, called the gradient. At w = 0 the slope is −10: nudge w up by 0.01 and the loss drops by about 0.1. Downhill is to the right.

    slope(w) = average of 2 * (guess - y) * x
    w = 0:   (2*(-2)*1 + 2*(-4)*2) / 2 = -10
    check:   loss(0.01) = 9.90025   (10 - 0.09975)
    negative slope -> loss falls as w grows
                   -> downhill is to the right
  4. Step downhill

    Gradient descent steps against the slope: new w = w − learning rate × slope. With a learning rate of 0.1: 0 − 0.1 × (−10) = 1. The loss drops from 10 to 2.5.

    w = w - lr * g
      = 0 - 0.1 * (-10)
      = 1
    loss(1) = 2.5   (was 10)
  5. Flatter ground, smaller step

    At w = 1 the slope is only −5, so the same rule takes a smaller step: 1 − 0.1 × (−5) = 1.5, and the loss falls to 0.625. Near the bottom the ground is flatter, so the steps shrink by themselves.

    w = 1:   slope = -5
    w = 1 - 0.1 * (-5) = 1.5
    loss(1.5) = 0.625
    step sizes so far: 1, then 0.5
  6. Repeat: the training loop

    That is the whole loop: measure the slope, step, repeat. Two more steps reach w = 1.75, then 1.875, and the loss falls to 0.16, then 0.04. The slope shrinks toward 0 at w = 2, which fits both examples exactly.

    step   w        loss         slope used
    1      1        2.5          -10
    2      1.5      0.625        -5
    3      1.75     0.15625      -2.5
    4      1.875    0.0390625    -1.25
    each step halves the distance to w = 2
  7. The learning rate

    The 0.1 is the learning rate: how big a step to take for each unit of slope. People choose it before training. Start again at w = 0 with 0.01 instead: four steps only reach w ≈ 0.37. Too small means slow.

    lr = 0.01, start at w = 0
    step 1   w = 0.1        loss = 9.025
    step 2   w = 0.195      loss = 8.145
    step 3   w = 0.28525    loss = 7.351
    step 4   w = 0.370988   loss = 6.634
    with lr = 0.1, four steps reached 1.875
  8. Too big a step

    Now try 0.4. From w = 0 the step overshoots: 0 − 0.4 × (−10) = 4, the far wall, where the loss is still 10. The slope there is +10, so it jumps straight back. It bounces for ever; anything bigger climbs out.

    lr = 0.4, start at w = 0
    w = 0 - 0.4 * (-10) = 4    loss 10
    w = 4 - 0.4 * (10)  = 0    loss 10
    w: 0 -> 4 -> 0 -> 4 -> 0 ... never settles
    lr above 0.4: each jump lands higher (diverges)
  9. Same idea, many knobs

    Real models have millions or billions of weights, so the valley has that many directions. Backpropagation finds the slope for every weight at once, and each step usually uses a small random batch of examples: stochastic gradient descent.

    one weight      slope = one number
    many weights    gradient = one slope per weight
    every step      w = w - lr * slope, for every weight
    backpropagation computes all the slopes at once
    SGD             each step uses a random mini-batch

Quiz Unlocks after the last step.