Search

Search papers and lessons

Chain Rule

Level 1 · 9 min read

The chain rule gives the derivative of a function built from other functions: multiply the rates of change along the chain.

On this page

  • Colour = topic mix
  • Calculus
  • Sign in to see your progress

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.

−1.5−1−0.500.511.5−100102030xf(x)
  • f(x) = (3x + 1)^2
  • tangent at x = 1
Near x = 1 the curve is steep. Its slope there is 24, the value the chain rule gives. The dashed line has that slope and touches the curve at the marked point.

The math

Definition

Definition: Chain rule

Let gg be differentiable at xx and let ff be differentiable at g(x)g(x). The composite function h(x)=f(g(x))h(x) = f(g(x)) is differentiable at xx, and

h′(x)=f′(g(x))⋅g′(x).h'(x) = f'(g(x)) \cdot g'(x).

Here gg is the inner function and ff is the outer function. g′(x)g'(x) is the rate of change of the inner function at xx. f′(g(x))f'(g(x)) is the rate of change of the outer function, evaluated at the point g(x)g(x) that the inner function produced. The outer derivative is not evaluated at xx itself.

The Leibniz form

Write u=g(x)u = g(x) and y=f(u)y = f(u). Then yy is a function of xx through uu, and the rule reads as if the dudu terms cancel:

dydx=dydu⋅dudx.\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx}.

This is a memory aid, not a proof (the dudu are not ordinary fractions), but it tells you which variable each factor belongs to.

Why it is true (sketch)

Consider the slope of hh between xx and x+Δx + \Delta. Multiply and divide by the change in the inner function, g(x+Δ)−g(x)g(x+\Delta) - g(x):

h(x+Δ)−h(x)Δ=f(g(x+Δ))−f(g(x))g(x+Δ)−g(x)⋅g(x+Δ)−g(x)Δ.\frac{h(x+\Delta) - h(x)}{\Delta} = \frac{f\big(g(x+\Delta)\big) - f\big(g(x)\big)}{g(x+\Delta) - g(x)} \cdot \frac{g(x+\Delta) - g(x)}{\Delta}.

As Δ\Delta goes to zero, g(x+Δ)g(x+\Delta) goes to g(x)g(x). The second factor becomes g′(x)g'(x). The first factor is a slope of ff between g(x)g(x) and a point very close to it, so it becomes f′(g(x))f'(g(x)). Multiplying the two limits gives the rule. A full proof must also handle the case where g(x+Δ)=g(x)g(x+\Delta) = g(x) for some small Δ\Delta, which this sketch skips.

Longer chains

For h(x)=f(g(k(x)))h(x) = f(g(k(x))), apply the rule twice, or equivalently multiply one factor for each layer, evaluating each derivative at the value that layer receives:

h′(x)=f′(g(k(x)))⋅g′(k(x))⋅k′(x).h'(x) = f'\big(g(k(x))\big) \cdot g'\big(k(x)\big) \cdot k'(x).

Leibniz form: if v=k(x)v = k(x), u=g(v)u = g(v), y=f(u)y = f(u), then dydx=dydu⋅dudv⋅dvdx\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dv} \cdot \frac{dv}{dx}.

Several variables: add the paths

Suppose z=F(x,y)z = F(x, y) where both xx and yy depend on a single variable tt. Then zz changes along two routes, one through xx and one through yy, and the total change is the sum of the two:

dzdt=∂F∂xdxdt+∂F∂ydydt.\frac{dz}{dt} = \frac{\partial F}{\partial x} \frac{dx}{dt} + \frac{\partial F}{\partial y} \frac{dy}{dt}.

This uses partial derivatives for the direct effects of xx and yy on FF. 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

Example
A tiny one: a squared linear function

Let h(x)=(3x+1)2h(x) = (3x + 1)^2. Take the inner function g(x)=3x+1g(x) = 3x + 1 and the outer function f(u)=u2f(u) = u^2.

  1. Inner derivative: g′(x)=3g'(x) = 3.
  2. Outer derivative: f′(u)=2uf'(u) = 2u, so f′(g(x))=2(3x+1)f'(g(x)) = 2(3x + 1).
  3. Multiply: h′(x)=2(3x+1)⋅3=6(3x+1)h'(x) = 2(3x + 1) \cdot 3 = 6(3x + 1).
  4. Evaluate at x=1x = 1: g(1)=4g(1) = 4, so h′(1)=6⋅4=24h'(1) = 6 \cdot 4 = 24.

Check by hand: h(1)=16h(1) = 16 and h(1.001)=(4.003)2=16.024009h(1.001) = (4.003)^2 = 16.024009. The slope between them is 0.024009/0.001=24.0090.024009 / 0.001 = 24.009, which is close to 2424. Interpretation: near x=1x = 1 the output rises about 24 units for each unit of input, the value shown in the plot.

Example
An exponential of a square

