Carrying the Error Down — Backpropagation
Chapter 1 showed that the direction to move a weight is set by , the gap between prediction and truth. That was with one layer. Stack layers and only the top one knows the gap: the weights below never touch the output, so they have no way of knowing how wrong they were.
How does the gap get down there? Chapter 0-2 already answered it. Multiply along a chain, add across a branch — the chain rule, and nothing else. This chapter writes that rule in the shape of layers, checks it against numerical differentiation, and then measures the case where it fails to arrive (vanishing gradients). No new principle appears.
1. The forward pass
Write layers as
with the input and the output. is applied componentwise (Chapter 0-2, case (b)).
The loss is a function of the output alone, and there are sets of weights. What we need is the gradient with respect to every .
2. The chain rule in the shape of layers
Define one key quantity: how much the loss moves when that layer's input moves.
At the output layer, the form from Chapter 1 carries over directly (for cross-entropy with softmax):
Now descend. The only route by which reaches the loss is through into , so applying equation (7) of Chapter 0-2 ():
That single line is all of backpropagation. Read it in two halves.
- Multiplying by redistributes the layer above's error in proportion to the weights. Routes that mattered going forward get more of the error coming back.
- The multiplies by how responsive the activation was at that point. Where the slope is flat (saturated), almost nothing gets through.
Once is in hand, the weight gradients are one product:
Here is the value from the forward pass, which is why those intermediates cannot be thrown away (section 5).
3. Checking that it is actually right
The derivation is short, and a wrong sign or a missing transpose leaves its shape intact. Implementations that run but are wrong are easy to write, so check it mechanically, with the central difference from Chapter 0-2 equation (2).
Measured: a small network of 4 inputs, hidden layers of 6 and 5, 3 outputs, loss , comparing equation (4) against the central difference () for every one of the 83 parameters.
| activation | parameters checked | largest difference |
|---|---|---|
| sigmoid | 83 | |
| tanh | 83 | |
| ReLU | 83 |
They agree. The size of the difference is the central difference's own error (order ), not backpropagation's.
4. Why "backward"
As Chapter 0-2 showed, matrix products can be taken in any order, and the order changes the cost. The loss is one output; the parameters number in the millions. In that shape, multiplying from the left — descending from the output side — is the cheap one.
For the same three stages (, , ), left-to-right costs multiplications against — 9.95 times. Going left, what you hold is always a row vector (that is ), and no large matrix is ever formed.
The name "backpropagation" describes the order of computation, nothing more. The differentiation rule is the same chain rule either way; the only choice made is which end to multiply from. For a problem with one input and millions of outputs, forward would be the cheaper direction (forward-mode automatic differentiation).
5. The price — keeping every intermediate
Equation (4) contains . Descending needs values computed on the way up. So during the forward pass, nothing can be discarded.
Batch 32, width 1024, 24 layers: . Raise the batch to 256 and it is 24.00 MB (eight times). In a real model this accumulates across every kind of layer, so training memory is usually set by stored activations rather than by model size. That is why memory runs out the moment you raise the batch size.
6. Sometimes it does not arrive — vanishing gradients
Repeat equation (3) times and the reaching layer 1 is a product of (weight) × (activation slope) over every layer. If the per-layer factor sits below 1, depth shrinks it exponentially — the same shape seen in the Chapter 0-2 figure.
Measured at width 128 and 20 layers, with weights initialised as , calling the gain:
| activation | gain | layer 1's ÷ last layer's |
|---|---|---|
| sigmoid | 1 | |
| tanh | 1 | 0.933 |
| tanh | 2 | |
| ReLU | 1 | |
| ReLU | 1.37 |
(The figure uses width 64 and draws its own weights, so its numbers do not match the table exactly. Measured: ReLU at reads in the table and 0.0018 in the figure; sigmoid at , against ; tanh at is further apart, 0.933 against 0.442. What matches is the pattern, not any single measured value.)
A common misreading: "gradients vanish because of the activation function"
Half right. Look at rows two and three above. Same tanh, same 20 layers, gain moved from 1 to 2 — and becomes . Not vanishing but exploding. Lower the gain instead and it vanishes.
| tanh gain | ratio (layer 1 ÷ last) |
|---|---|
| 0.5 | |
| 1.0 | 0.933 |
| 1.5 | 9.31 |
| 2.0 | 87.7 |
| 3.0 |
ReLU behaves the same way: at it shrinks to , and at it returns to 1.37. So "ReLU does not vanish" is wrong too. What governs this is not the activation but the per-layer product of (weight size) × (activation slope). The is exactly the factor that compensates for ReLU zeroing out half its inputs.
That said, the activation does have properties of its own. The derivative of sigmoid peaks at , so try to recover that with gain:
| sigmoid gain | ratio |
|---|---|
| 1 | |
| 2 | |
| 4 | |
| 8 |
It improves, but even at eight it does not reach 1. Raising the gain also saturates it — inputs grow and push into its flat region — so what you win you give back. That squeeze is why sigmoid is hard to stack deeply.
Exercises
The answers are folded away. Work them first.
Exercise 1. About equation (3). (a) What are the dimensions of , writing for layer 's width? (b) Why is there a transpose? (c) Why is a Hadamard product rather than a matrix product?
Show the answer
(a) . Going forward is (outputs × inputs), so transposing swaps them. Multiplying gives — the shapes agree.
(b) Because the Jacobian is being multiplied from the left. The Jacobian of is itself (Chapter 0-2, case (a)), and the chain rule gives . Rewrite that as a column vector and the transpose appears. It is not a new rule, just a row vector written vertically.
(c) Because a componentwise activation has a diagonal Jacobian. does not involve for , so the off-diagonal entries are zero, and multiplying by a diagonal matrix equals multiplying elementwise. No matrix is needed — one vector does it.
Exercise 2. In a 20-layer network, shrinks by a factor of 0.8 per layer on average. (a) How large is layer 1's relative to the last layer's? (b) What about 40 layers? (c) If the per-layer factor improved to 0.95, what is the 20-layer ratio?
Show the answer
(a) (19 hops between layers) — roughly one hundredth.
(b) . Doubling the depth squares the ratio. Depth rides in the exponent, so a tiny per-layer difference becomes orders of magnitude when deep.
(c) . Going from 0.8 to 0.95 per layer — a small change — turns into , about 26 times better. Initialisation and normalisation earn their keep by pulling the per-layer factor toward 1.
Exercise 3. You are checking a backward pass you wrote. (a) How? (b) How close is close enough? (c) Why can this not be used for training itself?
Show the answer
(a) Compare against central differences (Chapter 0-2, equation (2)). Move one parameter by , measure the loss twice, and compare against the backward pass. Do it for every parameter — one agreeing parameter will not catch a wrong transpose (the text checks all 83).
(b) Around with . The measurements in the text came in at to . Shrinking too far makes cancellation worse, not better, so nothing below about . With a kinked function like ReLU, parameters whose perturbation straddles the kink will disagree — that is not an implementation error.
(c) The counts do not compare. Each parameter costs two forward passes, so parameters cost . Backpropagation produces every gradient in a small multiple of one forward pass. At a hundred million parameters that is two hundred million passes against a handful.
What this chapter does not say
- How automatic differentiation is implemented — representing the graph, registering a backward rule per operation — is not covered. Equations (3) and (4) are specific to fully connected layers; convolution and attention take other forms, derived the same way from the chain rule.
- Remedies for vanishing and exploding gradients (residual connections, normalisation layers, gradient clipping, initialisation schemes) appear by name only; they are Chapter 5 onward. The measurements here stop at what happens, not how to fix it.
- The numbers are single measurements on synthetic data with one fixed initialisation. Change the depth, width or activation and they change. What does not change is the shape: the per-layer factor acts through an exponent.
- The memory estimate counts activations only — not gradients, optimiser state (twice the weights for Adam) or temporary buffers.
- Every number was computed and checked (central-difference agreement across all 83 parameters for three activations, the per-layer at 20 layers, the gain sweeps for tanh and sigmoid, and the memory estimate).
Comments
Sign in to comment