TExcelTAD
Home › Calculus Essentials › The Chain Rule

The Chain Rule

The chain rule differentiates functions inside functions, like sin⁡(x2)\sin(x^2) or (3x+1)10(3x + 1)^{10}. It says rates of change multiply along a chain: if yy changes 3 times as fast as uu, and uu changes 2 times as fast as xx, then yy changes 6 times as fast as xx. It is also the single idea that makes training neural networks possible.

The rule

If y=f(g(x))y = f(g(x)), write u=g(x)u = g(x) so y=f(u)y = f(u). Then

dydx=dydu⋅dudxor(f∘g)′(x)=f′(g(x))⋅g′(x)\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx} \qquad\text{or}\qquad (f \circ g)'(x) = f'(g(x)) \cdot g'(x)

"Differentiate the outer function, leave the inside alone, then multiply by the derivative of the inside." Leibniz notation makes it look like fractions cancelling. They are not fractions, but the notation is a reliable memory aid.

Worked examples

y=(3x+1)10outer u10, inner u=3x+1y′=10(3x+1)9⋅3=30(3x+1)9y=x2+9outer u, inner u=x2+9y′=12x2+9⋅2x=xx2+9y=sin⁡(x2)outer sin⁡u, inner u=x2y′=cos⁡(x2)⋅2x\begin{aligned} y &= (3x + 1)^{10} && \text{outer } u^{10},\ \text{inner } u = 3x + 1 \\ y' &= 10(3x + 1)^9 \cdot 3 = 30(3x + 1)^9 \\[1.2ex] y &= \sqrt{x^2 + 9} && \text{outer } \sqrt{u},\ \text{inner } u = x^2 + 9 \\ y' &= \frac{1}{2\sqrt{x^2 + 9}} \cdot 2x = \frac{x}{\sqrt{x^2 + 9}} \\[1.2ex] y &= \sin(x^2) && \text{outer } \sin u,\ \text{inner } u = x^2 \\ y' &= \cos(x^2) \cdot 2x \end{aligned}

Chains can be longer. For y=sin⁡3(4x)=(sin⁡(4x))3y = \sin^3(4x) = (\sin(4x))^3 there are three layers: cube, sine and 4x4x.

y′=3sin⁡2(4x)⋅cos⁡(4x)⋅4=12sin⁡2(4x)cos⁡(4x)y' = 3\sin^2(4x) \cdot \cos(4x) \cdot 4 = 12\sin^2(4x)\cos(4x)

Checking with SymPy and numbers

python · try it yourself
import sympy as sp

x = sp.symbols('x')
for expr in [(3*x + 1)**10, sp.sqrt(x**2 + 9), sp.sin(x**2), sp.sin(4*x)**3]:
    print(f"d/dx {str(expr):<16} = {sp.factor(sp.diff(expr, x))}")
d/dx (3*x + 1)**10    = 30*(3*x + 1)**9
d/dx sqrt(x**2 + 9)   = x/sqrt(x**2 + 9)
d/dx sin(x**2)        = 2*x*cos(x**2)
d/dx sin(4*x)**3      = 12*sin(4*x)**2*cos(4*x)

sp.factor tidies the result into a product, which is how the chain rule naturally produces it. Now numbers: the derivative of sin⁡(x2)\sin(x^2) at x=1.5x = 1.5 from shrinking central differences, against 2xcos⁡(x2)2x\cos(x^2):

python · try it yourself
import math

f = lambda x: math.sin(x**2)
x0 = 1.5
exact = 2 * x0 * math.cos(x0**2)
for h in [1e-1, 1e-2, 1e-3, 1e-4, 1e-5]:
    approx = (f(x0 + h) - f(x0 - h)) / (2 * h)
    print(f"h = {h:.0e}   {approx:.10f}   error {abs(approx - exact):.1e}")
