Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Matrix and vector calculus

Backpropagation as the Chain Rule on Computation Graphs

Backpropagation is the multivariable chain rule applied in reverse order over a computation graph. One forward pass stores values, and one backward pass hands each node its share of the blame. Build an autograd engine from scratch, then learn the memory tricks (checkpointing) and the pitfalls (vanishing gradients through time).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Passing the blame backwards

A factory has a line of machines. Each one takes the previous machine’s output and changes it. At the end, the product is wrong by some amount. Who should adjust, and by how much? Start at the end and walk backwards. Each machine knows how its output depends on its input, so it can pass back a share of the blame to the machine before it.

This backward blame-passing is backpropagation. It’s how every neural network learns.

Backpropagation, one node at a time
step 0/13: inputs set
a= 2b= -1c= 3u = a·bv = u + cw = v²s = 2cL = w + s
Blue = forward values (computed left to right), orange = gradients ∂L/∂node (computed right to left, each one using only its outputs' gradients and a local derivative). c feeds two nodes, so its gradient is the sum of both paths.
Why engineers care

A robot’s control policy might be a network with millions of adjustable numbers. Backpropagation computes how every one of them affects the robot’s mistake, in about the time it takes to run the network twice. Without it, modern AI training would be impossible.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Backpropagation passes blame…
  2. 2. Three machines in a row each double their input. The overall effect of the first input?
  3. 3. If one machine feeds two others, its blame is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

A computation graph

Every formula is a graph of simple operations. For f=(x+y)⋅zf = (x + y)\cdot z: s=x+ys = x + y, then f=s⋅zf = s\cdot z.

Forward pass (x=2x = 2, y=3y = 3, z=4z = 4): s=5s = 5, f=20f = 20.

Backward pass (start with ∂f/∂f=1\partial f/\partial f = 1):

  • f=szf = sz: ∂f/∂s=z=4\partial f/\partial s = z = 4 and ∂f/∂z=s=5\partial f/\partial z = s = 5.
  • s=x+ys = x + y: addition passes the gradient through unchanged, so ∂f/∂x=∂f/∂y=4\partial f/\partial x = \partial f/\partial y = 4.

Each node only needs its local derivative and the gradient arriving from its output.

📝 Quick check (Level 1)
0/3 answered
  1. 1. f = (x + y)z with x = 2, y = 3, z = 4. ∂f/∂z = ?
  2. 2. An addition node passes the gradient…
  3. 3. A multiplication node u = a·b sends ∂L/∂u times ___ to a.

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

The algorithm

  1. Forward: evaluate the nodes in topological order and store every intermediate value.
  2. Backward: set Lˉ=1\bar L = 1. In reverse topological order, for each node vv and each input pp, accumulate pˉ+=vˉ⋅∂v/∂p\bar p \mathrel{+}= \bar v\cdot\partial v/\partial p.

Here vˉ=∂L/∂v\bar v = \partial L/\partial v is called the adjoint. The +=\mathrel{+}= matters: gradients add where a value is used more than once (the multivariable chain rule, A3.2-4).

Node Local rule for inputsˉ\bar{\text{inputs}}
v=a+bv = a + b aˉ+=vˉ\bar a \mathrel{+}= \bar v, bˉ+=vˉ\bar b \mathrel{+}= \bar v
v=abv = ab aˉ+=vˉ b\bar a \mathrel{+}= \bar v\,b, bˉ+=vˉ a\bar b \mathrel{+}= \bar v\,a
v=tanh⁡av = \tanh a aˉ+=vˉ(1−v2)\bar a \mathrel{+}= \bar v(1 - v^2)
v=max⁡(0,a)v = \max(0,a) aˉ+=vˉ⋅1[a>0]\bar a \mathrel{+}= \bar v\cdot\mathbf 1[a>0]
y=Wx\mathbf y = W\mathbf x Wˉ+=yˉx⊤\bar W \mathrel{+}= \bar{\mathbf y}\mathbf x^\top, xˉ+=W⊤yˉ\bar{\mathbf x} \mathrel{+}= W^\top\bar{\mathbf y}
softmax-CE zˉ=p−y\bar{\mathbf z} = \mathbf p - \mathbf y (A3.3-4)

Costs: the backward pass takes about 1–3× the forward cost, and memory grows with the number of stored activations.

🐍 Exercise 1: build an autograd engine and learn XOR
Write a scalar node class with +, ×, power and tanh, each recording its local derivatives, and a backward() that runs the chain rule in reverse topological order. Check gradients add at forks, then train a tiny network on XOR.
📝 Quick check (Level 2)
0/3 answered
  1. 1. f = x·x + 3x at x = 4: ∂f/∂x = ?
  2. 2. Backward order is…
  3. 3. The ReLU gradient for a negative input is…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Why it’s correct

Claim: after processing node vv’s consumers, vˉ=∂L/∂v\bar v = \partial L/\partial v.

Proof by induction in reverse topological order. It holds for LL, since Lˉ=1\bar L = 1. For any vv, the variable LL depends on vv only through its consumers c1,…,ckc_1,\dots,c_k. By the multivariable chain rule, ∂L∂v=∑i∂L∂ci∂ci∂v=∑icˉi∂ci∂v\frac{\partial L}{\partial v} = \sum_i\frac{\partial L}{\partial c_i}\frac{\partial c_i}{\partial v} = \sum_i\bar c_i\frac{\partial c_i}{\partial v}, which is exactly what the accumulations compute. ∎

Why reverse order is the efficient one

For L:Rn→RL:\mathbb R^n\to\mathbb R, forward accumulation needs nn passes, one per input direction. Reverse accumulation gets all nn partials in one backward pass. Its cost is a constant multiple of evaluating LL, independent of nn (the “cheap gradient principle”; Griewank and Walther, 2008). With one scalar loss and millions of parameters, that’s the difference between feasible and impossible (Practice Code 4).

Vanishing and exploding gradients, again

Backprop multiplies local Jacobians along each path: hˉ0=J1⊤J2⊤⋯JT⊤hˉT\bar{\mathbf h}_0 = J_1^\top J_2^\top\cdots J_T^\top\bar{\mathbf h}_T. If their norms are typically <1<1, early adjoints vanish exponentially. If >1>1, they explode (A3.1-3). Recurrent networks suffer this over time steps (Practice Code 5). LSTMs, residual connections, normalization and careful initialization are the cures.

Exercise 3.1: For L = (ab + c)² + 2c, compute ∂L/∂c at a = 2, b = −1, c = 3

v=ab+c=1v = ab + c = 1. Path 1, through vv: 2v⋅1=22v\cdot1 = 2. Path 2, through 2c2c: 22. Total: 2+2=42 + 2 = 4. This is the demo’s default setting.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Backprop's correctness rests on…
  2. 2. Forward-mode passes needed for the full gradient of f: ℝ¹⁰⁰⁰ → ℝ?
  3. 3. Reverse-mode passes needed for the same?

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

Memory: gradient checkpointing

Storing every activation of a 100-layer network, or a long robot trajectory, can exhaust GPU memory. Checkpointing stores only every kk-th activation and recomputes each segment during the backward pass. With k≈depthk\approx\sqrt{\text{depth}}, memory falls from O(n)O(n) to O(n)O(\sqrt n) for one extra forward pass (Griewank, 1992; Chen et al., 2016). It’s standard in large-model training (torch.utils.checkpoint).

🐍 Exercise 2 (research): gradient checkpointing
Backprop through a 64-layer tanh network, storing all activations or checkpointing every 8 and recomputing segments. Check the gradients are identical and compare peak memory with the extra compute.

Backprop beyond neural networks

  • Backpropagation through time (Werbos, 1990) unrolls recurrent networks and sequence models. Truncated BPTT limits the horizon to control memory and exploding gradients.
  • Differentiable simulation and control. Backprop through physics simulators (Brax, Warp, MuJoCo MJX) gives exact policy and trajectory gradients for robots (Degrave et al., 2019; Hu et al., 2020, DiffTaichi). The adjoint method does the same through ODE solvers with constant memory (Chen et al., 2018, Neural ODEs). Long horizons bring chaotic, exploding gradients, an active research topic (Suh et al., 2022).
  • The adjoint method in engineering. Aerodynamic shape optimization of wings and rocket nozzles has used reverse-mode “adjoint” sensitivities since the 1980s (Jameson, 1988). It is the same mathematics as backprop, discovered independently.
  • Frameworks. PyTorch builds the graph dynamically during the forward pass (“define-by-run”). JAX traces functions into graphs and offers grad, vjp and jvp as composable transformations (A3.3-6).

References

  1. S. Linnainmaa (1970). MSc thesis, University of Helsinki. (Reverse-mode automatic differentiation.)
  2. D. Rumelhart, G. Hinton, R. Williams (1986). “Learning representations by back-propagating errors.” Nature, 323.
  3. P. Werbos (1990). “Backpropagation through time: what it does and how to do it.” Proceedings of the IEEE, 78(10).
  4. A. Griewank (1992). “Achieving logarithmic growth of temporal and spatial complexity in reverse automatic differentiation.” Optimization Methods and Software, 1.
  5. T. Chen, B. Xu, C. Zhang, C. Guestrin (2016). “Training deep nets with sublinear memory cost.” arXiv:1604.06174.
  6. A. Griewank, A. Walther (2008). Evaluating Derivatives, 2nd ed. SIAM.
  7. R. T. Q. Chen, Y. Rubanova, J. Bettencourt, D. Duvenaud (2018). “Neural ordinary differential equations.” NeurIPS.
  8. H. J. T. Suh, M. Simchowitz, K. Zhang, R. Tedrake (2022). “Do differentiable simulators give better policy gradients?” ICML.
  9. A. Paszke et al. (2019). “PyTorch: An imperative style, high-performance deep learning library.” NeurIPS.