Backpropagation: Sharing Out the Blame

How does a network with a hidden layer learn? Work out how much each weight is to blame for the error, starting at the output and passing it back, one link at a time.

Machine Learning · beginner · 12 min · 25 steps · free

What you'll learn

backprop.py

import math

# Backpropagation: share out the blame for the error, from the output back to every weight.
x, target = 1.0, 1.0
w1, w2 = 0.5, 0.5   # input -> hidden, hidden -> output
lr = 1.0

for step in range(2):
    h = round(1 / (1 + math.exp(-w1 * x)), 3)   # hidden neuron (sigmoid)
    out = round(w2 * h, 3)                       # output neuron
    loss = round((out - target) ** 2, 3)
    d_out = round(2 * (out - target), 3)         # how the loss changes with out
    d_w2 = round(d_out * h, 3)                   # chain rule, through out = w2 * h
    d_h = round(d_out * w2, 3)                   # blame passed back to the hidden neuron
    d_w1 = round(d_h * h * (1 - h) * x, 3)       # through the sigmoid, to w1
    w1, w2 = round(w1 - lr * d_w1, 3), round(w2 - lr * d_w2, 3)
    print("step", step, "loss", loss)

Step by step

  1. This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
  2. One training example: input 1, and the network should output 1.
  3. Two weights: w1 from the input to a hidden neuron, w2 from the hidden neuron to the output.
  4. Learning rate.
  5. step is created with the value 0.
  6. Forward pass, part 1: the hidden neuron applies a sigmoid to w1 × x: h = 0.622.
  7. Part 2: the output is w2 × h = 0.311. Far from the target 1.
  8. Loss: (0.311 − 1)² = 0.475.
  9. Backward pass. Start at the end: how does the loss change if out changes? 2 × (out − target) = −1.378. Negative: a bigger out would lower the loss.
  10. w2's share: out = w2 × h, so a change in w2 changes out by h. Gradient = −1.378 × 0.622 = −0.857.
  11. Pass the blame back to h: out changes by w2 for each change in h. −1.378 × 0.5 = −0.689.
  12. Through the sigmoid, whose slope is h × (1 − h), and on to w1: −0.162.
  13. Update both weights: w1 = 0.662, w2 = 1.357.
  14. It prints step 0 loss 0.475.
  15. step changes from 0 to 1.
  16. h changes from 0.622 to 0.66.
  17. Forward again: out = 0.896, much closer to 1.
  18. Loss 0.011, down from 0.475 in a single step.
  19. d_out changes from -1.378 to -0.208.
  20. d_w2 changes from -0.857 to -0.137.
  21. d_h changes from -0.689 to -0.282.
  22. d_w1 changes from -0.162 to -0.063.
  23. w1 changes from 0.662 to 0.725. w2 changes from 1.357 to 1.494.
  24. It prints step 1 loss 0.011.
  25. The loss fell from 0.475 to 0.011. Real networks run exactly this, forward then backward, over millions of weights and examples. Libraries like PyTorch work out every gradient for you, using this same chain rule.

Common mistakes

Best practices

Key takeaways

Check yourself

Why is it called BACKpropagation?

Answer: Gradients are worked out from the output back towards the input. Each earlier gradient reuses the one after it, so you start at the loss and move back.

For out = w2 × h, how does out change when w2 changes by a small amount?

Answer: By h times that amount. The slope of w2 × h with respect to w2 is h.

·

More in Deep Learning