Backpropagation, Step by Step
How the chain rule turns a network's forward pass into gradients for every weight, and why the "backward" direction is what makes training tractable.
Backpropagation, Step by Step
Backpropagation, Step by Step
Backpropagation is the algorithm that computes how much each weight in a neural network contributed to the final error, so gradient descent knows which direction to move each one. It is nothing more than the chain rule, applied systematically through a computation graph.
Forward Pass
Consider a tiny two-layer network: an input , a hidden unit , and an output , trained with squared error loss .
The forward pass computes each quantity left to right: , caching every intermediate value along the way. Those cached values are what backpropagation reuses — recomputing them during the backward pass would waste work.
Backward Pass: The Chain Rule
To update , we need . The chain rule decomposes it into a product of local derivatives, one per step in the forward pass:
Each factor is easy to compute in isolation:
| Term | Value |
|---|---|
Multiplying them together gives the gradient with respect to , without ever writing out the full expression for in terms of .
Why "Back"-propagation
The key insight is order: computing first and reusing it for every downstream derivative is far cheaper than recomputing the chain from scratch for every weight. In a network with millions of parameters sharing intermediate layers, this reuse is what makes training tractable — the cost of backpropagation is roughly the same as a single forward pass, regardless of how many parameters you have.
A Toy Implementation
def backward(x, y, w, b, v):
h = sigmoid(w * x + b)
y_hat = v * h
dL_dyhat = 2 * (y_hat - y)
dyhat_dh = v
dh_dw = sigmoid_derivative(w * x + b) * x
dL_dw = dL_dyhat * dyhat_dh * dh_dw
dL_dv = dL_dyhat * h
return dL_dw, dL_dvAutodiff frameworks like PyTorch and JAX generalize exactly this pattern to arbitrary computation graphs: every operation records its local derivative, and a single backward traversal multiplies them along every path from the loss back to each parameter.
Related notes
Convolutional Neural Networks, Explained
How convolution, pooling, and stacked layers let a network build up from edges to full objects with far fewer parameters than a fully-connected net.
Scaled Dot-Product Attention
A compact note on the attention equation, why scaling matters, and how to read the tensor shapes.
Newsletter
New articles in your inbox
An email when I publish something new. No spam, unsubscribe anytime.
Double opt-in. See the privacy policy for how your email is handled.