The maths you need · lesson 7 of 12
Parts in this lesson
A2 * u * 3the chain rule: outer derivative times inner derivative
Bs * (1 - s)the sigmoid’s derivative
Cslope(f, x)the central difference, as a check (from lesson 1.6)

The chain rule

Last updated

A model is functions inside functions: multiply and add, then squash with a sigmoid, then measure the mistake. To train it you need the derivative of the whole chain, and the chain rule gives it: multiply the derivatives of the links. Backpropagation, the method that trains every neural network, is this one rule used over and over (Stage 6).

By the end of this lesson you can

  • Split a function into a chain of simpler ones
  • Use the chain rule to differentiate a function inside a function
  • Derive the sigmoid’s derivative, σ(z)(1−σ(z)), and check it
  • Differentiate a chain of three, as one neuron’s loss

Functions in a chain

Take y=(3x+1)2. You could expand it, but it's easier to see it as two steps, one feeding the next:

u=3x+1,y=u2

In words: first work out u from x, then work out y from u. u is the inner function and squaring is the outer one. Each link on its own is easy to differentiate with lesson 1.6's rules: dudx=3 and dydu=2u.

A model looks the same, only longer: inputs → weighted sum → sigmoid → loss. Every link is simple; the chain is what makes it a model.

The rule: multiply the rates

Think of the links as gears. Turn x a little and u turns 3 times as much. Turn u a little and y turns 2u times as much. So y turns 3×2u times as much as x did. Nudge the first gear and compare:

Gear chaindy/dx = dy/du × du/dx
x1u = 3x + 14y = u²16× 3× 2u = × 8

Nudge x a little and watch how far each gear moves. At x = 1, u = 4 and y = 16.

That's the chain rule:

dydx=dydu⋅dudx

In words: the rate of the whole chain is the rates of the links multiplied together. Written with function names, if y=f(g(x)), then dydx=f′(g(x))g′(x): the outer derivative, worked out at the inner value, times the inner derivative.

For our chain at x=1: u=4, so dydx=2u×3=2×4×3=24. The central difference from lesson 1.6 agrees:

chain.ipynb

The derivative of the sigmoid

The sigmoid, σ(z)=11+e−z, is a chain too, and working through it gives a famously neat answer:

σ′(z)=σ(z)(1−σ(z))

In words: the slope of the sigmoid at z is its value times one minus its value. In NumPy: s = sigmoid(z), then s * (1 - s). (The working: write σ=v−1 with v=1+e−z. The power rule gives −v−2, the inner derivative is −e−z, and their product rearranges to σ(1−σ).)

sigmoid.ipynb

The formula and the nudge agree. Two things to notice: the slope is biggest, 0.25, at z=0, where the sigmoid is steepest; and it's small far from 0, where the curve is nearly flat. Stage 6 comes back to that: when a neuron's sigmoid sits on a flat part, its slope is tiny and it learns slowly.

A chain of three

Here's one neuron's mistake as a chain of three links. Input x=2, weight w, bias b=−1, and target t=1:

z=wx+b,p=σ(z),L=(p−t)2

In words: a weighted sum, squashed into a probability, then the squared mistake. To train w we need dLdw: how the mistake changes when the weight changes. The chain rule multiplies the three links:

dLdw=dLdp⋅dpdz⋅dzdw=2(p−t)⋅p(1−p)⋅x
neuron.ipynb

At w=0.5 the answer is −0.5: negative, so making w a little bigger makes the mistake smaller. Gradient descent (Stage 3) takes exactly that hint. Backpropagation (Stage 6) is this same multiplication, done from the loss backwards through every link of a network, reusing each product on the way.

Wrong way: forgetting the inner derivative

The commonest slip is to differentiate the outer function and stop. For y=(3x+1)2 that gives 2(3x+1), dropping the ×3:

forgot.ipynb

8 against 24: off by exactly the missing factor of 3. This is why the course checks every hand-worked derivative with a central difference. A slip like this doesn't crash anything; a model trained with it just learns badly, and you'd never know why.

Check what you learned

y=u3 and u=2x. What is dydx?
Predict, then check in step 3’s notebook: at a point where σ(z)=0.9, what is σ′(z)?
In step 4, dLdw came out negative. What does that tell you?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning