Welcome back. In the previous lesson, you calculated partial derivatives by holding all but one variable fixed, then assembled those partial derivatives into a gradient. That works when the function is written directly in terms of its inputs.
Models are rarely written that way. A loss depends on an output; the output depends on intermediate activations; those activations depend on parameters. The chain rule is the accounting rule that lets us trace local sensitivity through those intermediate computations. It is the mathematical core of backpropagation.
By the end of this lesson, you should be able to identify a short composition of functions, apply the chain rule correctly, and recognize why gradients through a computation graph sometimes multiply along a path and sometimes sum across paths.
Composition: a function used as another function’s input
A function is composite when one function’s output becomes another function’s input. Write
followed by
Combining those two stages gives
Here, is the inner function because it operates on first. The function is the outer function because it operates on the result of .
For example,
is composite. Define
and then
The square is applied not directly to , but to the intermediate quantity .
In contrast,
is a product of two functions. Neither function is nested inside the other. It needs the product rule, not just the chain rule. In real neural-network expressions, several differentiation rules can appear together, but for now the key skill is recognizing nesting.
A useful habit is to read an expression from the outside inward to identify the functions, then differentiate from the outside back toward the input.
Visualizing the chain rule and product rule | Chapter 4, Essence of calculus
Watch “Visualizing the chain rule and product rule” from 3Blue1Brown. Its number-line view makes the chain rule concrete: a small input change produces an intermediate change, which then produces an output change.
Watch propagated changes, where the video tracks a small change in x through x^2 and then through sine. Continue with the formal rule. Focus on why the outer derivative is evaluated at the intermediate value, not at the original input.
The one-variable chain rule
Suppose
and
A small change in changes at the local rate
That intermediate change then changes at the local rate
Therefore the total local rate of change from to is
Using the composition notation , this becomes
The formula has two factors:
- Outer sensitivity: . Differentiate the outer function, but evaluate it at the original inner expression.
- Inner sensitivity: . Differentiate the inner function with respect to the original input.
The important detail is the location of evaluation. The first factor is not generally . The outer function receives , so its derivative must be evaluated at .
A compact operational recipe
For a short composition:
- Name the inside expression .
- Differentiate the outside with respect to .
- Substitute the original expression for back in.
- Multiply by the derivative of the inside expression.
This preserves the structure of the computation and prevents the most common omission: forgetting the inner derivative.
Worked example: a power applied to a linear expression
Consider
Introduce an intermediate variable:
so that
Differentiate the outer function with respect to :
Differentiate the inner function with respect to :
Now multiply the local rates:
Finally, replace with its original definition:
At ,
So near , increasing by a small amount increases at roughly times that amount.
Notice the two necessary ingredients. If you differentiate only the square, you get
which is incomplete because it ignores how quickly the inside quantity changes. If you multiply by the derivative of the inside but replace the inside too early or incorrectly, you also lose the computation’s structure.
Here is another common pattern:
The outer function is sine, and the inner function is . Therefore,
The cosine is evaluated at , the value being passed into sine.
Chain rule (article) | Khan Academy
Read Khan Academy’s chain-rule review for a second, compact presentation of composite functions, a worked power example, and the two most frequent mistakes.
In “Quick review of composite functions,” read the opening explanation through the comparison between nesting and multiplication. Use the definition as an anchor, but read the surrounding paragraph for its examples. Then, in “Worked example of applying the chain rule,” follow the worked setup and compare each of its steps with the four-step recipe above. Finish with the subsection “Common mistake: Forgetting to multiply by the derivative of the inner function,” especially the missing factor.
Three nested stages: one factor per local operation
A model computation often has more than two stages. Consider
There are three successive operations:
Start at the output. The local derivatives are
The derivative from input to output is the product of every local derivative in the dependency chain:
Substitute the intermediate definitions only after forming the product:
A simplified form is
The product can look large, but each factor has a clear meaning: it measures the local scaling introduced by one operation.
This is a useful way to read backpropagation later:
- A forward pass calculates and stores intermediate values.
- A backward pass calculates local derivatives at those stored values.
- The total sensitivity is assembled from those local derivatives.
From a single chain to a computation graph
Real computations can branch. An input may influence the final output through more than one intermediate value. In that situation, each path contributes to the derivative, and the contributions are summed.

Suppose
where both and depend on and . Then the multivariable chain rule gives
Similarly,
The two products in each equation correspond to the two distinct dependency paths in the computational graph.
Worked branching example
Let
and let the final scalar output be
First calculate the local derivatives of the output with respect to its two immediate inputs:
Now calculate how the intermediates depend on :
Apply the multivariable chain rule:
At
the intermediate value is
Therefore,
The interpretation is precise: near this input point, a small increase in , with held fixed, increases at a local rate of about . That total rate includes both effects: the effect through and the separate effect through .
This “multiply within a path, add across paths” rule is the structural idea behind reverse-mode automatic differentiation.
A miniature model-loss chain
Consider a one-feature prediction model:
where and are parameters, is an input feature, and is the prediction. For one labeled example with target , define squared error:
This is a small version of the structure used in machine learning: parameters affect a prediction, which affects a loss.
To find the sensitivity of loss to the weight , treat as the intermediate variable:
The local derivatives are
and
Thus,
For the bias,
so
The difference between these gradients comes entirely from the local dependency of on each parameter. The weight affects the prediction in proportion to ; the bias affects it by the same amount regardless of .
A speech model is vastly larger than this toy calculation: its inputs are feature tensors or audio representations, its parameters are matrices, and its computation graph has many layers. But the same local logic applies. Frameworks such as PyTorch record a graph of operations and use the chain rule to calculate parameter gradients without manually expanding an enormous expression.
Chain-rule error checks
Before trusting a derivative, inspect it with these checks:
| Check | What it catches |
|---|---|
| Identify the outermost operation first. | Misidentifying a nested expression as a product. |
| Keep the original inside expression in the outer derivative. | Writing when the correct term is . |
| Include one local derivative for every stage. | Forgetting the derivative of an inner function. |
| Multiply along one dependency path. | Omitting a local sensitivity factor. |
| Add contributions from distinct paths. | Missing a second way an input influences the output. |
| Check the derivative’s shape. | In ML, the gradient with respect to a parameter must have that parameter’s shape. |
For hand calculations, naming intermediate variables is more than a stylistic preference. It turns a complicated expression into a short computation graph, makes the dependencies explicit, and mirrors the reasoning used by automatic differentiation systems.
Key takeaways
- The chain rule differentiates a composition such as :
- Differentiate the outer function first, evaluate it at the unchanged inner expression, and multiply by the derivative of the inner function.
- For several nested stages, multiply the local derivatives for all stages.
- In a branching computation graph, multiply local derivatives along each path and add the contributions across paths.
- Backpropagation is a scalable implementation of this rule for the large computation graphs used in neural networks.
Next, the course shifts from local change in functions to numerical summaries of data: mean, variance, and covariance. Those statistics will become essential when inspecting speech features, dataset balance, and variation across speakers or recording conditions.
Can't find a good explanation? Sign up and we'll make it for you
Sign up