The Chain Rule
The chain rule differentiates compositions: a function fed into another function. It is the most used rule in calculus, the one that makes , and routine, and the mathematical core of backpropagation in neural networks. This lesson states it, proves it carefully, and shows how to apply it to long chains.
For a gentler first look, see The Chain Rule and Backpropagation Is the Chain Rule.
Statement
If is differentiable at and is differentiable at , then , , is differentiable at and
In Leibniz notation, with and :
Read it as "derivative of the outside, evaluated at the inside, times the derivative of the inside". Rates multiply: if changes 3 times as fast as , and changes 2 times as fast as , then changes 6 times as fast as .
Why the naive proof fails, and a correct one
The tempting argument writes and lets . The flaw: may be zero for small (for example when is constant near , or oscillates like ), and then is .
Proof. Fix the problem by defining the slope of at with a helper that is safe when the step is zero:
Because is differentiable at , as , and for every (including 0)
Now put , so , and divide by :
As : is continuous at , so and ; and the last fraction tends to . The limit is . ∎
Worked examples
| Function | Outside | Inside | Derivative |
|---|---|---|---|
The last row proves the general exponential rule promised in Derivatives of Polynomials and Exponential Functions. Combined with the chain rule, the power rule for functions reads .
Longer chains
For three layers, , apply the rule twice:
Example: has layers cube, sine, exp, linear:
SymPy agrees, and a numeric check confirms the formula at one point:
import math
import sympy as sp
x = sp.symbols('x')
y = sp.sin(sp.exp(2*x))**3
dy = sp.diff(y, x)
print("SymPy:", dy)
hand = 3*sp.sin(sp.exp(2*x))**2 * sp.cos(sp.exp(2*x)) * sp.exp(2*x) * 2
print("matches hand formula:", sp.simplify(dy - hand) == 0)
F = sp.lambdify(x, y, "math")
h, a = 1e-6, 0.3
print("numeric y'(0.3) ≈", round((F(a + h) - F(a - h)) / (2*h), 5))
print("formula y'(0.3) =", round(float(dy.subs(x, a)), 5))SymPy: 6*exp(2*x)*sin(exp(2*x))**2*cos(exp(2*x))
matches hand formula: True
numeric y'(0.3) ≈ -2.55066
formula y'(0.3) = -2.55066
Forward and reverse: how a computer applies the rule
A chain produces a product of local derivatives. You can multiply them in two orders:
- Forward mode: start at , carry the pair (value, derivative) through each layer.
- Reverse mode: run forward to get all values, then multiply local derivatives from back to . This is backpropagation; with many inputs and one output (a loss), it gets every partial derivative in one backward pass.
import math
# y = sin(u2), u2 = u1**2, u1 = 3x + 1 at x = 0.5
x = 0.5
u1 = 3*x + 1
u2 = u1**2
y = math.sin(u2)
# local derivatives
d_u1_dx, d_u2_du1, d_y_du2 = 3, 2*u1, math.cos(u2)
# reverse sweep: start with dy/dy = 1 and walk back
grad = 1.0
for name, local in [("u2", d_y_du2), ("u1", d_u2_du1), ("x", d_u1_dx)]:
grad *= local
print(f"dy/d{name:2} = {grad: .6f}")
print("closed form 6(3x+1)·cos((3x+1)²):", round(6*u1*math.cos(u1**2), 6))dy/du2 = 0.999449
dy/du1 = 4.997247
dy/dx = 14.991741
closed form 6(3x+1)·cos((3x+1)²): 14.991741
Each step multiplies one more local derivative; the running product is the derivative of with respect to that node. A real framework does the same over a graph with millions of nodes (Automatic Differentiation).
Note: The vanishing gradient problem is the chain rule in action: a product of many factors smaller than 1 (such as sigmoid slopes, at most ) shrinks exponentially with depth. ReLU activations and residual connections were introduced largely to keep those products near 1.
In software
- Neural networks and LLMs: every gradient used in training is a chain-rule product through the layers (Machine Learning, Generative AI & LLMs).
- Easing and time remapping: if an animation plays along a remapped clock , its speed is .
- Physics with changing units or coordinates: converting a rate in one variable to a rate in another is always a chain-rule multiplication, which is the basis of Related Rates.
Exercises
Exercise 1: Power of a polynomial
Differentiate .
Answer
Solution
Exercise 2: Exponential with trig inside
Differentiate .
Answer
Solution
Exercise 3: Composite from a table
, , . Find .
Answer
Solution
Exercise 4: Quotient or chain?
Differentiate using the chain rule only, and evaluate at .
Answer
, and
Solution
import sympy as sp
x = sp.symbols('x')
d = sp.diff(1/sp.sqrt(x**2 + 9), x)
print(d, "|", d.subs(x, 4))-x/(x**2 + 9)**(3/2) | -4/125
Exercise 5: Reverse mode by hand
For write the forward values and the backward products at , as in the reverse-sweep example. Check against the closed form.
Answer
, ; , .
Solution
Test yourself
What is ?
Why does the proof need the helper instead of writing ?
- can be 0 for small , making undefined
- can be negative
- might not be continuous
- Leibniz notation is not rigorous at all
Backpropagation computes gradients by…
- finite differences for each weight
- symbolic simplification of the whole network
- multiplying local derivatives from the output back towards the inputs
- the quotient rule