ENع
←Backpropagation
Lesson 14 · Deep learning

Backpropagation

14 min11 steps5 questionsIntermediate
About this lesson

A network learns by changing its weights to make the loss smaller, and for that it needs the gradient of the loss for every weight. Backpropagation gets all of them in one sweep: run the network forward, then send the gradient backwards from the loss, multiplying by each part's local slope (the chain rule). Follow one tiny network through a forward pass, a backward pass, a numerical check and one gradient descent step that cuts its loss from 4 to 0.0004.

You'll learn to

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

Fig 14 · Interactive
01 / 11

A tiny network

Our network has one input, x = 1, two weights, w1 = 2 and w2 = 1.5, and one hidden neuron with ReLU: it keeps positive numbers and turns negative ones into 0. We want the output to be 1.

input    x = 1          target  t = 1
weights  w1 = 2         w2 = 1.5
neuron   z = w1 * x,    h = relu(z) = max(0, z)
output   y = w2 * h

the weights are what training is allowed to change
Loading figure…
or ← →
Read the lesson step by step
  1. A tiny network

    Our network has one input, x = 1, two weights, w1 = 2 and w2 = 1.5, and one hidden neuron with ReLU: it keeps positive numbers and turns negative ones into 0. We want the output to be 1.

    input    x = 1          target  t = 1
    weights  w1 = 2         w2 = 1.5
    neuron   z = w1 * x,    h = relu(z) = max(0, z)
    output   y = w2 * h
    
    the weights are what training is allowed to change
  2. The forward pass

    The forward pass runs the network from left to right: z = w1 × x = 2, ReLU keeps it, so h = 2, and y = w2 × h = 3. Each step keeps its numbers, because the backward pass will need them.

    z = w1 * x        # 2 * 1 = 2
    h = max(0.0, z)   # relu(2) = 2
    y = w2 * h        # 1.5 * 2 = 3
    
    remember z, h and y: the backward pass reuses them
  3. Measure the error

    The loss says how wrong the output is. We use squared error: (y − t)² = (3 − 1)² = 4, where t is the target. Training means making this number smaller by changing w1 and w2.

    loss = (y - t) ** 2   # (3 - 1) ** 2 = 4
    
    y = 3 but we wanted t = 1: the loss is 4
    goal: change w1 and w2 so the loss gets smaller
  4. Which weight to change?

    For each weight we want its gradient: how fast the loss changes when that weight moves a little. Nudging each weight and rerunning works, but costs a forward pass per weight. Backpropagation finds them all in one backward sweep.

    gradient of w1:  dw1 = how fast the loss changes
                     when w1 moves a tiny bit
    
    nudge every weight:  1 extra forward pass per weight
                         (big networks have millions)
    backpropagation:     1 backward pass for all of them
  5. Start at the loss

    Backpropagation starts at the end. The slope of (y − t)² for y is 2(y − t) = 2 × 2 = 4. So if y grew by a tiny amount, the loss would grow by about 4 times that amount.

    dy = 2 * (y - t)    # 2 * (3 - 1) = 4
    
    dy means: the slope of the loss for y
    y up by 0.001  ->  loss up by about 0.004
  6. The chain rule

    y = w2 × h, so nudging w2 changes y by h times as much, and nudging h by w2 times. The chain rule multiplies the slopes: dw2 = 4 × h = 8, and h gets 4 × w2 = 6.

    dw2 = dy * h    # 4 * 2   = 8
    dh = dy * w2    # 4 * 1.5 = 6
    
    chain rule: slope along a path = product of the
    local slopes on it (here dy times dy/dw2 = h)
  7. Through the ReLU

    ReLU's slope is 1 where its input is positive and 0 where it is negative. Here z = 2 is positive, so the gradient passes straight through: dz = 6. A neuron below zero would pass back nothing.

    dz = dh if z > 0 else 0.0   # z = 2 > 0
    dz = 6
    
    relu(z) = max(0, z): slope 1 for z > 0, 0 for z < 0
    z < 0 would give dz = 0, and w1 would not change
  8. Back to the first weight

    z = w1 × x, so dw1 = dz × x = 6 × 1 = 6. Every weight now has its gradient, from one sweep backwards. That is backpropagation: the chain rule, applied from the loss back, reusing each result.

    dw1 = dz * x    # 6 * 1 = 6
    
    gradients:  dw1 = 6   dw2 = 8
    each line reused the one before it: nothing
    was worked out twice
  9. Check by nudging

    We can test it. Nudge w1 from 2 to 2.001 and run forward again: the loss grows from 4 to about 4.006. That is 0.006 for a 0.001 nudge, a slope of about 6, just as backpropagation said.

    w1 = 2.001:  loss = 4.00600225
    (4.00600225 - 4) / 0.001 = 6.00225
    backpropagation said dw1 = 6: they agree
    
    a gradient check; then w1 goes back to 2
  10. Step downhill

    Now gradient descent uses the gradients. Each weight moves against its gradient, scaled by a learning rate of 0.1: w1 = 2 − 0.1 × 6 = 1.4 and w2 = 1.5 − 0.1 × 8 = 0.7.

    w1 -= lr * dw1  # 2 - 0.1 * 6   = 1.4
    w2 -= lr * dw2  # 1.5 - 0.1 * 8 = 0.7
    
    a positive gradient means "this weight raises the
    loss", so the weight goes down, and the other way round
  11. Run it again

    A new forward pass: h = 1.4 and y = 0.7 × 1.4 = 0.98. The loss falls from 4 to 0.0004. Real training repeats forward, backward and update many thousands of times, over many examples.

    z = 1.4 * 1 = 1.4     h = 1.4
    y = 0.7 * 1.4 = 0.98
    loss = (0.98 - 1) ** 2 = 0.0004     (was 4)
    
    one training step = forward, backward, update

Quiz Unlocks after the last step.