B · Neural networks
The Chain Rule & Computational Graphs
Write a model as a graph of small operations; the derivative of the whole is the product of the small derivatives along the way.
Try it: B3 · Backpropagation
Intuition
Training needs the gradient: how much the loss changes when each weight changes a little. A deep network is a long chain of simple steps (multiply, add, apply an activation, compare with the target), so the question is how a small nudge at the start travels down the chain to the end.
The chain rule says: multiply. If a small nudge to \(a\) moves \(b\) three times as much, and a nudge to \(b\) moves \(c\) half as much, then a nudge to \(a\) moves \(c\) by \(3 \times \tfrac12 = 1.5\) times as much. Each step only needs to know its own local rate of change; the end-to-end rate is the product along the path.
A computational graph makes this mechanical. Draw every elementary operation as a node and every intermediate value as an edge. A forward pass computes the values left to right. A backward pass starts from the loss with gradient 1 and walks right to left, multiplying by each node's local derivative.
The math
One variable
If \(y = f(u)\) and \(u = g(x)\), then
\[ \frac{dy}{dx} = \frac{dy}{du}\cdot\frac{du}{dx} = f'(g(x))\,g'(x) \]Many paths
If \(x\) affects the output through several intermediate values \(u_1, \dots, u_k\), their contributions add:
\[ \frac{\partial y}{\partial x} = \sum_{j} \frac{\partial y}{\partial u_j}\,\frac{\partial u_j}{\partial x} \]In a graph: when an edge splits (a value is used twice), the gradients flowing back into it are summed.
Local derivatives of common nodes
| Node | Forward | Backward: gradient to each input, given \(g\) from the output |
|---|---|---|
| add | \(z = a + b\) | \(g\) to both: addition passes gradients through unchanged |
| multiply | \(z = a\,b\) | \(g\,b\) to \(a\), \(g\,a\) to \(b\): each gets the other's value |
| sigmoid | \(a = \sigma(z)\) | \(g\,a(1 - a)\) |
| ReLU | \(a = \max(0, z)\) | \(g\) if \(z > 0\), else 0: a gate |
| square | \(z = a^2\) | \(2a\,g\) |
| matrix product | \(\mathbf{z} = W\mathbf{a}\) | \(W^\top\mathbf{g}\) to \(\mathbf{a}\), \(\mathbf{g}\,\mathbf{a}^\top\) to \(W\) |
Worked example (the animation). Forward: \(u = xw = -2\), \(z = u + b = 1\), \(a = \sigma(1) = 0.731\), \(L = (a - y)^2 = 0.072\).
Backward: \(\partial L/\partial L = 1\). Square: \(\partial L/\partial a = 2(a - y) = -0.538\). Sigmoid: \(\partial L/\partial z = -0.538 \cdot 0.731 \cdot 0.269 = -0.106\). Add: \(\partial L/\partial u = \partial L/\partial b = -0.106\). Multiply: \(\partial L/\partial w = -0.106 \cdot x = -0.212\) and \(\partial L/\partial x = -0.106 \cdot w = 0.106\).
Check one by nudging: \(w = -0.99\) gives \(z = 1.02\), \(a = 0.7350\), \(L = 0.07024\), down by about 0.0021 from 0.07233, which matches \(-0.212 \times 0.01\).
Forward mode and reverse mode
The same products can be accumulated in two orders. Forward mode carries "how much does everything change per unit of this one input" from left to right; one pass gives the derivatives with respect to one input. Reverse mode carries "how much does the output change per unit of this value" from right to left; one pass gives the derivatives of one output with respect to every input. Training has one output (the loss) and millions of inputs (the weights), so reverse mode wins by a factor of millions. Reverse mode applied to a neural network is backpropagation.
The price of reverse mode is memory: the backward pass needs the forward pass's intermediate values (the multiply node needs \(a\) and \(b\), the sigmoid needs \(a\)), so they are all stored. This is why training a network takes far more memory than running it.
Automatic differentiation
Deep learning libraries (PyTorch, JAX, TensorFlow) record the graph as your code runs, with each operation's local
derivative built in. Calling backward() walks it in reverse. This is neither symbolic differentiation
(no formula is built up, so no expression swell) nor numerical differentiation (no finite differences, so no
rounding trouble): it is the chain rule applied to concrete numbers, exact up to floating point.
Pitfalls
- Long chains multiply many factors. Products of many numbers below 1 vanish and above 1 explode; see Activation functions and Initialization.
- Non-differentiable points. ReLU has no derivative at exactly 0; libraries simply pick 0 (or 1), which works in practice.
- Check your gradients. When writing a backward pass by hand, compare it with the finite difference \((L(w + \epsilon) - L(w - \epsilon))/2\epsilon\), as in the worked example.
History
The chain rule is Leibniz's (1676). Reverse-mode automatic differentiation was described by Seppo Linnainmaa in his 1970 master's thesis, and Paul Werbos proposed using it to train neural networks in 1974; it reached a wide audience with Rumelhart, Hinton and Williams in 1986. Modern autodiff libraries, starting with Theano (2007), made writing the backward pass by hand a thing of the past.
Connections
Builds on
- Deep NetworksA deep network is a long chain of functions, exactly the shape the chain rule differentiates.
Leads to
- BackpropagationBackpropagation is the chain rule run backwards through the network's graph, reusing each intermediate result.
Further reading
- Visualizing the chain rule and product rule (3Blue1Brown)The chain rule as nudges passing through a chain of functions, the intuition the whole Topic rests on.
- The spelled-out intro to neural networks and backpropagation: building micrograd (Andrej Karpathy)Builds a computational graph and its backward pass from scratch in about 100 lines of Python.
- Automatic differentiation in machine learning: a survey (Baydin et al., 2018)Forward against reverse mode, and why reverse mode (backpropagation) wins when one loss depends on millions of weights.