Residual connections: when more layers made things worse
ResNet made deeper networks work with residual connections. Why did adding layers make a plain network worse?
Eight layers worked. Why not eighty?
AlexNet had eight layers with weights, and in 2012 it won by a wide margin. What happens if we keep adding layers?
A test: add layers, change nothing else
For this article, I trained 64-neuron-wide networks on 10,000 handwritten digits, changing only the number of layers. Each result averages two runs.
I measure training error on those practice digits and test error on unseen digits. Drag the slider to add layers.
At 2 layers the training error is about 1.6%. It falls at 4 and 6 layers, then climbs. At 64 layers the network gets more than a quarter of its practice digits wrong.
Worse on the data it practised on
The training error got worse, not just the test error. This is not overfitting: the deeper network cannot learn what the shallower one learned.
The ResNet paper found the same thing on real images and called it the degradation problem: a plain 56-layer network did worse than a 20-layer one, even on its training data. A plain network stacks layers one after another.
It should be impossible
Take a trained 20-layer network and add 36 layers that pass their input straight through. These are identity layers.
That 56-layer network gives exactly the same answers as the 20-layer one. The deeper network can represent the shallower solution, yet training fails to find it. What goes wrong?
Where does the learning signal go?
In a simple chain, the learning signal can shrink on its way back: if each layer passes on 70%, after 55 layers only a few parts in a billion remain. That is a vanishing gradient. The earliest layers receive almost no signal about how to change, so learning stalls.
Here is the signal through plain stacks of 8, 16, 32 and 56 layers. Each bar shows how much reaches that layer, compared with the last layer, which gets all of it. The scale is logarithmic because the differences are enormous.
At 8 layers the first layer gets about one eighth. At 32 layers it gets about 1 in 42,000; at 56, about 1 in 160 million. Even a modest loss at each layer adds up.
A shortcut
Instead of asking each layer to produce the whole answer, let it produce only a change, and add that change to its input:
The input travels around the layer on a shortcut and is added back at the end.
A block can preserve its input. If , then . The deeper network can keep the shallower solution available while other blocks learn changes.
The learning signal gets a highway. In the scalar case, the slope is . The shortcut gives the gradient a direct path with a factor of 1, which can help it flow through deep networks.
Try it: forward and back
This toy sends 1 through 12 single-weight layers, first plain and then with shortcuts. Press Play to follow the number and its gradient.
Things to look for:
- At a weight of 0.25, the plain chain leaves the first layer about 2 in 10 million of the gradient. The shortcut chain grows to 11.6. Real networks do not follow this neat scalar pattern.
- At zero, the plain chain loses the signal while the shortcut passes 1 through. Zero one middle layer and the shortcut still carries the signal onward.
Does it work on the real thing?
Now give the same digit networks shortcuts and compare the errors.
With shortcuts, training error stays under 2% from 2 to 64 layers. In this small test it does not improve with depth, but it no longer gets worse.
The residual stream
Straighten the shortcuts into one line from input to output, with blocks branching off it.
This line is the residual stream: a shared notebook that every layer reads and adds to. A block works out a change and adds it to what came before. The shortcut gives information a route around that block, though later blocks can still transform or cancel parts of it.
Most modern transformers use this same residual pattern. In the next articles the stream becomes a grid of numbers, one column for every word, but the picture stays the same.
What it bought
The ResNet paper introduced this in 2015. Its 34-layer network beat its 18-layer counterpart, and the approach scaled to 152 layers.
The takeaway: residual connections made very deep networks practical to optimise. Depth still brings optimisation and compute challenges, and that is why became a standard part of modern transformer blocks.
Next, we get words onto the stream: how words become numbers. Then we look at the first block that reads and updates them: attention.
Source: He, Zhang, Ren and Sun, “Deep Residual Learning for Image Recognition” (2015). The depth experiment and the gradient measurements are my own: small networks on digits, averaged over two runs, and illustrative of the effect rather than a reproduction of the paper.