TL;DR
Many functions are built by feeding one function into another: first square a number, then add five, then take the sine. The chain rule tells you how fast the final output changes when the input changes. You find the rate of change of each step and multiply them together, taking care to evaluate each rate at the right place.
It is the rule that lets you differentiate any expression made of layers, which is what a neural network is.
Where you'll see it: in backpropagation, where the error of a network is pushed backwards through every layer, and in the reparameterization trick used to train variational models.
Intuition
Gears in a chain
Picture three gears. The first turns the second, and the second turns the third. If the first gear makes 2 turns for every turn of the crank, and the second makes 3 turns for every turn of the first, then the third turns 6 times for every crank turn. The overall ratio is the product of the ratios along the chain.
A derivative is exactly such a ratio: how many units the output moves for each unit the input moves. A composed function is a chain of steps, so its overall ratio is the product of the step ratios. The chain rule is that product.
The catch is that each step's ratio depends on where that step currently sits. A gear's ratio can change as it turns, so you must look up each ratio at the point the signal actually reaches it. This is the part people most often get wrong.
- f(x) = (3x + 1)^2
- tangent at x = 1
The math
Definition
Let be differentiable at and let be differentiable at . The composite function is differentiable at , and
Here is the inner function and is the outer function. is the rate of change of the inner function at . is the rate of change of the outer function, evaluated at the point that the inner function produced. The outer derivative is not evaluated at itself.
The Leibniz form
Write and . Then is a function of through , and the rule reads as if the terms cancel:
This is a memory aid, not a proof (the are not ordinary fractions), but it tells you which variable each factor belongs to.
Why it is true (sketch)
Consider the slope of between and . Multiply and divide by the change in the inner function, :
As goes to zero, goes to . The second factor becomes . The first factor is a slope of between and a point very close to it, so it becomes . Multiplying the two limits gives the rule. A full proof must also handle the case where for some small , which this sketch skips.
Longer chains
For , apply the rule twice, or equivalently multiply one factor for each layer, evaluating each derivative at the value that layer receives:
Leibniz form: if , , , then .
Several variables: add the paths
Suppose where both and depend on a single variable . Then changes along two routes, one through and one through , and the total change is the sum of the two:
This uses partial derivatives for the direct effects of and on . The general rule is: multiply the rates along each path from the input to the output, then add over all paths. When every variable is a vector, the same idea is written with a Jacobian matrix, whose rows and columns are the partial derivatives of each output with respect to each input.
Worked examples
Let . Take the inner function and the outer function .
- Inner derivative: .
- Outer derivative: , so .
- Multiply: .
- Evaluate at : , so .
Check by hand: and . The slope between them is , which is close to . Interpretation: near the output rises about 24 units for each unit of input, the value shown in the plot.
Let . The inner function is and the outer function is , whose derivative is itself.
- Inner derivative: .
- Outer derivative at the inner value: .
- Multiply: .
- Evaluate at : .
Interpretation: at the function is climbing at about 5.4 units per unit of . Note that the outer derivative is , not . Forgetting to substitute the inner function is the most common slip.
A model predicts , where is an input and is a weight we want to tune. The target is , and the loss is . Find at .
- Start with at , so the error is and .
- Write the chain as . The three local rates are:
- Multiply along the chain: .
- Check: , so , which is at . Numerically, , and .
Interpretation: increasing slightly decreases the loss, so a gradient-descent step should increase . This is exactly the calculation a network performs for each weight, layer by layer, in Backpropagation.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Forgetting to evaluate the outer derivative at the inner value | You write instead of and get a plausible but wrong answer | Name the inner value () first, then differentiate at |
| Inner function not differentiable at the point (for example $ | x | 0$) |
| Summing paths in a multivariable chain incorrectly | Some routes are missed or counted twice | Draw the dependency graph and add one term per path |
Automatic differentiation applies the chain rule mechanically to every operation in a program. Backpropagation is its reverse-mode form, efficient when one scalar output depends on many inputs.
History
Gottfried Wilhelm Leibniz developed the notation in the late 1600s, and its cancelling-fraction look is why the chain rule is easy to remember. Isaac Newton worked with the same ideas in his own notation at about the same time. In the 1850s, Francesco Faà di Bruno gave a formula for higher derivatives of composite functions that still bears his name.
The rule became central to machine learning in the 1980s. Rumelhart, Hinton and Williams showed in 1986 that repeatedly applying the chain rule backwards through a network lets every weight learn from its share of the error. Their work made training multi-layer networks practical, and the same calculation is the core of modern deep learning.
Check yourself
QuestionState the chain rule for in words, and say where each derivative is evaluated.Show answer
The derivative of at is the outer derivative evaluated at the inner value, , multiplied by the inner derivative at , . The outer derivative is evaluated at , not at , because that is the point the outer function actually receives.
QuestionDifferentiate and evaluate at .Show answer
Let and . Then . At , , so .
QuestionWhy does the chain rule multiply the factors rather than add them?Show answer
Each factor scales a small change as it passes through one step. Scaling by one step and then by the next multiplies the effects, so the overall rate is the product. Adding would describe two separate effects, not one signal passing through both steps.
QuestionIn the sigmoid , the derivative is . A neuron computes . Find at with .Show answer
Let . The chain rule gives . At , and , so . Multiplying by gives .
QuestionA variational model draws with noise fixed. Why does the chain rule let gradients reach and ?Show answer
Because is a simple differentiable function of and once is fixed, and . Any loss that depends on then has derivatives with respect to and obtained by multiplying the loss gradient with these factors. Sampling directly from a distribution would block this path, so the reparameterization trick moves the randomness into , which needs no gradient.