Gradient descent needs the gradient of one scalar loss with respect to every parameter, and a large model has billions of them. Backpropagation computes all of those derivatives for roughly the cost of a couple of extra forward passes. It does so by applying the ordinary chain rule in a particular order and saving the right intermediate results along the way.
The computational graph
Any neural network computes its loss as a composition of simple operations: matrix multiplies, additions, elementwise nonlinearities, a softmax, a log. Write this as a directed acyclic graph. Inputs and parameters are the leaves, each node applies one operation to its parents, and a single scalar sits at the end.
The chain rule says that if depends on a variable only through the nodes that use , then
Call the adjoint of . The formula says that a node's adjoint is the sum of its children's adjoints, each multiplied by a local derivative that the node's own operation determines. Backpropagation is the algorithm that evaluates this recursion efficiently:
- Forward pass. Evaluate the graph from inputs to loss, storing every intermediate value.
- Backward pass. Set . Visit nodes in reverse order and, for each one, add its contribution into the adjoint of each parent .
Each node is visited once in each direction, and each local derivative costs about as much as the operation itself. The whole gradient therefore costs a small constant multiple of one forward evaluation, no matter how many parameters there are. In the automatic-differentiation literature this is called reverse-mode differentiation, and backpropagation is its use for neural networks.[1]
Worked example: a two-weight network
Take one input , one target , and the network
with and .
Forward: , , , .
Backward, one local derivative at a time:
| Adjoint | Local rule | Value |
|---|---|---|
Check numerically. Nudge to . Then and . The loss rises by for a change, a slope of about , which agrees with . Comparing against finite differences like this, called gradient checking, is the standard test for a hand-written backward pass.
Two things in the table generalise. First, needed the forward value and needed . The backward pass reuses stored activations, which is why training uses far more memory than inference. Second, passed through the ReLU's derivative, a gate that is 1 or 0. If had been negative, no gradient would have reached at all.
In the example, change to and keep everything else the same. What are , and ?
Show answer +Hide answer −
Now , so and , giving . Then and . Also , but the ReLU gate is closed (), so and . Neither weight receives any gradient: this unit is "dead" for this input.
The matrix form
Real layers act on vectors. For a linear layer with incoming adjoint , the chain rule gives
The weight gradient is an outer product of "error arriving from above" and "activation from below". The error passed further down is multiplied by the transpose of the forward matrix. A minibatch replaces vectors with matrices, and both rules become matrix multiplications. That is why the backward pass costs about twice the forward pass (one multiply for , one for ) and runs on the same hardware.
For the usual output layer, softmax followed by cross-entropy, the combined adjoint on the logits is simply , prediction minus target, as derived in Cross-entropy and KL divergence.
Why reverse mode, and who found it
The chain rule can also be applied forward: push the derivative of every intermediate with respect to one input through the graph alongside the values. That costs one pass per input. Reverse mode costs one pass per output. Training has one scalar output and up to billions of inputs, so reverse mode wins by the same factor.
Reverse-mode differentiation was published by Seppo Linnainmaa in his 1970 master's thesis, in the setting of rounding-error analysis.[2] Its use to train networks with hidden units was popularised by Rumelhart, Hinton and Williams in 1986. They showed that hidden units trained this way come to represent useful features of the task, something the earlier perceptron procedure could not do.[3] Modern frameworks implement reverse mode generically ("autograd"). Each operation registers its local derivative rule, and the framework records the graph during the forward pass and replays it backwards.
Where backpropagation struggles
Memory. Storing every activation for the backward pass dominates memory in large models. Activation checkpointing stores only some activations and recomputes the rest during the backward pass. Chen et al. reduced memory for an -layer network to at the cost of about one extra forward pass. One 1,000-layer network went from 48 GB to 7 GB at about 30% more run time.[4] Large-model training uses variants of this routinely.
Vanishing and exploding gradients. Through layers, the adjoint is multiplied by Jacobians. If their typical gain is below 1, the signal shrinks exponentially; if it is above 1, it blows up. Bengio, Simard and Frasconi showed that this makes learning long-range dependencies with gradient descent increasingly hard as the range grows.[5] Residual connections, , add an identity term to every layer's Jacobian, which gives gradients a path that does not shrink. This property helped residual networks train at depths of over a hundred layers,[6] and every transformer uses it. Careful initialisation, normalisation layers and gradient clipping address the same problem.
A residual block computes . Write in terms of and the Jacobian of , and explain why this helps deep networks.
Show answer +Hide answer −
. Even if is small, the identity term passes through unchanged, so gradients reaching early layers do not shrink exponentially with depth.
Key takeaways
- Backpropagation is reverse-mode application of the chain rule over a computational graph: a forward pass stores values, and a backward pass accumulates adjoints from loss to leaves.
- The full gradient costs a small constant multiple of one forward pass, independent of the number of parameters. Reverse mode suits one scalar output with many inputs.
- For a linear layer, and . Activations must be stored, which makes memory the main cost. Checkpointing trades compute for memory.
- Products of Jacobians can vanish or explode. Residual connections, normalisation and initialisation keep gradient flow usable at depth.
Sources
-
A. G. Baydin, B. A. Pearlmutter, A. A. Radul and J. M. Siskind (2018). "Automatic Differentiation in Machine Learning: a Survey." Journal of Machine Learning Research 18(153), 1–43. arXiv:1502.05767. https://arxiv.org/abs/1502.05767 ↩
-
A. Griewank (2012). "Who Invented the Reverse Mode of Differentiation?" Documenta Mathematica, Extra Volume: Optimization Stories, 389–400. https://emis.univie.ac.at/journals/DMJDMV/vol-ismp/52_griewank-andreas-b.html ↩
-
D. E. Rumelhart, G. E. Hinton and R. J. Williams (1986). "Learning representations by back-propagating errors." Nature 323(6088), 533–536. https://doi.org/10.1038/323533a0 ↩
-
T. Chen, B. Xu, C. Zhang and C. Guestrin (2016). "Training Deep Nets with Sublinear Memory Cost." arXiv:1604.06174. https://arxiv.org/abs/1604.06174 ↩
-
Y. Bengio, P. Simard and P. Frasconi (1994). "Learning long-term dependencies with gradient descent is difficult." IEEE Transactions on Neural Networks 5(2), 157–166. https://doi.org/10.1109/72.279181 ↩
-
K. He, X. Zhang, S. Ren and J. Sun (2015). "Deep Residual Learning for Image Recognition." arXiv:1512.03385. https://arxiv.org/abs/1512.03385 ↩