print(f"chain rule:  {exact:.10f}")
h = 1e-01   -1.8792804218   error 5.2e-03
h = 1e-02   -1.8844715803   error 4.9e-05
h = 1e-03   -1.8845203756   error 4.9e-07
h = 1e-04   -1.8845208632   error 4.9e-09
h = 1e-05   -1.8845208681   error 3.4e-11
chain rule:  -1.8845208682

The error drops by about 100 each time hh shrinks by 10: the central difference is second order, as promised in The Derivative.

Why it works (sketch)

Let Δu=g(x+h)−g(x)\Delta u = g(x + h) - g(x). Then

f(g(x+h))−f(g(x))h=[f(u+Δu)−f(u)Δu]⋅[Δuh]\frac{f(g(x + h)) - f(g(x))}{h} = \left[\frac{f(u + \Delta u) - f(u)}{\Delta u}\right] \cdot \left[\frac{\Delta u}{h}\right]

As h→0h \to 0, Δu→0\Delta u \to 0 because gg is continuous, so the first bracket tends to f′(u)f'(u) and the second to g′(x)g'(x). (A careful proof handles the case Δu=0\Delta u = 0, but the idea is exactly this.)

In software: backpropagation

A neural network is one long composition: input → linear layer → activation → linear layer → … → loss. To train it we need ∂ loss/∂w\partial\,\text{loss}/\partial w for every weight ww, and the chain rule gives it as a product of local derivatives along the path from ww to the loss.

Take a single neuron with squared-error loss:

z=wx+ba=σ(z)L=(a−y)2z = wx + b \qquad a = \sigma(z) \qquad L = (a - y)^2
∂L∂w=∂L∂a⋅∂a∂z⋅∂z∂w=2(a−y)⋅σ′(z)⋅x\frac{\partial L}{\partial w} = \frac{\partial L}{\partial a} \cdot \frac{\partial a}{\partial z} \cdot \frac{\partial z}{\partial w} = 2(a - y) \cdot \sigma'(z) \cdot x

Backpropagation computes those factors from the loss backwards, reusing each partial product for every weight upstream. That reuse is why one backward pass costs about the same as one forward pass, no matter how many millions of weights there are.

python · try it yourself
import math

sigmoid = lambda z: 1 / (1 + math.exp(-z))
x, y, w, b = 2.0, 1.0, 0.3, -0.1

def loss(w):
    a = sigmoid(w * x + b)
    return (a - y) ** 2

z = w * x + b
a = sigmoid(z)
dL_da, da_dz, dz_dw = 2 * (a - y), a * (1 - a), x
print(f"factors: dL/da = {dL_da:.6f}, da/dz = {da_dz:.6f}, dz/dw = {dz_dw}")
print(f"chain rule  dL/dw = {dL_da * da_dz * dz_dw:.8f}")
h = 1e-6
print(f"numeric     dL/dw = {(loss(w + h) - loss(w - h)) / (2 * h):.8f}")
factors: dL/da = -0.755081, da/dz = 0.235004, dz/dw = 2.0
chain rule  dL/dw = -0.35489383
numeric     dL/dw = -0.35489383

The full story, with many layers and vectors of weights, is in Backpropagation Is the Chain Rule, and the general machinery in Automatic Differentiation. The σ′=σ(1−σ)\sigma' = \sigma(1 - \sigma) factor is derived in Derivatives of Exponentials and Logs.

Note: Multiplying many factors is also the source of vanishing gradients: σ′(z)\sigma'(z) is at most 0.25, so a chain of ten sigmoid layers can scale a gradient by 0.2510≈10−60.25^{10} \approx 10^{-6}. This is one reason modern networks prefer ReLU, residual connections and normalisation.

Exercises

Exercise 1: One layer

Differentiate f(x)=(5−2x)6f(x) = (5 - 2x)^6.

Answer

f′(x)=−12(5−2x)5f'(x) = -12(5 - 2x)^5

