The chain rule
Last updated
A model is functions inside functions: multiply and add, then squash with a sigmoid, then measure the mistake. To train it you need the derivative of the whole chain, and the chain rule gives it: multiply the derivatives of the links. Backpropagation, the method that trains every neural network, is this one rule used over and over (Stage 6).
By the end of this lesson you can
- Split a function into a chain of simpler ones
- Use the chain rule to differentiate a function inside a function
- Derive the sigmoid’s derivative, , and check it
- Differentiate a chain of three, as one neuron’s loss
Functions in a chain
Take . You could expand it, but it's easier to see it as two steps, one feeding the next:
In words: first work out from , then work out from . is the inner function and squaring is the outer one. Each link on its own is easy to differentiate with lesson 1.6's rules: and .
A model looks the same, only longer: inputs → weighted sum → sigmoid → loss. Every link is simple; the chain is what makes it a model.
The rule: multiply the rates
Think of the links as gears. Turn a little and turns 3 times as much. Turn a little and turns times as much. So turns times as much as did. Nudge the first gear and compare:
Nudge x a little and watch how far each gear moves. At x = 1, u = 4 and y = 16.
That's the chain rule:
In words: the rate of the whole chain is the rates of the links multiplied together. Written with function names, if , then : the outer derivative, worked out at the inner value, times the inner derivative.
For our chain at : , so . The central difference from lesson 1.6 agrees:
The derivative of the sigmoid
The sigmoid, , is a chain too, and working through it gives a famously neat answer:
In words: the slope of the sigmoid at is its value times one minus its value. In NumPy: s = sigmoid(z), then s * (1 - s). (The working: write with . The power rule gives , the inner derivative is , and their product rearranges to .)
The formula and the nudge agree. Two things to notice: the slope is biggest, 0.25, at , where the sigmoid is steepest; and it's small far from 0, where the curve is nearly flat. Stage 6 comes back to that: when a neuron's sigmoid sits on a flat part, its slope is tiny and it learns slowly.
A chain of three
Here's one neuron's mistake as a chain of three links. Input , weight , bias , and target :
In words: a weighted sum, squashed into a probability, then the squared mistake. To train we need : how the mistake changes when the weight changes. The chain rule multiplies the three links:
At the answer is −0.5: negative, so making a little bigger makes the mistake smaller. Gradient descent (Stage 3) takes exactly that hint. Backpropagation (Stage 6) is this same multiplication, done from the loss backwards through every link of a network, reusing each product on the way.
Wrong way: forgetting the inner derivative
The commonest slip is to differentiate the outer function and stop. For that gives , dropping the :
8 against 24: off by exactly the missing factor of 3. This is why the course checks every hand-worked derivative with a central difference. A slip like this doesn't crash anything; a model trained with it just learns badly, and you'd never know why.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 6: Deep Feedforward Networks: the chain rule of calculus and back-propagation (6.5.2)
- Goodfellow, Bengio and Courville, Deep Learning, chapter 3: Probability and Information Theory: the sigmoid and the identity for its derivative (3.10)
- Prince, Understanding Deep Learning: chapter 7, gradients and backpropagation, which Stage 6 follows