All courses
Courses › How LLMs really work · foundation

The chain rule

Differentiate a composition by multiplying the rates of change along the chain.

Builds on: Derivatives

▶ Watch this lesson Start the whole course

What the animation says

A nudge through a chain

Often an output doesn't depend on x directly. x sets a middle quantity, u, and u sets the output, y. Real systems are long chains like this, each step feeding the next. You can read the rate of each single step. But nudge x at the start: how far does y move at the far end? Try it with money. One dollar buys zero point nine euros, and one euro buys a hundred sixty yen. So how many yen does one dollar buy?

Rates multiply

Multiply. Zero point nine times a hundred sixty is a hundred forty-four yen. Each step scales the nudge it receives. Now nudge x by a tiny amount. u moves by its rate times that nudge. That move is itself a nudge to u, so y moves by its own rate times it. So the rate of y per x is the rate of y per u, times the rate of u per x. That's the chain rule.

Where to measure the outer rate

Now a real case: y equals three x plus one, squared, at x equals one. Inner step: u is three x plus one. At x equals one, u is four. Its rate is three. Outer step: y is u squared. Its rate is two times u. But two u needs a number plugged in, and the outer step doesn't see x. It only sees u. Tap the value where the outer rate must be measured.

Multiply along the chain

The outer rate, measured at u equals four, is two times four: eight. The inner rate is three. Multiply along the chain: eight times three is twenty-four. So y should move about twenty-four times as much as x does. Let's test it. Nudge x by one thousandth. About how far does y move?

Check it, then remember

Run it. x becomes one point zero zero one, so u becomes four point zero zero three. Square that: y is sixteen point zero two four zero zero nine. It moved about zero point zero two four. That's twenty-four per unit, just as the chain rule said. To find how the end of a chain responds to a nudge at the start, multiply every step's rate. Measure each rate where that step currently is. Later, this one rule tells a long chain of calculations how each early setting affects the result.

How does a nudge travel through a chain?

Often an output doesn't depend on $x$ directly. $x$ sets some middle quantity $u$, and $u$ sets the output $y$. Real systems are long chains like this, step feeding step. You know how to read one step's rate of change. But if you nudge $x$ at the very start, **how much does $y$ at the far end move?** Can you get it from the rates of the individual steps alone?

Rates along a chain multiply

Nudge $x$ by a small amount $\varepsilon$. $u$ moves by about (rate of $u$ per $x$) $\times\,\varepsilon$. That move is itself a nudge to $u$, so $y$ moves by about (rate of $y$ per $u$) $\times$ (move of $u$). It works like currency conversion: 1 dollar → 0.9 euro, 1 euro → 160 yen, so 1 dollar → $0.9\times160=144$ yen. Each step's rate scales the nudge it receives.

The chain rule

If $u=g(x)$ and $y=f(u)$, so $y=f(g(x))$ (a composition), then $$\frac{dy}{dx}=\frac{dy}{du}\cdot\frac{du}{dx}=f'(g(x))\cdot g'(x)$$ In prime notation, $f'$ is the rate of $f$. Key detail: $f'$ is measured **at $u=g(x)$**, the point where the outer step actually is, not at $x$. Longer chains work the same way: multiply every step's rate.

Worked: y = (3x + 1)² at x = 1

Inner: $u=3x+1$. At $x=1$, $u=4$. Rate $du/dx=3$. Outer: $y=u^2$. Its rate is $2u$, measured at $u=4$: $dy/du=8$. Chain: $dy/dx=8\times3=24$. Check by nudging: $x=1.001$ gives $u=4.003$ and $y=16.024009$. So $y$ moved $0.024$ for a $0.001$ nudge, which is $24$ per unit. ✓

Remember

To find how the end of a chain responds to a nudge at the start, multiply the rate of every step. Measure each step's rate at the point where that step currently is. Later, this one rule is what tells a very long chain of calculations how each early setting affects the final result.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.