Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Calculus  ›  Lesson

The Chain Rule

Chain rule of derivation 14 minThe derivative of a function inside a function is the outer derivative times the inner derivative, and backpropagation is this rule applied layer by layer.

The Chain Rule

A model's error is never a simple function of a weight. The weight makes a prediction, the prediction makes an error, and the error gets squared. That is a chain of functions, and the chain rule is how you differentiate through it. Backpropagation, the method that trains every neural network, is this one rule used over and over.

The rule

Outside derivative times inside derivative; multiply along the chain.

Use the simplest rent model: predicted rent =w×x= w \times x, where xx is size in tens of square metres. For flat B, x=6x = 6 and the true rent is 22, so the squared-error loss is

L(w)=(6w−22)2L(w) = (6w - 22)^2

Break it into links:

  • Inside: u=6w−22u = 6w - 22, the prediction error. Its slope is du/dw=6du/dw = 6.
  • Outside: L=u2L = u^2. Its slope is dL/du=2udL/du = 2u.

Multiply: dL/dw=2u×6=12(6w−22)dL/dw = 2u \times 6 = 12(6w - 22).

At w=3w = 3: the prediction is 18, so u=−4u = -4 and dL/dw=12×(−4)=−48dL/dw = 12 \times (-4) = -48. The slope is negative, so increasing ww lowers the loss. That makes sense: 18 is below 22, so the weight is too small.

Longer chains work the same way. A neural network might compute z=wx+bz = wx + b, then s=σ(z)s = \sigma(z), then L=(s−y)2L = (s - y)^2. Then dL/dw=2(s−y)×σ′(z)×xdL/dw = 2(s - y) \times \sigma'(z) \times x: one factor per link. Backpropagation computes these factors from the loss backwards, layer by layer, and reuses each product for the layer below, which is why it is fast.

The shortcut

Name each link, write its local slope, multiply. Each link only needs its own small derivative; you never differentiate the whole expression at once.

Slope of the loss at w = 4Step 1 of 4
  1. Name the links

    u = 6w − 22, and L = u².

The classic mistake is stopping at the outside derivative and writing 2(6w−22)2(6w - 22). That forgets the inside slope of 6, and the answer is six times too small.

Drills

Answer fast — trust the rule.

quick drill

What is the derivative of (3w + 1)² with respect to w?

quick drill

For L(w) = (6w − 22)², what is dL/dw at w = 4?

quick drill

At w = 3 the slope dL/dw is −48. Which way should training move w?

quick drill

If dy/du = 5 and du/dw = −2, what is dy/dw?

quick drill

What is backpropagation?

Remember

  • Outside derivative times inside derivative; multiply along the chain.
  • Shortcut: name each link, write its local slope, multiply.
  • Trap: forgetting the inside slope.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDifferentiate the squared error

The rent model predicts 6w for flat B, whose true rent is 22, and the loss is (6w - 22)². Use the chain rule to find the derivative, evaluate it at w = 3 and w = 4, and explain which way to move w in each case.