Backpropagation
The chain rule, applied backwards through the whole net
Backpropagation computes the gradient of the loss with respect to every weight by applying the chain rule backward through the network, enabling gradient descent to actually train deep nets.
What you will learn
- Trace how errors flow backward from the loss to each weight
- Explain why backprop is a batch-wise local computation, not magic
- Identify when gradients vanish or explode and what to do
What is this?
Backpropagation is an algorithm that computes, for every weight in a network, how much the loss would change if that weight moved a little — the gradient. It works by walking the computational graph backward from the loss: at each node it applies the chain rule, multiplying the error arriving from above by the local derivative of that node. Repeat for millions of weights and you get one direction to descend.
Why do you need it?
- Without gradients you'd tune millions of weights blindly; backprop gives each one a precise update direction.
- It reuses shared intermediate values (activations), so it's efficient — one forward and one backward pass per batch.
- It's exact: the computed gradients are the true derivatives, not numerical approximations.
- Auto-differentiation frameworks (PyTorch, TF) automate it, but understanding it is how you debug training.
Mental model
Blame flowing down an organization chart
Error flowing backward through a network
The CEO (loss) reports a problem. Managers don't each get blamed directly — the share each one contributed is propagated layer by layer until every individual (weight) knows its own responsibility for the result.
How it works
- Forward pass: activations are computed for each layer and cached.
- Loss: the final prediction is scored against the target.
- Backward pass: starting from the loss, compute the gradient of the loss w.r.t. the last layer's weights (easy, local).
- Chain rule: the gradient for an earlier layer multiplies the gradient of the layer above by its local derivative.
- Update: each weight moves a small step opposite its gradient; the cached activations are reused so the whole thing costs about 2x a forward pass.
Gradients flow backward through the stored computational graph.
In production
- Every neural net you train — image classifiers, LLMs, recommenders — uses backprop for its updates.
- Transfer learning fine-tunes many layers at once using gradients computed through backprop.
- Gradient-based hyperparameter/architecture search reuses the same machinery for meta-learning.
Best practices
- Sanity-check gradients numerically or with a known one-shot case (e.g. a single neuron) before trusting custom code.
- Monitor per-layer gradient norms during training; order-of-magnitude drift signals instability.
- Use optimizers that leverage gradient statistics (Adam) but understand they add their own noise.
- Enable gradient clipping near loss spikes / large batch variance.
Common mistakes
- Thinking backprop 'tells each neuron its error' — it computes gradient field, neurons don't know anything.
- Recomputing rather than caching activations, wasting the 2x efficiency.
- Chaining too many ops through a custom loss and getting NaN gradients from fp16 overflow.
- Forgetting that depth multiplies many derivatives — see vanishing gradients.
Common issues
Training loss never drops below 0.7 on a 2-class problemSaturated sigmoid output: gradient ~ 0 for confident-but-wrong predictions
Use logits with cross-entropy (no manual sigmoid) so the gradient stays informative.
Loss goes to NaN mid-trainingExploding gradients — weights blew up in an early batch
Gradient clip, lower LR, or better initialization; check for raw exp/log on unstable values.
First layers barely change across epochsVanishing gradients from many small derivatives multiplied together
Use ReLU-family activations, normalization layers, residual/skip connections and reasonable initialization.
Debugging tips
- Print gradient norms by layer: if early layers get near-zero gradients while later are fine, gradients vanish.
- Verify the first backward step reduces loss on a fixed, tiny batch.
- Use one neuron/one layer to test that your manual loss gradient equals a numerical gradient.
- Freeze layers one at a time to isolate which layer's gradients explode.
Interview checkpoint
Hands-on lab
- Implement forward and backward passes for a tiny 2-layer MLP manually
- Derive and code the gradient of MSE loss yourself
- Verify your manual gradients against a numerical gradient check
Implement Backprop by Hand
30 min · intermediate
Quick quiz
What mathematical rule is backpropagation built on?
Backpropagation reuses cached forward activations, making it roughly 2x the forward pass cost.