TExcelTAD
Home › Calculus Essentials › The Multivariable Chain Rule

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 z=f(x,y)z = f(x, y), and both xx and yy depend on a parameter tt, say a point moving along a curve. Then zz depends on tt through two routes, and the contributions add:

dzdt=∂f∂x⋅dxdt+∂f∂y⋅dydt\frac{dz}{dt} = \frac{\partial f}{\partial x} \cdot \frac{dx}{dt} + \frac{\partial f}{\partial y} \cdot \frac{dy}{dt}

In vector form, dz/dt=∇f⋅r′(t)dz/dt = \nabla f \cdot \mathbf{r}'(t): the rate at which ff changes as you travel along the curve r(t)\mathbf{r}(t) is the gradient dotted with the velocity. With one variable this reduces to the ordinary chain rule.

Example. The temperature on a plate is T(x,y)=x2y+3yT(x, y) = x^2y + 3y, and an ant walks along x=cos⁡tx = \cos t, y=sin⁡ty = \sin t.

∂T∂x=2xy,∂T∂y=x2+3,dxdt=−sin⁡t,dydt=cos⁡tdTdt=2xy⋅(−sin⁡t)+(x2+3)⋅cos⁡tAt t=0: x=1, y=0→dTdt=0+4⋅1=4\begin{aligned} &\frac{\partial T}{\partial x} = 2xy, \quad \frac{\partial T}{\partial y} = x^2 + 3, \quad \frac{dx}{dt} = -\sin t, \quad \frac{dy}{dt} = \cos t \\ &\frac{dT}{dt} = 2xy \cdot (-\sin t) + (x^2 + 3) \cdot \cos t \\ &\text{At } t = 0{:}\ x = 1,\ y = 0 \quad\to\quad \frac{dT}{dt} = 0 + 4 \cdot 1 = 4 \end{aligned}
python · try it yourself
import 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 xx and yy depend on two parameters ss and tt, each partial derivative of zz collects one term per path:

∂z∂s=∂z∂x⋅∂x∂s+∂z∂y⋅∂y∂s∂z∂t=∂z∂x⋅∂x∂t+∂z∂y⋅∂y∂t\begin{aligned} \frac{\partial z}{\partial s} &= \frac{\partial z}{\partial x} \cdot \frac{\partial x}{\partial s} + \frac{\partial z}{\partial y} \cdot \frac{\partial y}{\partial s} \\ \frac{\partial z}{\partial t} &= \frac{\partial z}{\partial x} \cdot \frac{\partial x}{\partial t} + \frac{\partial z}{\partial y} \cdot \frac{\partial y}{\partial t} \end{aligned}

A tree diagram keeps track: draw zz at the top, its inputs below it, their inputs below them, and label each edge with a partial derivative. To get ∂z/∂s\partial z/\partial s, multiply along every path from zz down to ss and add the products. Polar coordinates are the classic case: x=rcos⁡θx = r\cos\theta, y=rsin⁡θy = r\sin\theta, so ∂f/∂r=fxcos⁡θ+fysin⁡θ\partial f/\partial r = f_x \cos\theta + f_y \sin\theta.

Implicit differentiation, the easy way

If F(x,y)=0F(x, y) = 0 defines yy as a function of xx, differentiate F(x,y(x))=0F(x, y(x)) = 0 with the chain rule: Fx+Fy⋅dy/dx=0F_x + F_y \cdot dy/dx = 0, so

dydx=−FxFy(where Fy≠0)\frac{dy}{dx} = -\frac{F_x}{F_y} \qquad (\text{where } F_y \ne 0)

For the circle x2+y2−25=0x^2 + y^2 - 25 = 0 this gives dy/dx=−2x/2y=−x/ydy/dx = -2x/2y = -x/y, 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:

u=w⋅x+b(weighted input)a=σ(u)(activation, σ(u)=1/(1+e−u))L=(a−y)2(loss for one example with target y)\begin{aligned} u &= w \cdot x + b && \text{(weighted input)} \\ a &= \sigma(u) && (\text{activation, } \sigma(u) = 1 / (1 + e^{-u})) \\ L &= (a - y)^2 && (\text{loss for one example with target } y) \end{aligned}

LL depends on ww through the chain w→u→a→Lw \to u \to a \to L, so

∂L∂w=∂L∂a⋅∂a∂u⋅∂u∂w=2(a−y)⋅σ(u)(1−σ(u))⋅x∂L∂b=2(a−y)⋅σ(u)(1−σ(u))⋅1\begin{aligned} \frac{\partial L}{\partial w} &= \frac{\partial L}{\partial a} \cdot \frac{\partial a}{\partial u} \cdot \frac{\partial u}{\partial w} = 2(a - y) \cdot \sigma(u)(1 - \sigma(u)) \cdot x \\ \frac{\partial L}{\partial b} &= 2(a - y) \cdot \sigma(u)(1 - \sigma(u)) \cdot 1 \end{aligned}

Backpropagation computes these products from the loss backwards, reusing the shared factor ∂L/∂u\partial L/\partial u 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 yourself
import 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

