C.W.K.
Stream
Lesson 04 of 05 · published

The Chain Rule: How Derivatives Compose

~10 min · chain-rule, composition, backprop

Level 0Math Novice
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

The Most Important Calculus Rule for AI

You almost never see a function in isolation in ML. Most are compositions: where one function feeds into another. To differentiate , you need the chain rule:

In words: "the derivative of the outer function (evaluated at the inner) times the derivative of the inner function." Multiply, don't add. The slopes compose multiplicatively as you nest functions.

Why It's the Backbone of Backprop

A neural network is a massive composition of functions: input → linear layer → activation → linear layer → activation → ... → loss. To compute how a loss changes with respect to a weight in the very first layer, you have to chain derivatives across every layer in between. The chain rule is the bookkeeping that makes this possible.

Backpropagation (next track) is just the chain rule applied systematically, one layer at a time, with intermediate values cached during the forward pass.

Quick Examples

  • where for the sigmoid.
The chain rule says derivatives multiply across compositions. Stack 100 layers, the gradient at the input is a product of 100 local derivatives. This is why deep networks suffer from vanishing/exploding gradients — and why ReLU, batch norm, and residual connections were invented.

Code

Chain rule via autograd·python
import torch

x = torch.tensor(2.0, requires_grad=True)

# y = sin(x^2) — a composition
y = torch.sin(x ** 2)
y.backward()

# By chain rule: dy/dx = cos(x^2) * 2x = cos(4) * 4 ≈ -2.614
print(x.grad.item())     # -2.614...

External links

Exercise

Compute by hand the derivative of f(x) = (3x + 1)^5 using the chain rule. Then verify with autograd at x=1.
Hint
Outer: u^5, derivative 5u^4. Inner: 3x+1, derivative 3. Chain: 5(3x+1)^4 · 3 = 15(3x+1)^4. At x=1: 15·4^4 = 3840.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.