Skip to content
Autograd

The Chain Rule

How to differentiate composed functions: the formula that connects every layer of a deep network.

The previous articles built up the gradient: a vector that captures how a scalar function changes with respect to each of its inputs. But the functions we care about in machine learning are rarely simple. They are compositions, sequences of operations applied one after another. Differentiating through a composition is the purpose of the chain rule.

The Formula

If zz depends on yy, and yy depends on xx, the chain rule gives the derivative of zz with respect to xx:

∂z∂x=∂z∂y⋅∂y∂x\frac{\partial z}{\partial x} = \frac{\partial z}{\partial y} \cdot \frac{\partial y}{\partial x}

In the language of composed functions: if y=f(x)y = f(x) and z=g(y)=g(f(x))z = g(y) = g(f(x)), then:

ddx[g(f(x))]=g′(f(x))⋅f′(x)\frac{d}{dx}\bigl[g(f(x))\bigr] = g'(f(x)) \cdot f'(x)

The derivative of the whole composition is the product of the local derivatives at each step.

Think of it as a pipeline. A change in xx causes a change in yy, which in turn causes a change in zz. The chain rule says: multiply the rates together.

x→fy→gzx \xrightarrow{f} y \xrightarrow{g} z ∂z∂x=∂z∂y⏟how z responds to y⋅∂y∂x⏟how y responds to x\frac{\partial z}{\partial x} = \underbrace{\frac{\partial z}{\partial y}}_{\text{how } z \text{ responds to } y} \cdot \underbrace{\frac{\partial y}{\partial x}}_{\text{how } y \text{ responds to } x}

A Concrete Example

Let f(x)=x2f(x) = x^2 and g(y)=3y+1g(y) = 3y + 1, so that z=g(f(x))=3x2+1z = g(f(x)) = 3x^2 + 1.

The local derivatives are:

∂y∂x=f′(x)=2x∂z∂y=g′(y)=3\frac{\partial y}{\partial x} = f'(x) = 2x \qquad \frac{\partial z}{\partial y} = g'(y) = 3

Applying the chain rule:

∂z∂x=3⋅2x=6x\frac{\partial z}{\partial x} = 3 \cdot 2x = 6x

We can verify this directly: ddx[3x2+1]=6x\dfrac{d}{dx}[3x^2 + 1] = 6x. The chain rule gives the same answer.

At x=2x = 2: ∂z∂x=12\dfrac{\partial z}{\partial x} = 12. A unit increase in xx from 22 increases zz by approximately 1212.

xy = f(x)z = g(y)fg∂y/∂x∂z/∂y∂z/∂x = ∂z/∂y · ∂y/∂x

The diagram shows the two passes. Reading left to right (blue), we compute the forward values. Reading right to left (green), we multiply the local derivatives to get the total derivative.

Longer Chains

The rule extends to any number of composed functions. For a chain x→h1→h2→⋯→hn→zx \to h_1 \to h_2 \to \cdots \to h_n \to z:

∂z∂x=∂z∂hn⋅∂hn∂hn−1⋯∂h2∂h1⋅∂h1∂x\frac{\partial z}{\partial x} = \frac{\partial z}{\partial h_n} \cdot \frac{\partial h_n}{\partial h_{n-1}} \cdots \frac{\partial h_2}{\partial h_1} \cdot \frac{\partial h_1}{\partial x}

Each factor ∂hk+1∂hk\dfrac{\partial h_{k+1}}{\partial h_k} is a local gradient: the derivative of one operation with respect to its own input, treating everything else as fixed.

The total derivative is just the product of all local gradients, accumulated from output to input. The chain rule turns a hard global problem (differentiating the full composition) into a sequence of easy local problems (differentiating each operation individually).

Why Neural Networks Need This

A neural network is a composition of operations. A simple two-layer network computes:

y^=g2 ⁣(g1(x))\hat{y} = g_2\!\bigl(g_1(x)\bigr)

where g1g_1 applies the first layer's weights and activation, and g2g_2 applies the second layer. In practice there are many more layers, each adding a step to the chain.

To understand how a parameter deep in the network affects the final output, we trace the chain from the output back to that parameter, multiplying local gradients at each step. The chain rule makes this possible for any number of layers.

For a parameter ww inside g1g_1:

∂y^∂w=∂y^∂g1⋅∂g1∂w\frac{\partial \hat{y}}{\partial w} = \frac{\partial \hat{y}}{\partial g_1} \cdot \frac{\partial g_1}{\partial w}

This is the chain rule applied once. In a deeper network it applies at every layer, each time multiplying in a new local gradient.

In Code

Applying the chain rule by hand makes the mechanics explicit:

# z = g(f(x)) where f(x) = x², g(y) = 3y + 1

x = 2.0
y = x ** 2           # forward: f(x)
z = 3 * y + 1        # forward: g(y)

dy_dx = 2 * x        # local gradient of f: f'(x) = 4
dz_dy = 3            # local gradient of g: g'(y) = 3

dz_dx = dz_dy * dy_dx   # chain rule: 3 × 4 = 12
print(dz_dx)             # 12.0

Deep learning frameworks apply this automatically. Every operation records its local gradient; when you request the derivative of the output, the framework multiplies them backward through the chain. That mechanism is what the next article covers.


The chain rule is the formula. The next articles are about the machinery that applies it: how frameworks record every operation, build a graph of computations, and walk that graph in reverse to compute all gradients at once.