z=x2y−y2z = x^2y - y^2, x=2tx = 2t, y=t3y = t^3. Use the chain rule to find dz/dtdz/dt at t=1t = 1.

Answer

dz/dt=14dz/dt = 14 at t=1t = 1.

Solution
∂z∂x=2xy,∂z∂y=x2−2y,dxdt=2,dydt=3t2At t=1: x=2, y=1dzdt=(2⋅2⋅1)⋅2+(4−2)⋅3=8+6=14\begin{aligned} &\frac{\partial z}{\partial x} = 2xy, \quad \frac{\partial z}{\partial y} = x^2 - 2y, \quad \frac{dx}{dt} = 2, \quad \frac{dy}{dt} = 3t^2 \\ &\text{At } t = 1{:}\ x = 2,\ y = 1 \\ &\frac{dz}{dt} = (2 \cdot 2 \cdot 1) \cdot 2 + (4 - 2) \cdot 3 = 8 + 6 = 14 \end{aligned}

Check by substitution: z=4t2⋅t3−t6=4t5−t6z = 4t^2 \cdot t^3 - t^6 = 4t^5 - t^6, dz/dt=20t4−6t5=14dz/dt = 20t^4 - 6t^5 = 14 at t=1t = 1.

Exercise 2: Two parameters

f(x,y)=x⋅yf(x, y) = x \cdot y, x=s+tx = s + t, y=s−ty = s - t. Find ∂f/∂s\partial f/\partial s and ∂f/∂t\partial f/\partial t with the chain rule.

Answer

∂f/∂s=2s\partial f/\partial s = 2s, ∂f/∂t=−2t\partial f/\partial t = -2t.

Solution
∂f∂s=y⋅1+x⋅1=(s−t)+(s+t)=2s∂f∂t=y⋅1+x⋅(−1)=(s−t)−(s+t)=−2t\begin{aligned} \frac{\partial f}{\partial s} &= y \cdot 1 + x \cdot 1 = (s - t) + (s + t) = 2s \\ \frac{\partial f}{\partial t} &= y \cdot 1 + x \cdot (-1) = (s - t) - (s + t) = -2t \end{aligned}

That matches f=(s+t)(s−t)=s2−t2f = (s + t)(s - t) = s^2 - t^2.

Exercise 3: Implicit slope

Find dy/dxdy/dx on the curve x3+y3=6xyx^3 + y^3 = 6xy at the point (3,3)(3, 3).

Answer

dy/dx=−1dy/dx = -1.

Solution
F=x3+y3−6xy,Fx=3x2−6y,Fy=3y2−6xAt (3,3): Fx=27−18=9,Fy=27−18=9dydx=−Fx/Fy=−1\begin{aligned} &F = x^3 + y^3 - 6xy, \qquad F_x = 3x^2 - 6y, \qquad F_y = 3y^2 - 6x \\ &\text{At } (3, 3){:}\ F_x = 27 - 18 = 9, \qquad F_y = 27 - 18 = 9 \\ &\frac{dy}{dx} = -F_x / F_y = -1 \end{aligned}

(3,3)(3, 3) is on the curve because 27+27=54=6⋅927 + 27 = 54 = 6 \cdot 9.

Exercise 4: A shared input (code)

A value vv feeds two branches: p=v2p = v^2, q=3vq = 3v, and the output is L=p⋅qL = p \cdot q. Compute dL/dvdL/dv at v=2v = 2 by adding the two path contributions, and check with a finite difference.

Answer

dL/dv=36dL/dv = 36 at v=2v = 2.

Solution
∂L∂p=q,∂L∂q=p,dpdv=2v,dqdv=3dLdv=q⋅2v+p⋅3=6⋅4+4⋅3=24+12=36\begin{aligned} &\frac{\partial L}{\partial p} = q, \quad \frac{\partial L}{\partial q} = p, \quad \frac{dp}{dv} = 2v, \quad \frac{dq}{dv} = 3 \\ &\frac{dL}{dv} = q \cdot 2v + p \cdot 3 = 6 \cdot 4 + 4 \cdot 3 = 24 + 12 = 36 \end{aligned}

(Directly: L=3v3L = 3v^3, dL/dv=9v2=36dL/dv = 9v^2 = 36.)

python · try it yourself
v = 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 z=f(x,y)z = f(x, y) with x=x(t)x = x(t), y=y(t)y = y(t), then dz/dtdz/dt equals…

  1. fx⋅fyf_x \cdot f_y
  2. fx⋅dx/dt+fy⋅dy/dtf_x \cdot dx/dt + f_y \cdot dy/dt
  3. fx+fyf_x + f_y
  4. dx/dt⋅dy/dtdx/dt \cdot dy/dt

In backpropagation, what happens when one variable feeds two later nodes?

  1. Only the larger contribution counts
  2. The contributions from both paths are multiplied
  3. The contributions from both paths are added
  4. Its gradient is zero

For F(x,y)=0F(x, y) = 0 defining y(x)y(x), dy/dxdy/dx is…

  1. Fx/FyF_x / F_y
  2. −Fy/Fx-F_y / F_x
  3. −Fx/Fy-F_x / F_y
  4. Fx⋅FyF_x \cdot F_y