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 depends on , and depends on , the chain rule gives the derivative of with respect to :
In the language of composed functions: if and , then:
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 causes a change in , which in turn causes a change in . The chain rule says: multiply the rates together.
A Concrete Example
Let and , so that .
The local derivatives are:
Applying the chain rule:
We can verify this directly: . The chain rule gives the same answer.
At : . A unit increase in from increases by approximately .
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 :
Each factor 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:
where applies the first layer's weights and activation, and 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 inside :
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.