Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Neural networks / How neural networks learn

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.

Add layers and watch the error

Interactive
training error (digits it practised on) test error (new digits) plain stack

Layers
6
Plain, training
Plain, test

Measurements from small networks: fully connected, 64 neurons wide, trained for 8 epochs on 10,000 handwritten digits (MNIST), averaged over 2 runs. They illustrate the effect; they are not a reproduction of the paper.

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.

56 layers can copy 20 layersTop row: a trained network of 20 layers. Bottom row: the same 20 layers followed by 36 identity layers that pass their input through unchanged. Both rows compute the same function.20 layers56 layers36 layers that do nothingas trainedsame answers, so the deeper one can always match the shallower one
What to notice: the bottom network is the top one with 36 layers added at the end (dashed). If each added layer simply passes its input along (an identity layer), the two networks give exactly the same answers, so the deeper one can always be at least as good. Hover or tap a part for an explanation.

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.

How much of the learning signal reaches each layer

Interactive
weaker than at the last layer at least as strong as at the last layer

Layers
56
First layer gets
Middle layer gets

Measurements from small networks: fully connected, 64 neurons wide, trained for 8 epochs on 10,000 handwritten digits (MNIST), averaged over 2 runs. They illustrate the effect; they are not a reproduction of the paper.

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:

y=x+f(x)y = x + f(x)

The input xx travels around the layer on a shortcut and is added back at the end.

A block can preserve its input. If f(x)=0f(x)=0, then y=xy=x. 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 1+∂f/∂x1 + \partial f / \partial x. The shortcut gives the gradient a direct path with a factor of 1, which can help it flow through deep networks.

The residual stream compared with a plain stackA line, the stream, runs from the input x to the output y. 2 blocks (layer 1, layer 2) hang off it. Each one reads the stream and its result is added back to the stream at a plus. Above it, a plain stack, in which each layer replaces the signal instead of adding to it.With shortcuts: the residual streamxylayer 1+
x+f1x + f_1
layer 2+
x+f1+f2x + f_1 + f_2
Plain stack: each layer replaces the signalxylayer 1
f1(x)f_1(x)
layer 2
f2(f1(x))f_2(f_1(x))
What to notice: in the plain stack above, each layer replaces the signal with its own output, so whatever it did not pass on is gone. In the residual stream below, the line is never interrupted. Each block hangs off it: it reads the stream, computes something, and the result is added back at a plus (green). Nothing is replaced, so the stream always carries everything that came before. Hover or tap any part for an explanation.

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.

Send a signal through the layers, forward and back

Interactive
A signal and its gradient through a plain chain and a chain with shortcutsTwo rows of layers. In the top row each layer replaces the signal, so the signal and the gradient shrink layer by layer when the weights are below one. In the bottom row each layer adds to the signal through a shortcut, so the signal is carried along and the gradient never falls below its value at the last layer. Bars show the gradient that reaches each layer on a log scale.

Set a layer’s weight to zero (press again to restore it):
Plain, output
–
Plain, first layer
–
Shortcuts, output
–
Shortcuts, first layer
–
Show the numbers, layer by layerHide the numbers
Plain xi=relu(wi xi−1)x_i = \mathrm{relu}(w_i\, x_{i-1}) and going back gi−1=gi⋅wig_{i-1} = g_i \cdot w_i (when the relu is on)
Shortcut xi=xi−1+relu(wi xi−1)x_i = x_{i-1} + \mathrm{relu}(w_i\, x_{i-1}) and going back gi−1=gi⋅(1+wi)g_{i-1} = g_i \cdot (1 + w_i)
What each layer sent forward, and the gradient that reached it (the last layer gets 1)
LayerWeightPlain signalPlain gradientShortcut signalShortcut gradient

A toy: one number, not an image, passes through layers that each have one weight and a relu, so every step can be followed. The arithmetic is the real chain rule, and it is the same arithmetic a deep network does, one weight at a time. The gradient shown is how much the output changes when the signal at that layer is nudged.

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.

The same networks, with shortcuts

Interactive
training error (digits it practised on) test error (new digits) plain stack with shortcuts

Layers
6
Plain, training
Plain, test
Shortcuts, training
Shortcuts, test

Measurements from small networks: fully connected, 64 neurons wide, trained for 8 epochs on 10,000 handwritten digits (MNIST), averaged over 2 runs. They illustrate the effect; they are not a reproduction of the paper.

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.

The residual streamA line, the stream, runs from the input x to the output y. 4 blocks (block 1, block 2, block 3, block 4) hang off it. Each one reads the stream and its result is added back to the stream at a plus.The residual streamxyblock 1+
x+f1x + f_1
block 2+
x+f1+f2x + f_1 + f_2
block 3+
x+⋯+f3x + \dots + f_{3}
block 4+
x+⋯+f4x + \dots + f_{4}
What to notice: the line along the top is the residual stream, and it is never interrupted. Each block hangs off it: it reads the stream, computes something, and the result is added back at a plus (green). Nothing is replaced, so the stream always carries everything that came before. Hover or tap any part for an explanation.

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 y=x+f(x)y = x + f(x) 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.

← Browse all subjectsBack to the beginning ↑