Backpropagation as reverse-mode autodiff
Backpropagate gradients through a computational graph by hand using the local chain rule at each node. Explain why one backward pass costs about 2× the forward pass.
Builds on: Gradients and Jacobians, Weights, parameters and layers
What the animation says
One number, billions of knobs
Training tweaks every weight to push one number down: the loss. To do that, it needs to know how the loss changes when each weight moves. One derivative per weight, billions of them. The naive way: nudge one weight, rerun the whole model, and see what the loss did. That is one forward pass per weight. A billion weights means a billion passes, every single step. Backpropagation gets all of those derivatives exactly, for roughly the cost of two more forward passes. Here is how.
Messages flow backwards
Write the forward pass as a graph of simple operations. Each node remembers its inputs. a is two, b is three. c is a times b: six. d is c plus a: eight. The loss is d squared: sixty-four. Now go backwards. Each node receives a message: how much the loss moves per unit of its output. It multiplies that by its local derivative and passes it on. The loss starts at one. d gets two times d: sixteen. The plus node passes sixteen straight to c. The times node sends b sixteen times a: thirty-two. But a feeds two nodes. What is its gradient?
Uses add: the local rule
Sixty-four. Through d, a gets sixteen. Through c, it gets sixteen times b: forty-eight. They add. Check: the loss equals a squared times b-plus-one squared. Its slope in a is sixty-four, and in b, thirty-two. So the rule is local. Each input's gradient gets plus-equals: the output's gradient times the local derivative. Start the loss at one and everything else at zero. Visit nodes in reverse order, so each gradient is complete before it is passed on. Here is a new graph. Tap the value whose gradient is a sum of two messages.
One product becomes two
Real networks are mostly matrix products. A layer computes Y equals X times W. Going backwards, the same local rule gives two products of the same size. X transpose times Y-bar is the weight gradient, used to update W. Y-bar times W transpose goes back to the previous layer. So the backward pass costs about two forwards, and a full training step about three. But those backward products need something from the forward pass. What must stay in memory?
Check it in code
The activations. The weight gradient needs X, so every layer's input stays in memory until backward. That's the price. Here it is in NumPy. One forward product, then the square's local rule: Y-bar is two Y. These two lines are the backward products: the weight gradient, and the message to the earlier layer. To check, nudge one weight by a millionth and rerun. The two printed numbers agree. Backprop is the chain rule applied locally, from the loss backwards. Uses add, and all gradients cost about two forward passes.
Pass sensitivities backwards, node by node
The local rule
Backprop by hand
Why backward ≈ 2× forward
import numpy as np
rng = np.random.default_rng(0)
X = rng.normal(size=(4, 3)); W = rng.normal(size=(3, 2))
Y = X @ W # forward: 1 matrix product
L = (Y**2).sum() # scalar loss
Yb = 2 * Y # dL/dY (local rule of the square)
Wb = X.T @ Yb # backward product 1: weight gradient
Xb = Yb @ W.T # backward product 2: sent to earlier layer
eps = 1e-6; W2 = W.copy(); W2[0, 0] += eps
print(Wb[0, 0], (((X @ W2)**2).sum() - L) / eps) # should match
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses