Curriculum · Unit 5 · Backprop and deep learning
How does a deep network work out which of its millions of weights to blame?
When a network gets an answer wrong, every one of its weights played some part. Backpropagation is the bookkeeping trick that hands each weight its exact share of the blame, in one sweep backward from the mistake. It's the engine under every model you've heard of, and it's small enough to watch by hand.
Nib says: Here's a network with just five neurons. Press Next to push one example through it, measure the mistake, send the blame backward and fix the weights, with every number worked out live on this page. Then scroll down and train it on XOR yourself with the +1, +10 and +100 buttons. The starting weights are ones I made up, so everything here is illustrative.
Made-up starting weights, sigmoid neurons, loss = ½(output − target)², learning rate 2. All numbers are computed by this page and rounded for display. Illustrative.
Try itTrain it on XOR
XOR means "one or the other, but not both". A single neuron can't learn it. This one has a hidden layer, so let's see if backprop can find the trick. Each step looks at all four rows and nudges every weight a little.
| x₁ | x₂ | h₁ | h₂ | output | want |
|---|
Same starting weights as above. Illustrative.
The idea in plain words
Training is a blame game. The network makes a guess, you measure how wrong it was with a single number called the loss, and then every weight needs to know: if I nudged myself up a little, would the loss go up or down, and by how much? That number is the weight's gradient. Once every weight has one, you move each a small step in the direction that lowers the loss, and repeat, many times.
For a single neuron, like the one in Unit 2, the blame is easy to share out. The trouble starts with hidden layers. A hidden neuron has no right answer of its own, so how can it know whether it helped or hurt? In 1986 David Rumelhart, Geoffrey Hinton and Ronald Williams showed a clear way through, using the chain rule from calculus. Start at the output, where the error is known. Pass it backward through each connection, multiplying by the weight and by how sensitive each neuron was, and every hidden neuron receives its share. Others, including Paul Werbos in the 1970s, had worked out the underlying math earlier. The 1986 paper showed that it makes hidden layers learn useful features of their own.
XOR is the classic test. One neuron can only draw one straight line between "yes" and "no", and XOR can't be split by one line. With two hidden neurons, the network can draw two lines and combine them. Train the toy above and look at the h₁ and h₂ columns: one hidden neuron usually ends up acting like "x₁ OR x₂", the other like "x₁ AND x₂", and the output learns "OR, but not AND". Nobody told it to. Backprop found it.
Then it went to work. In 1989 Yann LeCun and colleagues used backprop to train a convolutional network, which reuses the same small feature detectors across an image, to read handwritten ZIP code digits from real mail. In 1997 Sepp Hochreiter and Jürgen Schmidhuber's LSTM added gated memory cells, so a network could carry information along a sequence of words or sounds without losing it. But for years networks stayed fairly small, because data and computing power were scarce.
Two things changed that. ImageNet (2009) supplied millions of labeled photos, and GPUs, built for video games, turned out to be very good at a network's arithmetic. In 2012 AlexNet, a deep convolutional net trained on two gaming GPUs, won the ImageNet challenge by a huge margin, and the deep-learning boom began. Today's language models still learn by backprop, during pretraining and again during fine-tuning. There are just billions of weights instead of nine, and a great deal more compute.
From one paper to a boom
- 1986
Backprop
Rumelhart, Hinton and Williams show that sending the error backward lets hidden layers learn.
- 1989
Reading the mail
LeCun's team trains a convolutional network on handwritten ZIP code digits from US mail.
- 2009
ImageNet
Deng, Fei-Fei Li and colleagues publish a huge database of labeled images to learn from.
- 2012
AlexNet
A deep convolutional net trained on GPUs wins the ImageNet challenge: about 15% top-5 error against about 26% for the runner-up.
Along the way, in 1997, the LSTM gave networks a working memory for sequences. It powered speech recognition and translation until the transformer arrived in Unit 6.
Three things to keep
- 1
Backprop is the chain rule, applied backward. One sweep from the error back to the inputs gives every weight its gradient: which way to move, and how much it mattered.
- 2
Hidden layers invent their own features. Nobody tells the hidden neurons what to look for. In XOR they discover "OR" and "AND" by themselves.
- 3
The algorithm was ready long before the world was. Backprop dates to the 1980s. It took big labeled datasets and GPUs to make it explode in 2012.
Check yourself
A weight's gradient is negative. Should training make that weight bigger or smaller?
Bigger. A negative gradient means that raising the weight would lower the loss, and the update rule is new = old − learning rate × gradient, so subtracting a negative number raises it. You can see this on the output weights in step 5.
In step 4, why do the two weights leaving x₂ get no blame at all?
Because x₂ was 0 in that example. A weight's gradient is its neuron's share of the blame times the input flowing through it, and anything times 0 is 0. Those weights didn't affect this answer, so they aren't changed by it. Other rows of XOR will move them.
The backprop algorithm was published in 1986. Why did deep learning only take off around 2012?
Networks need lots of examples and lots of arithmetic. Large labeled datasets like ImageNet and fast parallel hardware like GPUs arrived later, and AlexNet showed what happened when you put them together.
Go deeper
Read
- ★ Paper · 1986 · Rumelhart, Hinton, Williams
Learning Representations by Back-propagating Errors
The four-page Nature letter that made backpropagation famous.
Read the paper ↗ - ★ Paper · 1989 · Yann LeCun et al.
Backpropagation Applied to Handwritten Zip Code Recognition
Convolutional networks trained with backprop, reading real digits.
Read the paper ↗ - ★ Paper · 2012 · Krizhevsky, Sutskever, Hinton
ImageNet Classification with Deep Convolutional Neural Networks (AlexNet)
The result that convinced the field: deep nets on GPUs, far ahead on ImageNet.
Read the paper ↗ - Paper · 1985 · Rumelhart, Hinton, Williams
Learning Internal Representations by Error Propagation
The long version, with worked examples, XOR among them.
Read the report ↗ - Paper · 1997 · Hochreiter, Schmidhuber
Long Short-Term Memory
Memory cells with gates, so a network can remember across a long sequence.
Read the paper ↗ - Paper · 2009 · Jia Deng, Fei-Fei Li et al.
ImageNet: A Large-Scale Hierarchical Image Database
The dataset that gave deep learning something big enough to chew on.
Read the paper ↗
Watch
- Video · about 2½ hours · Andrej Karpathy
The spelled-out intro to neural networks and backpropagation: building micrograd
Build this page's math yourself in about a hundred lines of Python. Start here.
Watch on YouTube ↗ - Video · about 2 hours · Andrej Karpathy
Building makemore, part 1: bigrams
A tiny model that invents names one letter at a time.
Watch on YouTube ↗ - Video · about 75 minutes · Andrej Karpathy
Building makemore, part 2: MLP
Swap the letter counts for a small neural network trained with backprop.
Watch on YouTube ↗ - Video · about 2 hours · Andrej Karpathy
Building makemore, part 3: activations, gradients, BatchNorm
Why deep networks are tricky to train, and the fixes.
Watch on YouTube ↗ - Video · about 2 hours · Andrej Karpathy
Building makemore, part 4: becoming a backprop ninja
Backprop by hand through every layer, like step 4 above but much bigger.
Watch on YouTube ↗ - Video · about 1 hour · Andrej Karpathy
Building makemore, part 5: building a WaveNet
A deeper, tree-shaped model, one step from the transformer.
Watch on YouTube ↗
Explore
- Unit 2 · A neuron that learns
How can a pile of simple on/off units learn anything?
The single neuron, and why it can't do XOR on its own.
Back to Unit 2 - Interactive timeline
Deep learning wakes up
AlexNet, GPUs and AlphaGo, with backprop, LeCun and LSTM in the era just before.
Open the timeline - 🎓 Lecture hall · free
Lectures for this unit
Backpropagation · Reading Handwriting · The Widest Street · A Memory That Lasts · A Crowd of Trees · A Picture for Every Word · The Night the Field Changed Its Mind.
See the lecture - Next · Unit 6
How does each word decide which other words matter?
Attention, the transformer, and the causal mask behind every GPT.
On to Unit 6