NeuroGenesys.
Optimization & GeneralizationAcademy • Foundations • Lesson 2 of 4

Backpropagation is the chain rule, organised

7 min read • By Will Schulz • NeuroGenesys AI Academy

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 LL sits at the end.

The chain rule says that if LL depends on a variable vv only through the nodes u1,…,umu_1,\dots,u_m that use vv, then

∂L∂v=∑j=1m∂L∂uj ∂uj∂v.\frac{\partial L}{\partial v} = \sum_{j=1}^{m} \frac{\partial L}{\partial u_j}\,\frac{\partial u_j}{\partial v}.

Call vˉ=∂L/∂v\bar v = \partial L/\partial v the adjoint of vv. 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:

  1. Forward pass. Evaluate the graph from inputs to loss, storing every intermediate value.
  2. Backward pass. Set Lˉ=1\bar L = 1. Visit nodes in reverse order and, for each one, add its contribution uˉ⋅∂u/∂v\bar u\cdot\partial u/\partial v into the adjoint of each parent vv.

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 x=2x=2, one target y=1y=1, and the network

z=w1x,h=ReLU(z),y^=w2h,L=12(y^−y)2,z = w_1 x,\qquad h = \mathrm{ReLU}(z),\qquad \hat y = w_2 h,\qquad L = \tfrac12(\hat y - y)^2,

with w1=0.5w_1 = 0.5 and w2=3w_2 = 3.

Forward: z=1z = 1, h=1h = 1, y^=3\hat y = 3, L=12(2)2=2L = \tfrac12(2)^2 = 2.

Backward, one local derivative at a time:

AdjointLocal ruleValue
y^ˉ\bar{\hat y}y^−y\hat y - y22
wˉ2\bar w_2y^ˉ⋅h\bar{\hat y}\cdot h2⋅1=22\cdot1 = 2
hˉ\bar hy^ˉ⋅w2\bar{\hat y}\cdot w_22⋅3=62\cdot3 = 6
zˉ\bar zhˉ⋅1[z>0]\bar h\cdot \mathbf{1}[z>0]6⋅1=66\cdot1 = 6
wˉ1\bar w_1zˉ⋅x\bar z\cdot x6⋅2=126\cdot2 = 12

Check numerically. Nudge w1w_1 to 0.5010.501. Then y^=3.006\hat y = 3.006 and L=12(2.006)2=2.012018L = \tfrac12(2.006)^2 = 2.012018. The loss rises by 0.0120180.012018 for a 0.0010.001 change, a slope of about 12.0212.02, which agrees with wˉ1=12\bar w_1 = 12. 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, wˉ2\bar w_2 needed the forward value hh and wˉ1\bar w_1 needed xx. The backward pass reuses stored activations, which is why training uses far more memory than inference. Second, zˉ\bar z passed through the ReLU's derivative, a gate that is 1 or 0. If zz had been negative, no gradient would have reached w1w_1 at all.

Check your understanding

In the example, change w1w_1 to −0.5-0.5 and keep everything else the same. What are LL, wˉ1\bar w_1 and wˉ2\bar w_2?

Show answer +

Now z=−1z=-1, so h=0h=0 and y^=0\hat y=0, giving L=12(0−1)2=0.5L=\tfrac12(0-1)^2=0.5. Then y^ˉ=−1\bar{\hat y} = -1 and wˉ2=y^ˉ⋅h=0\bar w_2 = \bar{\hat y}\cdot h = 0. Also hˉ=−3\bar h = -3, but the ReLU gate is closed (z<0z<0), so zˉ=0\bar z = 0 and wˉ1=0\bar w_1 = 0. Neither weight receives any gradient: this unit is "dead" for this input.

The matrix form

Real layers act on vectors. For a linear layer y=Wxy = Wx with incoming adjoint yˉ=∂L/∂y\bar y = \partial L/\partial y, the chain rule gives

Wˉ=yˉ x⊤,xˉ=W⊤yˉ.\bar W = \bar y\,x^\top, \qquad \bar x = W^\top \bar y .

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 Wˉ\bar W, one for xˉ\bar x) and runs on the same hardware.

For the usual output layer, softmax followed by cross-entropy, the combined adjoint on the logits is simply q−yq - y, 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 nn-layer network to O(n)O(\sqrt n) 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 nn layers, the adjoint is multiplied by nn 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, y=x+f(x)y = x + f(x), 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.

Check your understanding

A residual block computes y=x+f(x)y = x + f(x). Write xˉ\bar x in terms of yˉ\bar y and the Jacobian JfJ_f of ff, and explain why this helps deep networks.

Show answer +

xˉ=yˉ+Jf⊤yˉ=(I+Jf)⊤yˉ\bar x = \bar y + J_f^\top \bar y = (I + J_f)^\top \bar y. Even if JfJ_f is small, the identity term passes yˉ\bar y 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, Wˉ=yˉx⊤\bar W = \bar y x^\top and xˉ=W⊤yˉ\bar x = W^\top\bar y. 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

  1. 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 ↩

  2. 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 ↩

  3. 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 ↩

  4. 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 ↩

  5. 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 ↩

  6. 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 ↩

Leadership

A word from the founder — on why the brain is the blueprint, and why the next decade of intelligence will be built here.

"The brain is the universe's only proof that general intelligence is possible. We treat it as the blueprint."

From cortical interfaces to photonic silicon, every program at NeuroGenesys exists to answer one question: how does intelligence arise — and how do we build it safely.

Will Schulz

Will Schulz

Founder, NeuroGenesys • AW3 Technology