Let h(x)=ex2h(x) = e^{x^2}. The inner function is g(x)=x2g(x) = x^2 and the outer function is f(u)=euf(u) = e^u, whose derivative is itself.

  1. Inner derivative: g′(x)=2xg'(x) = 2x.
  2. Outer derivative at the inner value: f′(g(x))=ex2f'(g(x)) = e^{x^2}.
  3. Multiply: h′(x)=ex2⋅2x=2x ex2h'(x) = e^{x^2} \cdot 2x = 2x\,e^{x^2}.
  4. Evaluate at x=1x = 1: h′(1)=2⋅e1≈5.437h'(1) = 2 \cdot e^{1} \approx 5.437.

Interpretation: at x=1x = 1 the function is climbing at about 5.4 units per unit of xx. Note that the outer derivative is ex2e^{x^2}, not exe^x. Forgetting to substitute the inner function is the most common slip.

Example
A realistic one: the gradient of a squared error

A model predicts z=wxz = wx, where x=2x = 2 is an input and ww is a weight we want to tune. The target is t=5t = 5, and the loss is L=(z−t)2L = (z - t)^2. Find dLdw\frac{dL}{dw} at w=1w = 1.

  1. Start with z=2z = 2 at w=1w = 1, so the error is e=z−t=−3e = z - t = -3 and L=9L = 9.
  2. Write the chain as w→z→e→Lw \to z \to e \to L. The three local rates are:
    • dzdw=x=2\frac{dz}{dw} = x = 2
    • dedz=1\frac{de}{dz} = 1
    • dLde=2e=−6\frac{dL}{de} = 2e = -6
  3. Multiply along the chain: dLdw=(−6)(1)(2)=−12\frac{dL}{dw} = (-6)(1)(2) = -12.
  4. Check: L(w)=(2w−5)2L(w) = (2w - 5)^2, so dLdw=4(2w−5)\frac{dL}{dw} = 4(2w - 5), which is −12-12 at w=1w = 1. Numerically, L(1.001)=(−2.998)2=8.988004L(1.001) = (-2.998)^2 = 8.988004, and (8.988004−9)/0.001=−11.996(8.988004 - 9) / 0.001 = -11.996.

Interpretation: increasing ww slightly decreases the loss, so a gradient-descent step should increase ww. This is exactly the calculation a network performs for each weight, layer by layer, in Backpropagation.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Forgetting to evaluate the outer derivative at the inner valueYou write f′(x)f'(x) instead of f′(g(x))f'(g(x)) and get a plausible but wrong answerName the inner value (u=g(x)u = g(x)) first, then differentiate ff at uu
Inner function not differentiable at the point (for example $xatat0$)
Summing paths in a multivariable chain incorrectlySome routes are missed or counted twiceDraw 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 dydx\frac{dy}{dx} 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 h(x)=f(g(x))h(x) = f(g(x)) in words, and say where each derivative is evaluated.Show answer

The derivative of hh at xx is the outer derivative evaluated at the inner value, f′(g(x))f'(g(x)), multiplied by the inner derivative at xx, g′(x)g'(x). The outer derivative is evaluated at g(x)g(x), not at xx, because that is the point the outer function actually receives.

QuestionDifferentiate h(x)=(2x+5)3h(x) = (2x + 5)^3 and evaluate at x=0x = 0.Show answer

Let g(x)=2x+5g(x) = 2x + 5 and f(u)=u3f(u) = u^3. Then h′(x)=3(2x+5)2⋅2=6(2x+5)2h'(x) = 3(2x + 5)^2 \cdot 2 = 6(2x + 5)^2. At x=0x = 0, g(0)=5g(0) = 5, so h′(0)=6⋅25=150h'(0) = 6 \cdot 25 = 150.

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 σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}, the derivative is σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)). A neuron computes σ(wx)\sigma(wx). Find ddwσ(wx)\frac{d}{dw}\sigma(wx) at w=0w = 0 with x=3x = 3.Show answer

Let z=wxz = wx. The chain rule gives ddwσ(wx)=σ′(wx)⋅x=σ(wx)(1−σ(wx))⋅x\frac{d}{dw}\sigma(wx) = \sigma'(wx) \cdot x = \sigma(wx)(1 - \sigma(wx)) \cdot x. At w=0w = 0, z=0z = 0 and σ(0)=12\sigma(0) = \frac{1}{2}, so σ′(0)=12⋅12=14\sigma'(0) = \frac{1}{2} \cdot \frac{1}{2} = \frac{1}{4}. Multiplying by x=3x = 3 gives 34\frac{3}{4}.

QuestionA variational model draws z=μ+σεz = \mu + \sigma \varepsilon with noise ε\varepsilon fixed. Why does the chain rule let gradients reach μ\mu and σ\sigma?Show answer

Because zz is a simple differentiable function of μ\mu and σ\sigma once ε\varepsilon is fixed, ∂z∂μ=1\frac{\partial z}{\partial \mu} = 1 and ∂z∂σ=ε\frac{\partial z}{\partial \sigma} = \varepsilon. Any loss that depends on zz then has derivatives with respect to μ\mu and σ\sigma obtained by multiplying the loss gradient with these factors. Sampling zz directly from a distribution would block this path, so the reparameterization trick moves the randomness into ε\varepsilon, which needs no gradient.

Sources