B · Neural networks
Backpropagation
Compute every weight's gradient in one backward pass, by applying the chain rule from the loss back to the input.
Try it: B3 · Backpropagation
Intuition
To train a network with gradient descent you need, for every single weight, how much the loss would change if that weight changed a little. A network has thousands to billions of weights. Nudging each one and re-running the network to see what happens would take one forward pass per weight: hopeless.
Backpropagation gets all of them in one backward pass, for roughly the cost of one forward pass. It is the chain rule, organised cleverly. Start at the loss and ask "how much does the loss care about each output neuron?" Pass that answer back through the last layer's weights to learn how much it cares about each neuron in the layer before. Keep going. At every layer, the blame arriving at a neuron, times that neuron's input, is exactly the gradient of the weight that connects them.
The math
Take a layered network with pre-activations \(\mathbf{z}^{(l)} = W^{(l)}\mathbf{a}^{(l-1)} + \mathbf{b}^{(l)}\) and activations \(\mathbf{a}^{(l)} = f(\mathbf{z}^{(l)})\), where \(\mathbf{a}^{(0)} = \mathbf{x}\). Define the error of layer \(l\) as the gradient of the loss with respect to its pre-activations:
\[ \boldsymbol{\delta}^{(l)} = \frac{\partial L}{\partial \mathbf{z}^{(l)}} \]Backpropagation is four equations.
- Output error. \(\boldsymbol{\delta}^{(L)} = \nabla_{\mathbf{a}} L \odot f'(\mathbf{z}^{(L)})\). For a sigmoid or softmax output with cross-entropy this simplifies to \(\boldsymbol{\delta}^{(L)} = \mathbf{a}^{(L)} - \mathbf{y}\).
- Pass it back. \(\boldsymbol{\delta}^{(l)} = \big(W^{(l+1)\top}\boldsymbol{\delta}^{(l+1)}\big) \odot f'(\mathbf{z}^{(l)})\). Each neuron collects the errors of the neurons it feeds, weighted by the connecting weights, then scales by its own slope.
- Weight gradients. \(\dfrac{\partial L}{\partial W^{(l)}} = \boldsymbol{\delta}^{(l)}\,\mathbf{a}^{(l-1)\top}\): for a single weight, \(\partial L/\partial w_{jk} = \delta_j\, a_k\), the error at its end times the activation at its start.
- Bias gradients. \(\dfrac{\partial L}{\partial \mathbf{b}^{(l)}} = \boldsymbol{\delta}^{(l)}\).
With a mini-batch, the same equations run on matrices whose columns are the examples, and the weight gradients are averaged. Every step is a matrix product, which is why GPUs make training fast.
Cost
The backward pass performs about the same matrix products as the forward pass, twice (once for \(\boldsymbol\delta\), once for the weight gradients), so a full training step costs roughly three forward passes, independent of the number of weights. Compare with numerical differentiation: one extra forward pass per weight.
Worked example. One hidden ReLU neuron, one sigmoid output: \(x = 1\), \(w_1 = 0.5\), \(w_2 = 2\), no biases, target \(y = 0\), cross-entropy loss.
Forward: \(z_1 = 0.5\), \(a_1 = 0.5\), \(z_2 = 1\), \(a_2 = \sigma(1) = 0.731\).
Backward: \(\delta_2 = a_2 - y = 0.731\). \(\partial L/\partial w_2 = \delta_2 a_1 = 0.366\). \(\delta_1 = w_2\delta_2 \cdot \text{ReLU}'(z_1) = 2 \cdot 0.731 \cdot 1 = 1.462\). \(\partial L/\partial w_1 = \delta_1 x = 1.462\).
Update with \(\eta = 0.1\): \(w_2 = 1.963\), \(w_1 = 0.354\). Both moves lower the output toward the target 0.
Practical points
- Gradient checking. A hand-written backward pass is easy to get subtly wrong (training still "works", only worse). Compare a few gradients with finite differences.
- Memory. The backward pass needs every layer's activations from the forward pass. Gradient checkpointing stores only some and recomputes the rest, trading time for memory.
- Vanishing and exploding gradients. Equation 2 multiplies by a weight matrix and a slope at every layer; through many layers the product can shrink to nothing or blow up. ReLU-like activations, careful initialization, normalization layers and residual connections are all answers to equation 2.
- Through time. A recurrent network unrolled over a sequence is a deep network with shared weights; backpropagation through time sums the gradients from every step.
Is the brain doing this?
Probably not literally: backpropagation needs each synapse to know the error of neurons downstream, sent back through the same weights, and real neurons have no obvious channel for that. Whether the brain approximates it is an open research question. For artificial networks, though, nothing has come close to it.
History
The method is reverse-mode automatic differentiation (Linnainmaa, 1970), applied to neural networks by Paul Werbos in 1974. The 1986 Nature paper by David Rumelhart, Geoffrey Hinton and Ronald Williams showed hidden units learning useful internal representations, and made backpropagation the standard way to train neural networks, as it still is.
Connections
Builds on
- The Chain Rule & Computational GraphsBackpropagation is the chain rule run backwards through the network's graph, reusing each intermediate result.
- Gradient DescentIt computes the gradient that gradient descent then follows.
Further reading
- What is backpropagation really doing? (3Blue1Brown)The intuition first: how each weight's nudge flows back from the loss.
- How the backpropagation algorithm works, chapter 2 of Neural Networks and Deep Learning (Michael Nielsen)The four equations of backpropagation, derived step by step.
- Calculus on Computational Graphs: Backpropagation (Chris Olah)Why going backwards is so much cheaper than going forwards, on small graphs you can follow by hand.
- Neural Networks Part 3: gradient checks (Stanford CS231n)How the Gradient check card works, and the pitfalls of comparing with finite differences.