The Multivariable Chain Rule
When a quantity depends on some variables, which in turn depend on others, the chain rule tells you how a change at the bottom ripples up to the top: add up the contributions along every path. This one rule is the mathematics of backpropagation, so it is worth seeing clearly.
One parameter, two intermediate variables
Suppose , and both and depend on a parameter , say a point moving along a curve. Then depends on through two routes, and the contributions add:
In vector form, : the rate at which changes as you travel along the curve is the gradient dotted with the velocity. With one variable this reduces to the ordinary chain rule.
Example. The temperature on a plate is , and an ant walks along , .
python · try it yourselfimport sympy as sp
t, x, y = sp.symbols('t x y')
T = x**2 * y + 3 * y
xt, yt = sp.cos(t), sp.sin(t)
chain = sp.diff(T, x) * sp.diff(xt, t) + sp.diff(T, y) * sp.diff(yt, t)
chain = chain.subs({x: xt, y: yt})
direct = sp.diff(T.subs({x: xt, y: yt}), t) # substitute first, then differentiate
print("chain rule :", sp.simplify(chain))
print("same? ", sp.simplify(chain - direct) == 0)
print("dT/dt at t=0:", chain.subs(t, 0), " at t=pi/3:", sp.nsimplify(sp.simplify(chain.subs(t, sp.pi / 3))))
chain rule : 3*cos(t)**3 + cos(t)
same? True
dT/dt at t=0: 4 at t=pi/3: 7/8
Substituting first and differentiating directly gives the same answer, but the chain-rule form never builds the full composite expression. That matters when the composite is huge, as in a neural network.
Several parameters and tree diagrams
If and depend on two parameters and , each partial derivative of collects one term per path:
A tree diagram keeps track: draw at the top, its inputs below it, their inputs below them, and label each edge with a partial derivative. To get , multiply along every path from down to and add the products. Polar coordinates are the classic case: , , so .
Implicit differentiation, the easy way
If defines as a function of , differentiate with the chain rule: , so
For the circle this gives , the same as implicit differentiation, with less work.
In software: backpropagation
A neural network is a long composition of simple functions. Take a single neuron with a squared-error loss:
depends on through the chain , so
Backpropagation computes these products from the loss backwards, reusing the shared factor for every parameter. When a variable feeds several later nodes (a weight shared across positions, a hidden unit feeding many outputs), the contributions from each path are added, exactly as in the formula with two routes above. See Backpropagation Is the Chain Rule and Automatic Differentiation.
python · try it yourselfimport math
def sigma(u):
return 1 / (1 + math.exp(-u))
def loss(w, b, x=2.0, y=1.0):
return (sigma(w * x + b) - y) ** 2
w, b, x, y = 0.3, -0.5, 2.0, 1.0
# forward pass
u = w * x + b
a = sigma(u)
L = (a - y) ** 2
# backward pass: multiply local derivatives from the loss down
dL_da = 2 * (a - y)
da_du = a * (1 - a)
dL_du = dL_da * da_du
dL_dw, dL_db = dL_du * x, dL_du * 1
h = 1e-6
print(f"u = {u:.4f}, a = {a:.6f}, L = {L:.6f}")
print(f"dL/dw: backprop {dL_dw:.6f}, numeric {(loss(w + h, b) - loss(w - h, b)) / (2 * h):.6f}")
print(f"dL/db: backprop {dL_db:.6f}, numeric {(loss(w, b + h) - loss(w, b - h)) / (2 * h):.6f}")
u = 0.1000, a = 0.524979, L = 0.225645
dL/dw: backprop -0.473835, numeric -0.473835
dL/db: backprop -0.236918, numeric -0.236918
Exercises
Exercise 1: Rate along a path
, , . Use the chain rule to find at .
Answer
at .
Solution
Check by substitution: , at .
Exercise 2: Two parameters
, , . Find and with the chain rule.
Answer
, .
Solution
That matches .
Exercise 3: Implicit slope
Find on the curve at the point .
Answer
.
Solution
is on the curve because .
Exercise 4: A shared input (code)
A value feeds two branches: , , and the output is . Compute at by adding the two path contributions, and check with a finite difference.
Answer
at .
Solution
(Directly: , .)
python · try it yourselfv = 2.0
p, q = v**2, 3 * v
grad = q * (2 * v) + p * 3
L = lambda v: (v**2) * (3 * v)
h = 1e-6
print(grad, round((L(v + h) - L(v - h)) / (2 * h), 6))
36.0 36.0
Test yourself
If with , , then equals…
In backpropagation, what happens when one variable feeds two later nodes?
- Only the larger contribution counts
- The contributions from both paths are multiplied
- The contributions from both paths are added
- Its gradient is zero
For defining , is…