A network is a long chain of small functions. The chain rule turns "how does the loss change if I nudge this one weight?" into a bookkeeping job, and backpropagation is that job done in the cheapest possible order.
M4 gave you the derivative of a single function and the finite-difference check that tests it. M5 gave you partial derivatives and the gradient: one slope per parameter, bundled into a vector. M2 gave you matrices as machines that transform vectors. This lesson joins them. A real model is dozens of layers, each feeding the next, and the slope of the loss with respect to a weight in layer 3 depends on everything downstream of it. The chain rule says how to get that slope. Backpropagation is the chain rule organized so that one sweep through the network yields the slope for every weight. The statistics course, Lesson 8 (The Statistics Behind Training a Model), described gradient descent as a walk downhill. This lesson shows where the direction of that walk comes from.
A composite function feeds the output of one function into another: f(g(x)). First compute the inner value u = g(x), then compute y = f(u). A neural network layer is exactly this shape, and a whole network is many of them stacked.
The chain rule says that the slope of the composite is the product of the slopes of the pieces:
Read it as a chain of exchange rates. If nudging x by 1 moves u by 3, and nudging u by 1 moves y by 14, then nudging x by 1 moves y by 3 × 14 = 42. Rates multiply.
Let y = (3x + 1)2 and evaluate at x = 2. Name the inner piece u = 3x + 1.
Check with M4's finite difference: nudge x to 2.000001, get y = (7.000003)2 = 49.000042, and (49.000042 − 49) / 0.000001 = 42. The product of local slopes matches the measured slope.
Draw a calculation as a computational graph. Each node is one simple operation (multiply, add, sigmoid, subtract and square). Each arrow carries a value from one node to the next. A neural network's loss is one big graph.
The quantity travelling backward is the slope of the final loss L with respect to the value at that point, written ∂L/∂(value); at the loss node it starts at 1.
Build the smallest network that still has a hidden nonlinearity: one input, one hidden unit with a sigmoid, one linear output. Sigmoid is σ(z) = 1 / (1 + e−z); it squashes any number into the range 0 to 1, and its slope is σ′(z) = σ(z)(1 − σ(z)). Suppose a team is fitting a toy score predictor on a single example and every number is fixed up front:
| Item | Symbol | Value |
|---|---|---|
| Input | x | 1.5 |
| Target (what we want) | t | 1.0 |
| Hidden weight and bias | w1, b1 | 0.4, 0.1 |
| Output weight and bias | w2, b2 | 0.9, −0.2 |
| Learning rate | η | 0.5 |
The model is z = w1x + b1, then h = σ(z), then Ŷ = w2h + b2 (the prediction, written "y-hat"), and the loss is squared error with a half in front so the derivative comes out clean: L = ½(Ŷ − t)2.
The prediction is 0.4014 against a target of 1.0, so the loss is 0.1792. Now the graph, with the forward values on top and the backward gradients underneath:
The same graph carries two kinds of numbers. Blue arrows carry values forward; red arrows carry "how much does the loss care about this value" backward.
Start at the loss and walk left. At each step, the gradient arriving from the right is multiplied by the local derivative of that node.
| Step | Local derivative | Arithmetic | Gradient |
|---|---|---|---|
| Loss to prediction | ∂L/∂Ŷ = Ŷ − t | 0.4014 − 1.0 | −0.5986 |
| Prediction to w2 | ∂Ŷ/∂w2 = h | −0.5986 × 0.6682 | −0.4000 |
| Prediction to b2 | ∂Ŷ/∂b2 = 1 | −0.5986 × 1 | −0.5986 |
| Prediction to hidden | ∂Ŷ/∂h = w2 | −0.5986 × 0.9 | −0.5388 |
| Hidden to pre-activation | σ′(z) = h(1 − h) | 0.6682 × 0.3318 = 0.2217; −0.5388 × 0.2217 | −0.1195 |
| Pre-activation to w1 | ∂z/∂w1 = x | −0.1195 × 1.5 | −0.1792 |
| Pre-activation to b1 | ∂z/∂b1 = 1 | −0.1195 × 1 | −0.1195 |
The longest path shows the chain rule at full length: ∂L/∂w1 = (∂L/∂Ŷ) × (∂Ŷ/∂h) × (∂h/∂z) × (∂z/∂w1) = (−0.5986)(0.9)(0.2217)(1.5) = −0.1792. The backward pass reused −0.5986 and −0.5388 for several results; computing shared partial products once is the savings of backprop.
All four gradients are negative: raising any parameter would lower the loss. Gradient descent (the statistics course, Lesson 8) subtracts η times the gradient, so each parameter moves up:
Never trust hand-derived gradients without the M4 test. Nudge each parameter by ±0.000001, recompute the loss, and divide the difference by 0.000002:
| Parameter | Backprop gradient | Finite difference |
|---|---|---|
| w1 | −0.1792 | −0.1792 |
| b1 | −0.1195 | −0.1195 |
| w2 | −0.4000 | −0.4000 |
| b2 | −0.5986 | −0.5986 |
They agree to about ten digits when run in code. The whole check, with no libraries:
In the tiny network every variable fed exactly one next node. Real networks branch: one value goes to two places, and both influence the loss. The multivariable chain rule says to add up the contributions along each path.
Take a variable x that feeds two nodes, u = x + 1 and v = x2, which are then multiplied: f = u × v. At x = 3:
Check: f = (x + 1)x2 = x3 + x2, so df/dx = 3x2 + 2x = 27 + 6 = 33. A finite difference at step 0.000001 also returns 33.00001.
This is common: a residual connection (section 7) sends a layer's input down two routes, and a shared embedding table receives a gradient from every position where a token appears. Whenever two gradients arrive at the same node in a backward pass, they are added.
The examples above have one number per node. A real layer takes a vector in and gives a vector out. The M5 gradient generalizes to the Jacobian: a matrix whose entry in row i, column j is the partial derivative of output i with respect to input j. For a layer with 1,000 inputs and 1,000 outputs, the Jacobian is a 1,000 × 1,000 matrix. (For a linear layer a = Wz from M2, the Jacobian is simply W itself.)
The chain rule for a stack of layers then reads: the Jacobian of the whole network is the product of the layer Jacobians. Only the order of multiplication is a choice, and the order decides the cost:
The difference is large. A 1,000-entry vector times a 1,000 × 1,000 matrix costs about 1,000,000 multiply-adds. Two 1,000 × 1,000 matrices cost about 1,000,000,000. Backprop keeps the left factor a vector, so it never pays the second price.
The backward pass multiplies one factor per layer. A long product of numbers is dangerous: if the factors are mostly below 1 the product shrinks toward zero, and if they are mostly above 1 it blows up.
Sigmoid is the classic case. Its slope σ′(z) = σ(z)(1 − σ(z)) peaks at z = 0, where σ = 0.5 and the slope is 0.5 × 0.5 = 0.25. It is never larger. In the tiny network the slope was 0.2217, already close to that ceiling, and it shrinks quickly when z is large in either direction. Now stack layers. If each layer's weight is 1 and every sigmoid sits at its best case, each layer multiplies the gradient by at most 0.25:
| Sigmoid layers | Best-case gradient factor | Value |
|---|---|---|
| 5 | 0.255 | 0.00098 |
| 10 | 0.2510 | 0.00000095 |
| 20 | 0.2520 | 9.1 × 10−13 |
With 10 layers, the gradient reaching the first layer is under one millionth of the gradient at the output. The early layers receive almost no signal, so they barely learn. That is the vanishing gradient problem, and it is the best case. Sigmoid slopes are at most 0.25, so this is the best case for a chain of sigmoids; large weights can offset it, which is the exploding side of the same product.
The mirror image is the exploding gradient. If each layer's factor is 1.5 instead, the product grows:
A gradient that large produces a wild update, and the loss can jump to infinity or NaN. The usual remedy is gradient clipping: if the gradient's length exceeds a limit, rescale it to the limit before updating.
The ReLU activation, max(0, z), has slope exactly 1 for any positive input and 0 for a negative one. For active units the factor per layer is therefore 1 rather than at most 0.25, so a product of many such factors does not shrink just from the activation. A negative input has slope 0 and passes no gradient, so units can get stuck "dead"; and the weights still multiply in. ReLU lowers the risk but does not remove it.
A residual connection computes y = x + F(x), adding the input back onto the output. Differentiating gives dy/dx = 1 + dF/dx. That "+1" is the sum-over-paths rule from section 4 at work: the gradient has a direct route that skips F entirely, so even if dF/dx is tiny, the total factor stays near 1 instead of near 0. This is one reason very deep networks became trainable. It is a helpful property, not a guarantee; a deep network still needs sensible initialization and learning rates.
Nobody derives these equations by hand for a real model. Deep-learning frameworks track the computation as the forward pass runs, then execute reverse-mode automatic differentiation to produce every gradient. Each primitive operation ships with its own local derivative; the framework just chains them. A call like loss.backward() is the backward pass from section 3, at scale.
Knowing the mechanism still pays. A NaN gradient means look for a node with a huge local derivative. Slow early layers mean vanishing factors. Out-of-memory in training but not inference means saved forward values. A non-differentiable operation, such as rounding or a hard argmax, stops the gradient there.
If loss does not move, print the gradient size per layer. Near-zero gradients in early layers and healthy ones late point to vanishing factors; spikes point to exploding ones. Exactly zero for a group of parameters often means something is detached from the graph.
One backward pass costs a small multiple of a forward pass and needs the activations stored. That is why recomputing activations instead of storing them exists, and why fine-tuning few parameters, as LoRA does, cuts memory: frozen parameters need no stored gradient.
Derivatives answer "how fast does this change?" M7 covers integrals: adding up a quantity that varies continuously. That is how a probability density becomes a probability, how an expected value over a continuous distribution is computed, and why normalizing constants appear in probabilistic models. It connects the calculus you now have back to the statistics course.
1. For y = (3x + 1)2, what is dy/dx at x = 2?
2. In the tiny network, the hidden unit's output is h = 0.6682. What is the local derivative of the sigmoid at that point, used in the backward pass?
3. Why is reverse mode (backprop) the standard choice for training a model with millions of parameters and one loss value?
4. A stack of 10 sigmoid layers, each with weight 1 and each at the best-case slope of 0.25, scales the gradient reaching the first layer by about:
5. A variable x feeds two nodes whose outputs are later combined into the loss. How does backprop combine the gradients arriving at x?