Skip to the workspace
Pebellslearn by building
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.

machine learning / The Chain Rule

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.