The chain rule
链式法则
Track a change through composed calculations: multiply local sensitivities along a dependency and add contributions from different paths.
chain-ruleA change passes through two stages
Suppose a parameter first becomes , then . Changing changes , which changes . The first local rate is 2 and the second, at the current , is . Together they give .
At , , and the derivative with respect to is 54. Omitting the inner factor of 2 would incorrectly give 27. The chain rule combines local effects without requiring expansion of the entire composed expression.
Evaluate each derivative at the right point
If is differentiable at and at , then:
The outer derivative is evaluated at the actual inner output. Replacing with measures the slope at the wrong location. Although differential notation resembles cancellation of fractions, the rule follows from composing local linear approximations, not arbitrary symbol cancellation. MML, Sections 5.1.2 and 5.3
When an inner computation produces multiple quantities , several can affect one loss. Their contributions add:
For example, let , , and . Input affects the result through both and , giving and similarly . At the gradient is . D2L, Section 2.4.4
Matrix notation makes this compact. For and , arrange Jacobians with outputs as rows and inputs as columns: entry records output ‘s sensitivity to input . Then:
The first product has shape . The second expression uses column gradients, hence the transpose. Do not silently switch row- and column-gradient conventions within a derivation.
Check local composition against the complete function
This complete program supplies local derivatives by hand and compares them with finite differences. It verifies the chain rule for the examples; it is not a general automatic-differentiation engine.
def scalar_value_and_derivative(x):
u = 2 * x - 1
value = u ** 3
derivative = (3 * u * u) * 2
return value, derivative
def branched(x, y):
u, v = x + y, x * y
value = u * u + v
du, dv = 2 * u, 1
return value, [du + dv * y, du + dv * x]
assert scalar_value_and_derivative(2) == (27, 54)
value, gradient = branched(2, 3)
assert value == 31 and gradient == [13, 12]
h = 1e-5
dx = (branched(2 + h, 3)[0] - branched(2 - h, 3)[0]) / (2 * h)
dy = (branched(2, 3 + h)[0] - branched(2, 3 - h)[0]) / (2 * h)
assert abs(dx - gradient[0]) < 1e-8
assert abs(dy - gradient[1]) < 1e-8
These fixed-size examples take a constant number of arithmetic operations. Large compositions can make explicit Jacobians expensive: an matrix alone stores numbers. Automatic differentiation often calculates products between Jacobians and vectors without materializing the whole matrix.
Direct dependencies, branches, and nonsmooth points
If the function also uses directly, propagating only through an intermediate is incomplete. For , the derivative includes direct term and indirect term . Their sum at is 135.
The chain rule requires the relevant differentiability conditions. A program with different expressions on different branches can have a derivative along its current branch, while a branch boundary may be nondifferentiable. Rounding, comparisons, and discrete choices need treatment beyond blindly applying a smooth chain rule.
Shared intermediates create another implementation trap. A value used twice downstream receives both contributions to its gradient. The final assignment must not overwrite the earlier one. Computational graphs make these dependencies explicit.
Count every influence path
- What is the derivative of at ? Answer: 6, evaluating the outer slope at .
- If , , and , what is the derivative? Answer: 2. Each use contributes 1.
- With of shape and of shape , what is the composite shape? Answer: , with product ordered as .
- Why is the derivative of not just times the previous derivative? Answer: the leading also changes and contributes a direct dependency.
Turn the formula into executable differentiation
Backpropagation sends scalar-loss sensitivity backward through dependencies, while forward mode sends an input-direction perturbation forward. Both use the chain rule with different accumulation orders and products. Computational graphs describe dependencies, automatic differentiation propagates derivatives, and gradient checks test the resulting implementation.
Make a prediction, then check
For L=(2x−1)³, what is the derivative at x=2?
Learning objectives
- Evaluate local derivatives at the correct intermediate values and sum branch contributions.
- Order Jacobian products by their shapes and check a composition with finite differences.
Prerequisites & connections
Explore related knowledge →References
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Dive into Deep Learning — Calculus
Aston Zhang; Zachary C. Lipton; Mu Li; Alexander J. Smola · 2023
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.
- Recalculated single-path derivative 54, branched gradient (13,12), and direct-plus-indirect derivative 135; compared with finite differences.
- Added the Jacobian-entry definition and checked (p×m)(m×n) order and column-gradient transpose; retained differentiability assumptions and shared-contribution sums.
- Compared arguments, numbers, examples, limitations, and exercise answers across languages; structural validation separately compares display formulas, code, sources, and sections. No independent human peer review.
Source locations checked:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §5.1.2 and §5.3: composition rules and Jacobians
- Dive into Deep Learning — Calculus · §2.4.4: chain rule
Related topics