Backpropagation Through Layers
Each layer's error signal comes from the layer above it, sent back through the same weights (transposed) and scaled by each neuron's activation slope. Once a layer has its error signal, its weight gradient is that signal times the activations that came in, and the whole backward pass costs about as much as a forward pass or two.
What the symbols mean
- Error signal of layer l
∂L/∂z⁽ˡ⁾: how the loss reacts to each neuron's weighted sum in layer l.
- Next layer's weights
The matrix the forward pass used to go from layer l to layer l + 1.
- Elementwise product
Multiply entry by entry.
- Weighted sums of layer l
Stored during the forward pass.
What the symbols mean
- Incoming activations
What layer l received as input in the forward pass.
Explanation
Backpropagation through layers is the one-neuron calculation done for a whole layer at once, with matrices doing the bookkeeping. Each layer gets an error signal (delta, the usual letter for a difference), a vector saying how much the loss would change if each neuron's weighted sum were nudged. The first formula reads like a sentence: take the next layer's error , send it back through the same weights it came forward through (that's the ), then scale each entry by how steep that neuron's activation was (that's the ). The second formula says that once a layer knows its , its weight gradient is times whatever came into the layer. Two lines of algebra train every neural network you've heard of; bear with the indices, they're the only hard part.
Where the transpose comes from. Recall from Layers as Matrix Multiplication that the next layer computes , where . Neuron feeds every neuron of the next layer, through weight . So there's one path from to the loss through each , and by the first lesson of this unit we add over paths:
That sum runs down column of , which is row of . So the whole vector is . Going forward, each row of gathers the inputs into one neuron; going backward, each column gathers the blame from the neurons it fed. Then is one more link in the chain, which multiplies entry by . That's the .
The weight gradient is an outer product. In , the weight multiplies only , so and . That's the one-neuron rule "error signal times input", for every pair at once. Arranged as a matrix, it's a column times a row: . Notice it comes out the same shape as , which it has to, so that makes sense.
Jacobians: the chain rule for vectors. The Jacobian of a function from to is the matrix of all its partial derivatives, row for output , column for input . For the Jacobian is just . For applied entry by entry, it's a diagonal matrix with down the diagonal, because each depends only on its own . The chain rule for vector functions says Jacobians multiply, like the Multivariable Chain Rule with matrices in place of numbers. Backprop multiplies a gradient row vector by one Jacobian after another (a vector–Jacobian product). It never builds the giant Jacobian of the whole network; multiplying by and then by a diagonal matrix, which is just , is all it ever does.
Why it costs about as much as one forward pass. Count multiply-adds. Forward, layer computes : one multiply-add per weight. Backward, it computes (again one per weight) and (one per weight). So the backward pass costs about twice the forward pass, and the complete gradient (forward plus backward) costs about three forward passes, however many weights there are. Compare the obvious alternative: nudge each weight separately and rerun the network to see what the loss does. That's one forward pass per weight: a million passes for a million weights. In Big-O terms backprop is for weights and nudging is .
The recipe:
- Forward pass, storing every and .
- Output error. For a linear output with , or a sigmoid output with cross-entropy, it's simply .
- For each layer from the top down: record and , then pass down with .
- Update every weight and bias.
A common mistake. It's tempting to send the error back with instead of , because it's the same matrix connecting the same two layers. Shapes catch it every time: is and has entries, so only can multiply it and produce the entries layer needs. (With a square both multiply fine, which is exactly when the bug slips through.) A second one: multiplying by , the activation, instead of , its slope. Write the shape next to every line and both mistakes become hard to make.
See it
Worked examples
A network has input , a ReLU hidden layer with and , and a linear output with and . The target is and . Find every gradient.
- 1
Forward pass, hidden neuron 1: row 1 of dotted with , plus its bias. .
A digit-reading network maps pixels to hidden neurons to outputs. Count the multiply-adds in one forward pass and in one backward pass, and compare with finding the gradient by nudging each parameter and rerunning the network.
- 1
Layer 1 weights: is , so it has entries.
Practice
Type your answer and check it. Solve them all to complete the lesson.
Layer 3 of a network has 4 neurons and receives 3 activations from layer 2, so is . What shape is ?
A network's hidden layer 1 uses ReLU and had . The layer after it has weights and error signal . Find the hidden layer's error signal .
A layer has error signal and received activations . Find , entered row by row.
In a sigmoid hidden layer, neuron has and activation . Find .
A team wants to check their digit reader (, fully connected, with biases) by computing its whole gradient the slow way: nudge one parameter at a time and rerun the network. How many extra forward passes does that take?
A layer applies the sigmoid entry by entry, , to 3 neurons. What does its Jacobian look like?
(Matrix operations review.) A network's first layer has weights and error signal . Backprop one step further to get the gradient of the loss with respect to the input, .
The hardest one here. Get it, and you've got this lesson.
A network has input , a sigmoid hidden layer with and , and a linear output with , . The target is and . Find , entered row by row.
Where it's used
The "backward costs about twice the forward" count is the rule of thumb behind every estimate of what it costs to train a large model. A forward pass takes about 2 floating-point operations per parameter per token (a multiply and an add), the backward pass about 4, so training costs about 6 × parameters × tokens operations. Plug in a model with billions of parameters and trillions of tokens of text, and that one formula, which comes straight from this lesson's count, tells you how many GPU-months the run needs before anyone starts it.