AI Wiki中文/EN

The chain rule

链式法则

Track a change through composed calculations: multiply local sensitivities along a dependency and add contributions from different paths.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

A change passes through two stages

Suppose a parameter first becomes u=2x−1u=2x-1, then L=u3L=u^3. Changing xx changes uu, which changes LL. The first local rate is 2 and the second, at the current uu, is 3u23u^2. Together they give 6u26u^2.

At x=2x=2, u=3,L=27u=3,L=27, and the derivative with respect to xx 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 uu is differentiable at xx and gg at u(x)u(x), then:

ddxg(u(x))=g′(u(x))u′(x).\frac{d}{dx}g(u(x))=g'(u(x))u'(x).

The outer derivative is evaluated at the actual inner output. Replacing g′(u(x))g'(u(x)) with g′(x)g'(x) 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 hj(x)h_j(x), several can affect one loss. Their contributions add:

∂L∂xi=∑j=1m∂L∂hj∂hj∂xi.\frac{\partial L}{\partial x_i}=\sum_{j=1}^{m}\frac{\partial L}{\partial h_j}\frac{\partial h_j}{\partial x_i}.

For example, let u=x+yu=x+y, v=xyv=xy, and L=u2+vL=u^2+v. Input xx affects the result through both uu and vv, giving ∂L/∂x=2u+y\partial L/\partial x=2u+y and similarly ∂L/∂y=2u+x\partial L/\partial y=2u+x. At (2,3)(2,3) the gradient is (13,12)⊤(13,12)^\top. D2L, Section 2.4.4

Matrix notation makes this compact. For f:Rn→Rmf:\mathbb R^n\to\mathbb R^m and g:Rm→Rpg:\mathbb R^m\to\mathbb R^p, arrange Jacobians with outputs as rows and inputs as columns: entry (Jf)ji=∂fj/∂xi(J_f)_{ji}=\partial f_j/\partial x_i records output jj‘s sensitivity to input ii. Then:

Jg∘f(x)=Jg(f(x))Jf(x),∇xL=Jf(x)⊤∇fL.J_{g\circ f}(x)=J_g(f(x))J_f(x),\qquad \nabla_x L=J_f(x)^\top\nabla_f L.

The first product has shape (p×m)(m×n)=p×n(p\times m)(m\times n)=p\times n. 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 m×nm\times n matrix alone stores mnmn 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 xx directly, propagating only through an intermediate is incomplete. For F(x)=x(2x−1)3F(x)=x(2x-1)^3, the derivative includes direct term (2x−1)3(2x-1)^3 and indirect term 6x(2x−1)26x(2x-1)^2. Their sum at x=2x=2 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

  1. What is the derivative of L=(2x−1)3L=(2x-1)^3 at x=0x=0? Answer: 6, evaluating the outer slope at u=−1u=-1.
  2. If u=xu=x, v=xv=x, and L=u+vL=u+v, what is the derivative? Answer: 2. Each use contributes 1.
  3. With JfJ_f of shape 4×34\times3 and JgJ_g of shape 2×42\times4, what is the composite shape? Answer: 2×32\times3, with product ordered as JgJfJ_gJ_f.
  4. Why is the derivative of F(x)=x(2x−1)3F(x)=x(2x-1)^3 not just xx times the previous derivative? Answer: the leading xx 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?

Choose one answer

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

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. 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:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.