Solution
outer u6→6u5,inner u=5−2x→−2f′=6(5−2x)5⋅(−2)=−12(5−2x)5\begin{aligned} &\text{outer } u^6 \to 6u^5, \qquad \text{inner } u = 5 - 2x \to -2 \\ f' &= 6(5 - 2x)^5 \cdot (-2) = -12(5 - 2x)^5 \end{aligned}

Exercise 2: A root in the denominator

Differentiate g(x)=11+x3g(x) = \dfrac{1}{\sqrt{1 + x^3}}.

Answer

g′(x)=−3x22(1+x3)3/2g'(x) = -\dfrac{3x^2}{2(1 + x^3)^{3/2}}

Solution
g=(1+x3)−1/2g′=−12(1+x3)−3/2⋅3x2=−3x22(1+x3)3/2\begin{aligned} g &= (1 + x^3)^{-1/2} \\ g' &= -\tfrac{1}{2}(1 + x^3)^{-3/2} \cdot 3x^2 = -\frac{3x^2}{2(1 + x^3)^{3/2}} \end{aligned}

Exercise 3: Three layers

Differentiate h(x)=cos⁡2(3x+1)h(x) = \cos^2(3x + 1) and evaluate at a point where 3x+1=π/43x + 1 = \pi/4.

Answer

h′(x)=−6cos⁡(3x+1)sin⁡(3x+1)=−3sin⁡(6x+2)h'(x) = -6\cos(3x + 1)\sin(3x + 1) = -3\sin(6x + 2); at that point h′=−3h' = -3.

Solution
h=(cos⁡u)2,u=3x+1h′=2cos⁡u⋅(−sin⁡u)⋅3=−6sin⁡ucos⁡u=−3sin⁡(2u)at u=π4:h′=−3sin⁡π2=−3\begin{aligned} h &= (\cos u)^2, \qquad u = 3x + 1 \\ h' &= 2\cos u \cdot (-\sin u) \cdot 3 = -6\sin u\cos u = -3\sin(2u) \\ \text{at } u = \tfrac{\pi}{4}{:}\quad h' &= -3\sin\tfrac{\pi}{2} = -3 \end{aligned}

Exercise 4: Gradient of a two-layer chain

For L(w)=(tanh⁡(wx)−y)2L(w) = (\tanh(wx) - y)^2 with x=1.5x = 1.5, y=0.5y = 0.5, compute dL/dwdL/dw at w=0.4w = 0.4 with the chain rule (tanh⁡′=1−tanh⁡2\tanh' = 1 - \tanh^2) and check it numerically.

Answer

About 0.07910.0791 (both methods agree).

Solution
dLdw=2(tanh⁡(wx)−y)⋅(1−tanh⁡2(wx))⋅x\frac{dL}{dw} = 2(\tanh(wx) - y) \cdot (1 - \tanh^2(wx)) \cdot x
python · try it yourself
import math

x, y, w = 1.5, 0.5, 0.4
t = math.tanh(w * x)
print(round(2 * (t - y) * (1 - t * t) * x, 4))
L = lambda w: (math.tanh(w * x) - y) ** 2
h = 1e-6
print(round((L(w + h) - L(w - h)) / (2 * h), 4))
0.0791
0.0791

Test yourself

What is ddx(x2+1)5\frac{d}{dx}(x^2 + 1)^5?

  1. 5(x2+1)45(x^2 + 1)^4
  2. 10x(x2+1)410x(x^2 + 1)^4
  3. 5(2x)45(2x)^4
  4. (2x)5(2x)^5

In backpropagation, why compute derivatives from the loss backwards?

  1. It is more accurate than forward order
  2. The forward pass cannot be differentiated
  3. Each shared partial product is computed once and reused for all upstream weights
  4. To avoid using the chain rule

What is ddxesin⁡x\frac{d}{dx} e^{\sin x}?

  1. esin⁡xe^{\sin x}
  2. cos⁡x⋅esin⁡x\cos x \cdot e^{\sin x}
  3. ecos⁡xe^{\cos x}
  4. sin⁡x⋅esin⁡x−1\sin x \cdot e^{\sin x - 1}