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

Backpropagation: The Chain Rule Doing Bookkeeping

~12 min · backprop, chain-rule, gradients

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

The Question Backprop Answers

You did a forward pass. You got a loss. Now you need to know: "For each weight in this network — possibly billions of them — how would the loss change if I nudged that weight a tiny bit?" Those numbers are the gradients. With them, gradient descent can update every weight to reduce loss.

Naively, you'd compute each gradient separately by perturbing one weight at a time. That's extra forward passes for weights. With billions of weights, this would take forever.

Backprop's Trick

Backpropagation computes ALL gradients in essentially one extra pass through the network — backwards. The math behind this is the chain rule (you saw it in the Calculus track). The engineering is: cache intermediate values during the forward pass, then walk backward, multiplying local Jacobians as you go.

For each weight in layer :

That's a chain of derivatives. Backprop computes each once, reuses them across all weights.

The Blame-Game Metaphor

Think of backprop as blame propagation. The loss says "we're off the bullseye by this much." Backprop walks backward through the network, asking each layer: "how much did you contribute to the miss?" Each layer answers in proportion to its weight × the gradient flowing in from above. Then it passes the (chained) blame to the layer behind it.

By the end, every weight knows its share of the error. Adjust each weight against its share, and the next forward pass gets closer to the bullseye.

Backprop = chain rule + dynamic programming. It computes gradients for every parameter in a single backward pass by reusing intermediate derivatives instead of recomputing them.

Code

One backward call, all gradients·python
import torch

# Manual two-layer net to see backprop in action
x = torch.randn(1, 4)
W1 = torch.randn(4, 8, requires_grad=True)
W2 = torch.randn(8, 1, requires_grad=True)

# Forward
h = torch.relu(x @ W1)
y_hat = h @ W2
loss = (y_hat ** 2).sum()        # arbitrary loss for demo

# Backward — chain rule, automated
loss.backward()
print(W1.grad.shape)             # torch.Size([4, 8])
print(W2.grad.shape)             # torch.Size([8, 1])
# Both gradients computed in ONE backward call.

External links

Exercise

Take a simple expression like y = (x*w1 + b1)**2 with x=2, w1=3, b1=1. Compute dy/dw1 by hand using the chain rule, then verify with PyTorch autograd.
Hint
By chain rule: dy/dw1 = 2(x*w1 + b1) * x = 2*7*2 = 28 at the given values. Autograd will agree exactly.

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.