101 puffs

Chapter 2 · Neural networksPuff 8 of 23

Passing the blame backwards

A tiny network is shown one example. The right answer is 0.10, but it says 0.70: too high. It has six weights, and every one of them played some part. To learn, it must decide how much each weight is to blame, and fix them all a little. How does it share out the blame?

Guess first

The network’s answer is too high. Who is to blame?

Play with it

Fig. 1 · Backpropagation A tiny network learning one example

Press Forward to run the example through the network, then Backward to share out the blame, then Update.

How it works

Tap a step to see it in the figure.

Check yourself

So what is backpropagation, mainly?

Go deeper: the mathThe chain rule, with this network’s live numbers

The network

Inputs x1 = 1.0 and x2 = 0.5, target y = 0.1. The hidden neurons use the sigmoid σ from puff 6, and the loss L is the squared miss:

h1 = σ(w1x1 + w2x2)
h2 = σ(w3x1 + w4x2)
ŷ = w5h1 + w6h2    L = (ŷ − y)²

The chain rule

A small change in w1 changes h1’s total, which changes h1, which changes ŷ, which changes L. The chain rule says the overall slope is the product of the slopes of each link:

∂L∂w1 = 2(ŷ − y) · w5 · h1(1 − h1) · x1

Here h(1 − h) is the slope of the sigmoid. Backpropagation computes the shared front part once, as a “delta” for each neuron, starting at the end:

δout = 2(ŷ − y) = ?
δ1 = δout · w5 · h1(1 − h1) = ?
δ2 = δout · w6 · h2(1 − h2) = ?

Then every weight’s blame is the delta at its far end times the value at its near end: ∂L/∂w5 = δouth1, ∂L/∂w1 = δ1x1, ∂L/∂w2 = δ1x2, and so on. The update is w ← w − η ∂L/∂w with learning rate η = 0.1.

Check it by nudging

You could get the same slope the slow way: nudge one weight a tiny bit, run forward again, and see how much the loss moved. On this page, nudging w1 up by 0.0001 gives a slope of ?; backpropagation says ?.

Why this matters

Nudging needs one extra forward pass per weight. With six weights that’s fine; with a billion it means a billion forward passes for a single step. Backpropagation gets every slope from one forward pass and one backward pass, and the backward pass costs about the same as the forward one, because it does the same multiplications in reverse and reuses each delta for every weight that feeds into it.

What this page leaves out

Real neurons also have a bias (puff 6); it gets blamed the same way, with blame equal to its neuron’s delta. Real training averages the blame over a batch of many examples instead of one. And deep-learning libraries do all of this automatically, for any network you write, which is called automatic differentiation. The idea was popularised for neural networks by Rumelhart, Hinton and Williams in 1986, building on earlier work on computing slopes backwards (Linnainmaa, 1970; Werbos, 1974).

Sources

  1. D. E. Rumelhart, G. E. Hinton and R. J. Williams, “Learning representations by back-propagating errors”, Nature 323, 533–536, 1986.
  2. I. Goodfellow, Y. Bengio and A. Courville, Deep Learning, MIT Press, 2016, section 6.5.
  3. A. Karpathy, micrograd, a tiny automatic-differentiation engine written for teaching.
  4. The network, its starting weights and its one example are made up for this page; every number is computed live.