The idea
Training needs for every parameter , and a modern network has millions of them. Backprop is the observation that all those derivatives share structure. The chain rule for a composition multiplies local derivatives along the path — and if you evaluate that product from the loss backwards, every parameter's gradient reuses the partial products already computed for the layers above it. One backward sweep—same asymptotic order as the forward pass and often roughly twice its FLOPs in dense networks—produces all parameter gradients.
The bookkeeping runs on the error signal (loss gradient with respect to layer 's pre-activation). For a dense layer , three local rules do everything:
The third rule is the recursion: it hands the error to the previous layer, which multiplies elementwise by its activation derivative, , and repeats. Note the first rule needs — this is exactly why the forward pass caches every layer input.
Two consequences fall straight out of the product structure:
- Vanishing gradients: if each layer's factor has magnitude below 1 (saturated sigmoids contribute at most ), the product decays exponentially with depth and early layers stop learning.
- Exploding gradients: factors above 1 compound the same way upward; the symptom is loss spikes and NaNs, the blunt fix is gradient clipping.
Worked example
The interview classic — one hidden ReLU unit, sigmoid output, and BCE loss — uses:
Forward (cache everything):
The loss is:
Backward, one row per chain-rule hop:
| Quantity | Rule | Value |
|---|---|---|
Sanity checks worth saying out loud in an interview: every gradient is negative, so a gradient-descent step increases every parameter, which pushes up and toward the label — the signs make sense. And after one SGD step with (e.g. , ), rerunning the forward pass gives and : the loss halved.
Had been negative, would have killed and both first-layer gradients — the "dead ReLU" failure mode in miniature: no activation, no gradient, no learning on that path.
Cheat sheet
Core recursion: starting from , repeat for each layer:
| Op | Local backward rule |
|---|---|
| Dense | , , |
| ReLU | pass where , zero elsewhere |
| Sigmoid (generic) | |
| Sigmoid + BCE (fused) | directly at the logit |
| Softmax + CE (fused) | directly at the logits |
| Residual | gradients split and add at the junction: identity branch passes unchanged, residual branch contributes — the identity term is the gradient highway |
| Branch reused twice | gradients from both consumers sum |
- Cost: backward ≈ 2x forward FLOPs; total training step ≈ 3x forward.
- Memory: all layer inputs cached; gradient checkpointing recomputes activations to cut memory ~ at ~1 extra forward pass.
- Vanishing: symptoms — early-layer gradients orders of magnitude below late-layer ones, loss plateaus. Fixes — ReLU/GELU, He/Xavier init, batch/layer norm, residual connections.
- Exploding: symptoms — loss spikes/NaN. Fixes — gradient clipping (clip global norm to ~1.0), lower LR, better init.
- Gradient check: central difference , float64, eval mode, compare relative error (pass ≈ ).
- at the output logit is the single most reusable fact in gradient interviews.