The Chain Rule
When a quantity depends on intermediate variables that themselves depend on other variables, the chain rule says how to differentiate through the whole chain: add up the contributions of every path from the output back to the input. This lesson states the general multivariable chain rule, proves it from differentiability, turns it into a tool for implicit differentiation, and shows that backpropagation in neural networks is this rule applied to a computational graph.
For the one-variable rule see The Chain Rule; the survey version of this lesson is The Multivariable Chain Rule.
Case 1: one parameter
Let be differentiable, and let , be differentiable. Then is a function of and
In vector form, with : . The rate of change of along a moving point is the gradient dotted with the velocity.
Proof. Let produce changes , . Differentiability of means
Divide by and let . Since and are differentiable they are continuous, so and , while and . The -terms vanish, leaving the formula. Note that the proof needs to be differentiable, not merely to have partial derivatives.
Example: the temperature on a plate is , and an ant walks along , . At the ant is at with velocity :
Case 2 and the general rule
If with , , then holding the other parameter fixed gives
General version. If depends on and each depends on , then for each
A tree diagram makes this mechanical: draw at the top, its variables below, their variables below those. The derivative of with respect to is the sum over all branches from down to of the product of the derivatives along each branch.
In matrix language this is a product of Jacobians, : a row times an matrix.
import sympy as sp
s, t = sp.symbols("s t")
x = s**2 * t
y = s + sp.exp(t)
X, Y = sp.symbols("X Y")
f = X * sp.sin(Y) + Y**2
# chain rule: sum over the two branches
chain_s = (sp.diff(f, X) * sp.diff(x, s) + sp.diff(f, Y) * sp.diff(y, s)).subs({X: x, Y: y})
direct_s = sp.diff(f.subs({X: x, Y: y}), s)
print("dz/ds via chain rule:", sp.simplify(chain_s))
print("difference from direct:", sp.simplify(chain_s - direct_s))
# Jacobian product at (s, t) = (1, 0)
Jf = sp.Matrix([[sp.diff(f, X), sp.diff(f, Y)]]).subs({X: x, Y: y})
Jx = sp.Matrix([x, y]).jacobian([s, t])
print("J at (1, 0):", (Jf * Jx).subs({s: 1, t: 0}).applyfunc(sp.nsimplify))dz/ds via chain rule: s**2*t*cos(s + exp(t)) + 2*s*t*sin(s + exp(t)) + 2*s + 2*exp(t)
difference from direct: 0
J at (1, 0): Matrix([[4, sin(2) + 4]])
At we have and . By hand, with and :
which is the row of the Jacobian product.
Implicit differentiation
Suppose defines implicitly as a differentiable function of . Differentiate with the chain rule: , so
Likewise, if defines :
The Implicit Function Theorem guarantees that such a function exists near a point where , has continuous partials, and (or ).
Example: , the folium. , , so . At : .
In software: backpropagation is the chain rule
A neural network computes the loss through a graph of simple operations. Reverse-mode automatic differentiation (backpropagation) walks the graph from the output back to the inputs, multiplying local derivatives along each edge and summing where branches meet, exactly the tree-diagram rule. Reverse mode computes the whole gradient of one scalar output in about the cost of one or two forward passes, however many inputs there are; that is why training networks with billions of weights is feasible.
import math
# forward pass for L = (w*x + b - y)^2 * 0.5 with a tanh activation
w, b, x, y = 0.8, -0.2, 1.5, 0.3
z = w * x + b # node 1
a = math.tanh(z) # node 2
L = 0.5 * (a - y)**2 # node 3
# backward pass: multiply local derivatives along the chain
dL_da = a - y
da_dz = 1 - a**2
dL_dz = dL_da * da_dz
dL_dw, dL_db = dL_dz * x, dL_dz * 1.0
print(f"L = {L:.6f} dL/dw = {dL_dw:.6f} dL/db = {dL_db:.6f}")
# check with central differences
def loss(w, b):
return 0.5 * (math.tanh(w * x + b) - y)**2
h = 1e-6
print(f"numeric dL/dw = {(loss(w + h, b) - loss(w - h, b)) / (2 * h):.6f}")
print(f"numeric dL/db = {(loss(w, b + h) - loss(w, b - h)) / (2 * h):.6f}")L = 0.106535 dL/dw = 0.290787 dL/db = 0.193858
numeric dL/dw = 0.290787
numeric dL/db = 0.193858
More: Backpropagation and Automatic Differentiation.
Other places it appears:
- Physics and graphics: changing coordinates (world ↔ local, Cartesian ↔ polar) transforms derivatives with the Jacobian of the map, e.g. normals and velocities under a transform.
- Related rates in simulations: of a changing shape from and .
Exercises
Exercise 1: Along a curve
Let with , , . Compute by the chain rule and check by substituting first.
Answer
.
Solution
Exercise 2: Polar coordinates
Let with , . Express and in terms of and , and show that .
Answer
and ; the identity follows by squaring and adding.
Solution
So is the same in both coordinate systems, as it must be.
Exercise 3: Implicit slope
Find for the curve at the point .
Answer
.
Solution
Exercise 4: Backprop by hand (code)
For with , , , , compute with the chain rule (use ) and confirm with a central difference.
Answer
.
Solution
import math
x, y, w = 2.0, 1.0, 0.5
sig = lambda z: 1 / (1 + math.exp(-z))
a = sig(w * x)
print(round(2 * (a - y) * a * (1 - a) * x, 4))
L = lambda w: (sig(w * x) - y)**2
print(round((L(w + 1e-6) - L(w - 1e-6)) / 2e-6, 4))-0.2115
-0.2115
Test yourself
, , . What is ?
defines implicitly. equals…
Why is reverse-mode autodiff (backpropagation) used to train neural networks?
- It avoids the chain rule
- It gives the gradient of one scalar loss with respect to all weights at roughly the cost of a forward pass
- It is exact while forward mode is approximate
- It needs no memory