Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Passing the blame backwards
When a network’s prediction is wrong, every weight shares some blame. Backpropagation works out how much: it starts at the output error and passes it back through the network, layer by layer. At each layer it reuses what was computed on the way forward, so every weight learns which way to move. It costs about as much as one more forward pass.
Every trained network, from a landing-site classifier to a learned controller, was trained with gradients computed by backpropagation. Knowing the equations lets you write custom layers, debug “my loss doesn’t go down” problems, check gradients numerically, and reason about memory, which is dominated by the activations that the backward pass needs.
- 1. Backpropagation computes…
- 2. The backward pass reuses…
- 3. Its cost is roughly…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.The four equations
Take a batch of B examples and layers Z_l = H_(l−1) W_l + b_l, with H_l = φ(Z_l) for hidden layers. Softmax with cross-entropy follows the last layer, and the loss is averaged over the batch. Write δ_l = ∂L/∂Z_l (shape B × n_l).
- Output error: δ_L = (P − Y)/B, where P is the softmax output and Y the one-hot labels. For probabilities (0.7, 0.2, 0.1) and (0.1, 0.1, 0.8) with true classes 0 and 2, this gives δ = [[−0.15, 0.1, 0.05], [0.05, 0.05, −0.1]]. Each row sums to 0.
- Weight gradient: ∂L/∂W_l = H_(l−1)ᵀ δ_l (shape n_(l−1) × n_l, the same as W_l).
- Bias gradient: ∂L/∂b_l = the sum of the rows of δ_l.
- Error back one layer: δ_(l−1) = (δ_l W_lᵀ) ⊙ φ′(Z_(l−1)). For ReLU, φ′ is 1 where the pre-activation was positive and 0 elsewhere.
Order. Run forward and store every H_(l−1) and Z_l. Then go backward from l = L to 1, applying equations 2–4.
Why softmax with cross-entropy gives P − Y. For one example, L = −log p_c with p = softmax(z). Then ∂L/∂z_k = p_k − 1[k = c]: the softmax Jacobian and the logarithm’s derivative cancel neatly.
- 1. ∂L/∂W_l has the same shape as…
- 2. For ReLU, φ′(z) for z = −0.3 is…
- 3. Each row of δ_L for softmax cross-entropy sums to…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Implementing and checking it
Gradient checking. Compare the backprop gradient with a finite difference, (L(θ + h) − L(θ − h))/(2h) (central). The relative difference ‖g_bp − g_fd‖/(‖g_bp‖ + ‖g_fd‖) should be around 10⁻⁷ or smaller in float64. A value around 10⁻² usually means a bug.
Exercise 1.
- For a 5 → 7 → 6 → 3 ReLU network with softmax cross-entropy, the backprop gradients matched central differences to a worst relative difference below 10⁻¹⁰ (about 3 × 10⁻¹¹; the last digits vary between machines).
- Using these gradients, 200 steps of gradient descent drove the loss on 8 examples from 1.329 to 0.0015. That success is itself a sanity check: a correct backward pass can overfit a tiny batch (E0-10).
Common bugs that gradient checks catch:
- forgetting the 1/B factor;
- transposing a weight in equation 4;
- applying φ′ to H instead of Z;
- reusing an overwritten activation.
loss = torch.nn.functional.cross_entropy(model(x), y)
loss.backward() # fills p.grad for every parameter
torch.autograd.gradcheck(f, inputs, eps=1e-6) # finite-difference check in float64- 1. A relative difference of about 10⁻² in a float64 gradient check usually means…
- 2. In exercise 1, the worst relative difference was…
- 3. Applying φ′ to H instead of Z is wrong because…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Derivation of equation 4. Z_l = H_(l−1) W_l + b_l and H_(l−1) = φ(Z_(l−1)). By the chain rule:
- ∂L/∂H_(l−1) = δ_l W_lᵀ, because each entry of H_(l−1) feeds n_l outputs through the row of W_l;
- ∂L/∂Z_(l−1) = ∂L/∂H_(l−1) ⊙ φ′(Z_(l−1)), because φ acts elementwise.
Cost.
- The backward pass does two matrix products per layer (for ∂W and ∂H) against one in the forward pass, so training costs about 3× the forward pass per step.
- It must store all activations, B Σₗ n_l numbers. Gradient checkpointing (C6-4) trades recomputation for memory.
Reverse versus forward mode. For a scalar loss and P parameters, reverse mode obtains all P derivatives in one backward pass. Forward mode would need P passes. This is why backpropagation made training large networks feasible (Linnainmaa, 1970; Werbos, 1974; Rumelhart, Hinton and Williams, 1986).
Finite differences.
- The central difference has truncation error O(h²), and its rounding error is about ε/h for machine epsilon ε. Balancing the two gives h ≈ ε^(1/3): about 6 × 10⁻⁶ in float64 and 5 × 10⁻³ in float32.
- The forward difference has truncation error O(h), so its best step is about √ε and it is less accurate.
- At a ReLU kink, a step that crosses the kink is meaningless.
Interview questions.
- Why is backprop not just “the chain rule”? It is the chain rule evaluated in reverse topological order, with intermediate results reused, which makes it O(forward cost) rather than exponential.
- Why do gradient checks use float64? In float32 the best achievable relative error is only about 10⁻⁴, which is too coarse to tell small bugs from rounding (exercise 2).
- 1. One training step costs about…
- 2. The best central-difference step is about…
- 3. Reverse mode is preferred for training because…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.How accurate can a gradient check be?
History.
- S. Linnainmaa (1970) described reverse-mode automatic differentiation.
- P. Werbos (1974) proposed it for neural networks.
- D. Rumelhart, G. Hinton and R. Williams (1986) popularised backpropagation.
- Y. LeCun et al. (1998, “Efficient BackProp”) gave practical guidance.
- Automatic differentiation frameworks (Theano 2010, TensorFlow 2015, PyTorch 2017) made manual derivation optional, but gradient checks remain the standard test for custom layers.
Exercise 2. Relative error of finite-difference derivatives for 40 first-layer weights of a 10 → 16 → 16 → 4 network (32 examples), against backprop in float64.
| setting | h = 10⁻¹ | 10⁻² | 10⁻³ | 10⁻⁴ | 10⁻⁵ | 10⁻⁶ | 10⁻⁷ | 10⁻⁸ | 10⁻¹⁰ |
|---|---|---|---|---|---|---|---|---|---|
| tanh, float64, central | 8.7e−3 | 8.8e−5 | 8.8e−7 | 8.8e−9 | 4.4e−10 | 4.2e−9 | 4.7e−8 | 4.8e−7 | 4.8e−5 |
| tanh, float64, forward | 9.1e−2 | 9.4e−3 | 9.4e−4 | 9.4e−5 | 9.4e−6 | 9.4e−7 | 1.1e−7 | 7.9e−7 | 7.1e−5 |
| tanh, float32, central | 8.7e−3 | 2.6e−4 | 2.0e−3 | 2.8e−2 | 0.20 | 2.8 | 29 | 150 | 1.0 |
| ReLU, float64, central | 0.20 | 6.0e−2 | 4.9e−3 | 3.0e−10 | 3.6e−10 | 4.8e−9 | 4.4e−8 | 3.8e−7 | 4.3e−5 |
- The error curves are V-shaped. On the left, truncation error falls like h² (central: 100× per decade) or h (forward: 10× per decade). On the right, rounding error grows like ε/h.
- The best steps matched theory. Central differences in float64 were best at h = 10⁻⁵ (4.4 × 10⁻¹⁰), near ε^(1/3). Forward differences were best at 10⁻⁷ (1.1 × 10⁻⁷), near √ε, and were 250× worse.
- float32 checks are coarse. The best error was 2.6 × 10⁻⁴, at h = 10⁻². With h = 10⁻¹⁰ the loss did not change at all in float32, so the estimate was 0 (relative error 1).
- ReLU kinks spoil large steps. At h = 10⁻³ the ReLU network’s error was 4.9 × 10⁻³, against 8.8 × 10⁻⁷ for tanh, because some perturbations crossed a kink. Once h was small enough to avoid kinks, it was as accurate as tanh.
Backpropagation in AI, robotics and aerospace
- Custom layers: physics-informed losses, differentiable rendering, or a learned Kalman filter need hand-derived or checked gradients.
- Memory planning: the stored activations for the backward pass dominate training memory on edge or onboard hardware.
- Differentiable simulation: backpropagating through simulators of vehicles or contact dynamics uses exactly these equations, through physics steps instead of layers.
References
- D. E. Rumelhart, G. E. Hinton, R. J. Williams (1986). “Learning representations by back-propagating errors.” Nature 323.
- S. Linnainmaa (1976). “Taylor expansion of the accumulated rounding error.” BIT 16.
- Y. LeCun, L. Bottou, G. Orr, K.-R. Müller (1998). “Efficient BackProp.” In Neural Networks: Tricks of the Trade.
- A. G. Baydin, B. A. Pearlmutter, A. A. Radul, J. M. Siskind (2018). “Automatic differentiation in machine learning: a survey.” JMLR 18.