Foundations to Frontier
Tests
E. Deep Learning › E0 Core Concepts (concept pages)

Backpropagation: Full Derivation

Backpropagation computes the gradient of a network's loss with respect to every weight in one backward pass, at a cost comparable to the forward pass. Applying the chain rule layer by layer in matrix form gives four equations: the error at the output (for softmax with cross-entropy, simply probabilities minus one-hot labels), the error carried back through each weight matrix and activation derivative, and the weight and bias gradients as products of stored activations and errors. This page derives these equations for a multilayer perceptron, shows the shapes, explains why reverse mode is efficient, and covers gradient checking with finite differences, including how to choose the step size. In the exercises you implement the backward pass of a ReLU network with softmax cross-entropy, verify it against finite differences and train with it, then map how the accuracy of gradient checks depends on step size, difference formula, precision and ReLU kinks.

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.

Backpropagation step by step
backward 3: δ₁ = ∂L/∂h ⊙ ReLU′(z₁), ∂L/∂W₁ = xᵀδ₁
w = 0.80, ∂L/∂w = -0.2919w = -0.50, ∂L/∂w = 0.0000w = 0.30, ∂L/∂w = 0.0000w = 0.20, ∂L/∂w = -0.1459w = 0.90, ∂L/∂w = 0.0000w = -0.70, ∂L/∂w = 0.0000w = 1.00, ∂L/∂w = -0.1824w = -0.60, ∂L/∂w = 0.1824w = -0.80, ∂L/∂w = 0.0000w = 0.70, ∂L/∂w = 0.0000w = 0.50, ∂L/∂w = 0.0000w = 0.40, ∂L/∂w = 0.00001.000.50δ -0.292δ 0.000δ 0.000δ -0.182δ 0.182loss 0.201 (true class 0)
∂L/∂W₁[0,0]: backprop -0.29188084 · central difference -0.29188084 · relative error 4.0e-11
Forward steps fill in values; backward steps send δ = ∂L/∂z from the output back toward the input, reusing the stored forward values. Edge thickness then shows each weight's gradient (hover for numbers). A hidden unit with a negative sum passes no gradient (its δ is 0), and neither do its incoming weights. The check below agrees best for h around 10⁻⁵. Larger h suffers truncation error and smaller h rounding error, and if a ReLU sum sits near 0, a large h steps across the kink.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Backpropagation computes…
  2. 2. The backward pass reuses…
  3. 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).

  1. 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.
  2. Weight gradient: ∂L/∂W_l = H_(l−1)ᵀ δ_l (shape n_(l−1) × n_l, the same as W_l).
  3. Bias gradient: ∂L/∂b_l = the sum of the rows of δ_l.
  4. 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.

📝 Quick check (Level 1)
0/3 answered
  1. 1. ∂L/∂W_l has the same shape as…
  2. 2. For ReLU, φ′(z) for z = −0.3 is…
  3. 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
🐍 Exercise 1: backpropagation for an MLP
Implement the forward pass with stored activations and the matrix-form backward pass for a ReLU network with softmax cross-entropy; check every gradient with central differences and train on a tiny batch.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A relative difference of about 10⁻² in a float64 gradient check usually means…
  2. 2. In exercise 1, the worst relative difference was…
  3. 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).
📝 Quick check (Level 3)
0/3 answered
  1. 1. One training step costs about…
  2. 2. The best central-difference step is about…
  3. 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.
🐍 Exercise 2 (research): gradient-check accuracy
Measure the relative error of central and forward finite differences against backprop for step sizes 10⁻¹ to 10⁻¹⁰, in float64 and float32, for tanh and ReLU networks.

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

  1. D. E. Rumelhart, G. E. Hinton, R. J. Williams (1986). “Learning representations by back-propagating errors.” Nature 323.
  2. S. Linnainmaa (1976). “Taylor expansion of the accumulated rounding error.” BIT 16.
  3. Y. LeCun, L. Bottou, G. Orr, K.-R. Müller (1998). “Efficient BackProp.” In Neural Networks: Tricks of the Trade.
  4. A. G. Baydin, B. A. Pearlmutter, A. A. Radul, J. M. Siskind (2018). “Automatic differentiation in machine learning: a survey.” JMLR 18.