Math for AILoading progress…

The chain rule

Calculate slopes, use the chain rule and distinguish derivatives from partial derivatives.

A chain of machines

Many functions are built by feeding one function into another: first compute u = 3x + 1, then square it: y = u². The pair is a composition. Two questions: what is the slope of y with respect to x, and how do we get it without expanding everything?

Think of slopes as magnification. A tiny step in x becomes a step 3 times as large in u, because the inner machine has slope 3. A tiny step in u becomes a step 2u times as large in y, because the outer machine has slope 2u. Magnifications multiply: y reacts to x by 2u · 3. That is the chain rule: dy/dx = (dy/du) · (du/dx).

x = 2
→
inner
u = 3x + 1
slope 3
→
u = 7
→
outer
y = u²
slope 2u = 14
→
y = 49
dy/dx = (dy/du) · (du/dx) = 14 · 3 = 42
check: central difference of the whole machine = 42.0000
wrong: outer slope evaluated at x instead of u gives 12
step in x: 0.050step in u: 0.1500 (about 3 × the x step)step in y: 2.1225 (about 42 × the x step)

Each stage magnifies a tiny step by its own slope. The magnifications multiply, so the slopes multiply. Bar lengths are drawn to scale, capped at the edge of the picture.

Choose x and a composition. The bars at the bottom show a tiny step growing at each stage, drawn to scale.

The worked example, with the right and wrong evaluation points

For y = (3x + 1)² at x = 2: the inner value is u = 7, the inner slope is 3, the outer slope 2u is 14, and 14 · 3 = 42. Python agrees:

f = lambda x: (3 * x + 1) ** 2
h = 1e-5
print((f(2 + h) - f(2 - h)) / (2 * h))
print(2 * 2 * 3)
print(2 * (3 * 2 + 1) * 3)
# output:
42.000000000896875
12
42

The mistake used 2x = 4 as the outer slope and got 12. The outer derivative must be evaluated at the inner output u = 3x + 1 = 7, not at x.

Predict first. What is the derivative of (3x + 1)² at x = 0?

Why this matters for learning

A neural network is a long chain of functions. The loss L = (w·x + b − t)² is a chain: inner r = w·x + b − t, outer r². The slope with respect to w is 2r · x and with respect to b is 2r · 1. Backpropagation is the chain rule applied systematically along such chains. You write exactly these functions in the practice.

Chapter context

A derivative measures a local rate of change. It describes what a function does under a very small input change, rather than its average behavior over an arbitrary interval. This local sensitivity is what training algorithms use.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}h. ddxf(g(x))=f′(g(x))g′(x),∂∂x(x2+3xy)=2x+3y.\frac{d}{dx}f(g(x))=f'(g(x))g'(x),\quad \frac{\partial}{\partial x}(x^2+3xy)=2x+3y.

For numerical checking, use (f(x+h)−f(x−h))/(2h)(f(x+h)-f(x-h))/(2h) and report a tolerance.

Worked example

Let f(x)=(3x+1)^2. At x=2, the inner value is 7, its derivative is 3, and the outer derivative is 14. Multiplying gives 42. A central difference with h=0.00001 gives approximately the same answer, within numerical error.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
f = lambda x: (3*x + 1)**2
x, h = 2, 1e-5
analytic = 6*(3*x + 1)
numeric = (f(x+h)-f(x-h))/(2*h)
assert analytic == 42
assert isclose(numeric, analytic, rel_tol=1e-7)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A function value is not its derivative. The chain rule multiplies sensitivities, not intermediate values. At a corner such as abs(x) at zero, a two-sided derivative does not exist.