Lesson 4: Backpropagation and the Chain Rule
Gradient descent needs the gradient — the slope of the loss with respect to every weight in the network. A network can have thousands or millions of weights, so computing each slope separately, by wiggling one weight at a time and re-measuring the loss, would be far too slow. Backpropagation is the algorithm that computes all of those slopes in a single efficient pass, and it is the reason training large networks is practical at all.
The key idea is the chain rule from calculus. The loss depends on the output, the output depends on the last layer, and the last layer depends on the layer before it, all the way back to the first weights. The chain rule says that the effect of an early weight on the loss is the product of the effects along that chain: how the loss changes when the output changes, times how the output changes when the last layer changes, times how that layer changes when the earlier weight changes. Backpropagation simply applies this idea systematically, layer by layer, from the output backwards.
The Backward Pass
Training therefore has two passes. The forward pass runs an example through the network and records what every neuron computed, along with the loss. The backward pass then starts at the loss and walks backwards, asking at each layer how much each of its inputs contributed to the error, and passing that blame further back. By the time the walk reaches the first layer, every weight knows exactly how it should change. The weights are then updated by one small gradient-descent step, and the next example begins.
This is also why activation functions matter for training, not just for what the network can represent. The backward pass multiplies by the slope of each activation function, so if that slope is near zero, the learning signal fades as it travels backwards — a problem called the vanishing gradient. It is one reason ReLU became popular: its slope is 1 for positive inputs, so the signal passes through unchanged, while sigmoid's slope is small almost everywhere and lets the signal shrink layer by layer.
Blame, Fairly Shared
A useful way to think about backpropagation is as a fair system for sharing blame. When a network misclassifies a picture, many weights contributed to the mistake, but not equally: a weight in the last layer had a direct effect, while a weight in the first layer acted through many intermediate steps. Backpropagation works out each weight's share of the blame exactly, using the chain rule, instead of guessing. That precision is what lets gradient descent improve all the weights at once, in the right proportions.
You do not need to work out these derivatives by hand — libraries do that for you, as you will see when you build a network in PyTorch. But knowing what backpropagation is doing will change how you debug: when training stalls, you will know to ask whether the gradients are vanishing, whether the learning rate is wrong, or whether the loss itself is the problem, rather than guessing blindly.
Backpropagation in a Tiny Network
A concrete example makes the chain rule feel real. Imagine a network with one input, one hidden neuron and one output, and suppose the final answer is too high by 0.4. The backward pass asks, step by step, how much each part contributed. The output neuron's activation function has a slope of, say, 0.5 at this point, so half the error passes back to the weighted sum. The weight connecting the hidden neuron to the output is 2.0, so the blame passed to the hidden neuron is doubled. The hidden neuron's own activation slope is 0.3, so the blame shrinks again before reaching the first weight. Each multiplication is one link of the chain, and the product of all of them is the gradient for that first weight — the exact amount by which changing it would change the loss.
Two lessons fall out of this small example. First, the gradient for an early weight is the product of many numbers, so if several of them are small, the product is tiny — the vanishing gradient, in miniature. Second, every weight's update is computed from the same backward walk, which is why backpropagation is so efficient: one pass, all gradients, no repeated work. Libraries perform this walk for you, but knowing what it computes — and why long chains shrink the signal — will guide every debugging decision you make later.
Watch & Learn
One video to watch for this unit, from a trusted educator.
- But what is a neural network? | Deep learning chapter 1 — 3Blue1Brown
Grant Sanderson builds the picture of neurons, layers and activations from scratch, using handwritten digits — the same example this unit uses.
Key Terms
- Backpropagation
- The algorithm that computes the gradient of the loss with respect to every weight, working backwards from the output.
- Chain rule
- The calculus rule for the derivative of a chain of functions: multiply the derivatives along the chain.
- Forward pass
- Running an example through the network to compute its output and the loss.
- Backward pass
- Walking from the loss back through the layers, computing how each weight should change.
- Vanishing gradient
- When the learning signal shrinks as it travels backwards, so early layers barely learn.
- Derivative
- The slope of a function: how much its output changes when its input changes a little.
Discussion Questions
- Why would wiggling each weight one at a time to find the gradient be too slow for a large network?
- If the backward pass multiplies by each activation function's slope, what happens when many of those slopes are tiny?
- Backpropagation shares blame exactly. When might exact blame still lead training astray?
Practice Questions
- What does backpropagation compute?
- In which direction does the backward pass travel, and where does it start?
- A function is made of two steps: y = f(x), then z = g(y). How does the chain rule give dz/dx?
- Why can a sigmoid activation cause a vanishing gradient?
- What is the difference between the forward pass and the backward pass?
Show answers
- The gradient of the loss with respect to every weight in the network. That is, how each weight should change to reduce the loss.
- It starts at the loss and travels backwards, from the output layer towards the input layer. That is why it is called backpropagation.
- dz/dx = (dz/dy) × (dy/dx). Multiply the slope of the outer step by the slope of the inner step.
- Its slope is small almost everywhere, so multiplying many small slopes together shrinks the signal as it travels backwards. Early layers then receive almost no learning signal.
- The forward pass computes the network's output and loss; the backward pass computes how each weight contributed to that loss. Training needs both, in that order.