Backpropagation & Gradient Flow
How the error signal travels backward through depth, and how to keep it alive.
To train a network you need one number for every weight: how the loss would change if you nudged that weight. Modern networks have billions of weights. Backpropagation computes every one of those numbers for the price of about three forward passes, and that single fact makes deep learning possible. It also exposes the catch: the signal that reaches the early layers is a product of many factors, and long products tend to vanish or explode.
What you’ll learn
- Explain why training needs the gradient of the loss with respect to every weight, and why nudging the weights one at a time is hopeless at scale.
- Run the forward and backward pass on a small computational graph, multiplying each local derivative by the gradient arriving from above.
- Explain why gradients vanish or explode in deep networks, and compute how fast for a chain of sigmoid layers.
- Describe the four standard remedies (careful initialization, normalization layers, residual connections and gradient clipping) and what each does to the flow of gradients.
A billion knobs, one number
Training adjusts a network's weights to lower a loss , a single number that measures how wrong its predictions are on the training data. To know which way to turn each knob, you need, for every weight , the partial derivative : how fast the loss changes when that weight alone moves. Together these form the gradient , and gradient descent moves every weight a small step against it, , where is the learning rate.
The obvious way to measure a derivative is to nudge: change by a tiny amount , run the network again and see how much the loss moved.
A network has 10 million weights, and one forward pass over a batch of training data takes 0.1 seconds. Estimating one full gradient by nudging each weight in turn takes about how many days?
Show the answer
11.6 days
passes s s, about days, for one gradient. Training takes many thousands of gradient steps. Backpropagation gets the exact gradient of the same network in roughly seconds.
Backpropagation computes the exact derivative for every weight at once. For typical networks the backward pass costs about twice the forward pass, so the whole gradient costs about three forward passes, whether the network has a thousand weights or a billion. The next screen shows how; the rest of the lesson shows why the answer can still be useless in a very deep network, and what to do about it.
Think first If backpropagation is so much cheaper, why does anyone still compute finite differences?
As a test. Comparing a handful of backprop derivatives with finite differences, a gradient check, catches bugs in a hand-written backward pass: if the two disagree beyond rounding error, the code is wrong. You will write one in the Representation lesson Backpropagation & Automatic Differentiation.
The chain rule on a graph
Every network, however large, is built from simple operations: multiply, add, apply an activation. Draw each operation as a node and each intermediate value as an arrow, and you have a computational graph. Take the smallest useful one: a model with one weight predicts and is scored by the squared error .
Forward pass (left to right), with input , target and weight : the product , the residual , the loss . Keep these values; the backward pass needs them.
Backward pass (right to left): start from and, at each node, multiply the gradient arriving from above by that node's local derivative. This is the chain rule:
With and learning rate , one gradient-descent step moves to what?
Show the answer
C.
. The prediction becomes and the loss , down from . A negative derivative means the loss falls as grows, so the step increases .
Two rules handle every graph:
- Multiply along a path. A node receives a gradient from above, multiplies it by its local derivative and sends it on. Nothing else about the rest of the graph matters to that node.
- Add where paths meet. If a value feeds several later nodes, its gradient is the sum of the gradients coming back along each path.
The backward pass visits each node once, in reverse order, which is why it costs about as much as the forward pass instead of once per weight.
Gradients are products
Now stack many layers. The gradient that reaches the first layer has passed back through every layer above it, picking up one factor per layer: that layer's weight times the slope of its activation function. For a chain with one neuron per layer, with :
Long products are fragile. If the typical factor is , twenty layers shrink the gradient by : the vanishing gradient, where the first layers barely learn. If it is , twenty layers amplify it by : the exploding gradient, where one step can throw the weights far away. Only factors close to survive depth.
The classic sigmoid makes vanishing almost certain. Its slope is at most , at , and nearly zero in its flat tails, where the unit is saturated.
A chain of 10 sigmoid layers has weights of size 1 and every unit sits at , where the slope is largest. The gradient reaching the bottom is multiplied by . Write that factor as : what is ?
Show the answer
6
, so : a millionfold loss of signal in the best case. Saturated units, with slopes near zero, make it far worse.
Four ways to keep the gradient alive
For two decades, the vanishing gradient was a major reason why networks with more than a few layers were so hard to train. Four remedies, now standard, keep the per-layer factors near one or route around them:
| Remedy | What it does | Where you study it |
|---|---|---|
| Initialization | Scales the random starting weights so each layer's factor starts near : Xavier for tanh, He for ReLU, which must make up for zeroing half its inputs. | Initialization & Vanishing Gradients |
| Normalization layers | Rescale the activations inside the network to a fixed size at every layer, so the forward signal cannot shrink or blow up (BatchNorm, LayerNorm, RMSNorm). In deep networks they work best combined with residual connections. | BatchNorm, LayerNorm & RMSNorm |
| Residual connections | Compute instead of . The identity part has derivative exactly , so the gradient has a direct path back through every block: a gradient highway. | Residual Connections |
| Gradient clipping | If the gradient's length exceeds a threshold, shrink it to that length before the step. It does not prevent explosions, but it stops one from wrecking the weights. | Exploding Gradients & Clipping (Emergence) |
A residual block computes . Why does a useful gradient reach even when 's own derivative is tiny?
- Because : the identity term passes the gradient through unchanged
- Because the residual block has fewer weights, so each one gets a larger share of the gradient
- Because is always initialized to the identity
- Because the gradient skips the block entirely and is never trained
Show the answer
A. Because : the identity term passes the gradient through unchanged
The chain rule through gives . Even if is near zero, the carries the gradient down intact. Through many blocks the gradient becomes a sum over paths, including the all-identity path, instead of a product of small factors.
Where it came from, and where it goes
In the Representation lessons you will derive and build each piece:
- Backpropagation & Automatic Differentiation: backprop for a whole layer in matrix form, what it costs in time and memory, forward versus reverse mode, and PyTorch's autograd.
- Initialization & Vanishing Gradients: the variance calculation behind the Xavier and He scales, and why all-zero weights fail.
- BatchNorm, LayerNorm & RMSNorm: what each normalizes, over which axes, and why training gets easier.
- Residual Connections: the degradation problem, the identity shortcut and how skip connections made networks hundreds of layers deep trainable.
The Emergence lessons then reach the research frontier: differentiating through fixed points and optimization problems, tracing predictions back to training examples, the exact learning dynamics of deep linear networks, criticality and the finite-width effects that make the depth-to-width ratio matter, training without normalization layers, and the stability problems of normalization and residual streams in large transformers.
Think first Backpropagation needs every intermediate value from the forward pass. What does that imply for training a very deep network on long inputs?
Memory grows with depth times batch size times input length, because every layer's activations must be stored until the backward pass reaches it. Training therefore needs far more memory than running the trained model. Tricks such as gradient checkpointing, which stores only some activations and recomputes the rest during the backward pass, trade extra compute for memory.
Key takeaways
- Training needs for every weight. Finite differences cost one forward pass per weight; backpropagation gets all of them for about three forward passes.
- Backpropagation is the chain rule on a computational graph: the forward pass stores intermediate values, and the backward pass multiplies each local derivative by the gradient from above, adding where paths meet.
- The gradient reaching early layers is a product of one factor per layer, so it vanishes when the factors are below one and explodes when they are above; the sigmoid's slope is at most .
- Careful initialization, normalization layers, residual connections and gradient clipping keep the signal alive; modern networks use all four.
- Reverse-mode differentiation dates to 1970, its use in neural networks to Werbos and to Rumelhart, Hinton and Williams (1986); the vanishing gradient was identified by Hochreiter (1991) and Bengio, Simard and Frasconi (1994).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A network has weights. Roughly how many forward passes' worth of computation does one full gradient cost by finite differences, and by backpropagation?
- About either way
- About by finite differences; about by backpropagation
- About either way, since every weight needs its own derivative
- About by finite differences; about by backpropagation
Show the answer
B. About by finite differences; about by backpropagation
Nudging needs one extra forward pass per weight. Backpropagation runs one forward pass and one backward pass, which costs about twice the forward pass, so about three in total regardless of .
For with , and , what is ?
Show the answer
D.
Forward: , residual , . Backward: , passed through the subtraction unchanged, then multiplied by : .
In a 30-layer network the backward signal is multiplied by about at each layer. Compared with the last layer, how large is the gradient at the first?
- About a thousandth as large
- About times as large
- About times as large
- Exactly zero
Show the answer
A. About a thousandth as large
. Factors compound multiplicatively: a modest per-layer shrinkage becomes a thousandfold loss over thirty layers.
Why does He initialization for ReLU networks use weight variance where LeCun's scale for tanh uses ( inputs per neuron)?
- ReLU networks are usually wider, so they need larger weights
- ReLU's slope is on positive inputs
- ReLU sets about half of its inputs to zero, halving the signal's mean square at every layer; doubling the weight variance compensates
- Larger weights make ReLU units saturate, which stabilizes training
Show the answer
C. ReLU sets about half of its inputs to zero, halving the signal's mean square at every layer; doubling the weight variance compensates
For a zero-mean symmetric pre-activation , . Each layer would lose half its mean square; a factor of in the weight variance puts it back, so the per-layer factor is .
Gradient clipping by norm rescales the gradient to length whenever . What does it change?
- It flips the sign of the largest gradient entries
- It shortens the step but keeps its direction, so one exploding gradient cannot throw the weights far away
- It removes the cause of exploding gradients from the network
- It sets small gradient entries to zero to save computation
Show the answer
B. It shortens the step but keeps its direction, so one exploding gradient cannot throw the weights far away
Clipping replaces by : same direction, bounded length. The network can still produce huge gradients; clipping only limits the damage of a single step. The Emergence lesson Exploding Gradients & Clipping shows why this can even speed training up.
Go deeper
- Backpropagation & Automatic Differentiation · Computational graphs, reverse mode and why a gradient costs only a few forward passes.
- Initialization & Vanishing Gradients · Xavier and He: choosing weight scales so signals survive many layers.
- BatchNorm, LayerNorm & RMSNorm · What each one normalizes over, and why it makes training faster and more stable.
- Residual Connections · Skip connections, gradient highways and networks hundreds of layers deep.
Further reading
- S. J. D. Prince, Understanding Deep Learning, ch. 7 "Gradients and initialization" (MIT Press, 2023), free at udlbook.github.io/udlbook.
- D. E. Rumelhart, G. E. Hinton & R. J. Williams, "Learning representations by back-propagating errors," Nature 323 (1986).