Residual connections (ResNet): a shortcut that keeps gradients alive

Stacking more layers sounds like it should help, but deep networks stacked without shortcuts got worse, even on their training data. Here you watch, as bars, how one shortcut that adds the input back lets the gradient reach the early layers. Then you follow ∂y/∂x = 1 + ∂F/∂x with numbers.

Short answer. A residual connection is a shortcut that adds a layer's input straight to its output: y = x + F(x). The layers (F) do not rebuild the input from scratch. They learn only how much to change it. Take the slope and you get ∂y/∂x = 1 + ∂F/∂x, so even with many layers the gradient rides the shortcut's 1 back to the early layers without shrinking. Very deep convolutional networks (ResNet) and transformers both use this shortcut in every block.

Each bar is the size of the gradient that flowed backward to that layer's input. The gradient at the output is set to 1. The vertical scale is a log scale, so read it by digits: 1, 10⁻⁵, 10⁻¹⁰. This is a scaled-down toy. There is only one value, and every layer's F has the same slope, 0.1. In a real network each layer has its own slope, and the values are vectors, so matrices (Jacobians) get multiplied instead of single numbers.

Try this. ① At the start (10 layers, no shortcut) the gradient that reaches layer 1 is 1 × 10⁻¹⁰. Each extra layer drops the bar by one digit. ② Press "With shortcut" and the same 10 layers give 2.594. ③ Raise the layers to 20: without the shortcut 1 × 10⁻²⁰, with it 6.727. Even with the shortcut the size does not sit exactly at 1. It grows a little. ④ Go down to 1 layer: 0.1 and 1.1, which is just one layer's slope.

Definitions

The text below explains with bars and the lab. These are the textbook definitions. x is the value that enters a block, F is the function made by the few layers inside the block, and y is the block output.

residual connection, skip connection
A structure that groups a few layers into one block and sends the block input x along a path that skips those layers (the skip connection), then adds it as is to the block output. A block built this way is a residual block. The skip connection has no weights, and x and F(x) must have the same shape. When x is a single number, taking the derivative gives the second formula.

y=x+F(x)

∂y∂x=1+∂F∂x

residual
The difference between the output H(x) the block should give and the input x. The layers of a residual block learn this difference instead of H(x).

F(x)=H(x)−x

projection shortcut
A skip connection with a weight Ws that turns x into the shape of F(x) before adding, used when x and F(x) have different shapes. Convolutional networks use a 1×1 convolution with stride 2.

y=F(x)+Wsx

vanishing gradient
In backpropagation, the gradients of each layer are multiplied on the way back to the input. When numbers smaller than 1 are multiplied again and again, the gradient that reaches the early layers gets close to 0. Then the weights of the early layers barely change.
degradation problem
A deeper network, made by stacking more layers, gets a higher training error than a shallower one. Unlike overfitting, where only the test error is worse, it is a problem of optimization failing to find good weights.

1. What a residual connection is: a shortcut that adds the input back

A layer in a neural network takes an input x and makes new values. In a plain network, what a few layers make, F(x), is the next input. A network with residual connections groups a few layers into a block and adds the incoming x to the block's output.

plain block:     y = F(x)
residual block:  y = x + F(x)      x comes by the shortcut, F(x) comes through the layers

The path that sends x on unchanged is the shortcut. It has no weights and costs one addition. F only has to learn "the output you want minus x", the difference that is left over. That leftover is the residual, hence the name. When a block should do nothing (output = input), F just has to be 0. A plain block doing the same job needs its weights to build an exact "pass it through". The original paper's idea is that pushing F toward 0 is easier to learn than that.

Left: a block of plainly stacked layers. Right: the ResNet residual block. The layers are the same. The only difference is the dashed purple line: the input x skips the layers and is added just before the last ReLU. So the layers only have to produce F(x), the change from the input, not the whole output.

2. The problem: deeper networks learned worse

More layers can express more. Take a shallow network, put layers on top that just pass values through, and you have a deeper network with the same result. So a deeper network should never be worse than a shallow one. Yet the ResNet paper showed that plain networks with more layers had a higher training error. That is not overfitting, where only the test error is bad. The deeper network fit even the training data worse, so learning (optimization) failed to find good weights. The paper called this the degradation problem.

The gradient story overlaps with this. Backpropagation starts from the gradient at the output and multiplies by each layer's slope on the way back to the input. If every layer multiplies by a number below 1, the gradient that reaches the early layers gets close to 0 (vanishing gradient), and those layers barely learn. "No shortcut" in the lab above shows this. Note, though, that the original paper checked that its plain networks with batch normalization did not have vanishing gradients, and it did not blame degradation on them. The explanation that the shortcut passes the gradient through unchanged is written as a formula in the same authors' next paper (source 2). The next section is that formula.

3. How the formula is built

Press "Next" and a piece is added to the formula (orange background marks the new piece), the bars change to that step's numbers, and the reason appears below. It is the same toy as the lab above (F(x) = 0.1·x).

  1. One block: y = x + F(x). With x = 2, F(2) = 0.2 and y = 2.2.
  2. The slope: ∂y/∂x = 1 + ∂F/∂x. Grow x by 0.01 and y grows by 0.011, a rate of 1.1. The 1 is the shortcut's part, so it stays whatever F is.
  3. Stack them and multiply: (1 + a)(1 + b)(1 + c). The chain rule multiplies the slope of each block. 1.1³ = 1.331. Without shortcuts it is 0.1³ = 0.001.
  4. Open it up and a 1 is left: 1 + (a + b + c) + (ab + bc + ca) + abc. The first term, 1, is the path that never goes through F and uses only shortcuts. It is always 1, however many blocks there are. A network without shortcuts has only the last term, abc.

Try this. ① The first bars are x 2, F(x) 0.2, y 2.2. ② The next step's bars are shortcut 1, F 0.1, sum 1.1. ③ The gradients after 1, 2, 3 blocks are 1.1, 1.21, 1.331, and without shortcuts 0.1, 0.01, 0.001. ④ The opened-up terms are 1, 0.3, 0.03, 0.001, and they add up to 1.331, the same as ③. For the product to be 0, one of a, b, c would have to be exactly −1. Even if every ∂F/∂x is −0.1, the gradient after 10 blocks is still 0.9¹⁰ = 0.3487.

4. Work it by hand: three blocks

Start at x = 2 and go through three blocks. Every F is F(x) = 0.1·x. Values go forward (input to output), and the gradient is the product of the slopes.

Blocks passedWith shortcut: value x + 0.1·xProduct of slopesWithout: value 0.1·xProduct of slopes
02121
12.21.10.20.1
22.421.210.020.01
32.6621.3310.0020.001

Without the shortcut, both the value and the gradient become 10 times smaller per block. With it, both grow a little, 1.1 times per block. In this toy every F is the same straight line, so the value and the gradient move at the same rate. In a real network F bends, so the two move separately.

5. Properties to know

PropertyIn plain wordsIn the lab
A 1 is added to the slope∂y/∂x = 1 + ∂F/∂x. The part that comes by the shortcut does not shrinkSection 3, step ②
The product rarely hits 0The opened-up product always has a term equal to 1. To get 0, some block's ∂F/∂x must be exactly −1Section 3, step ④
Doing nothing is easyIf F = 0 then y = x. A block that is not needed can push F toward 0Section 1
It does not vanish, but it is not always 11.1 multiplied 20 times is 6.727. It can growLab above, 20 layers
Adding needs the same sizeIf the sizes differ, the shortcut needs something to match them (a projection)Section 6

6. When the sizes do not match

Adding needs x and F(x) to have the same shape. A convolutional network often halves the image width and height and adds channels partway through, so in that block x and F(x) have different shapes. There are two ways to handle it.

So the usual pattern is a projection only in the few blocks where the size changes, and a plain add everywhere else. Transformers make every layer's output the same size from the start, so they can always add directly.

7. In transformers too

One layer of a transformer is attention plus a small network (feed-forward), and both are wrapped in a residual connection. There are two common shapes, depending on where the normalization goes.

# The original paper: normalize after adding (Post-LN)
x = LayerNorm(x + Attention(x))
x = LayerNorm(x + FeedForward(x))

# The other shape: normalize what goes into F, add outside the normalization (Pre-LN)
x = x + Attention(LayerNorm(x))
x = x + FeedForward(LayerNorm(x))

In Pre-LN, x is never normalized on the main path. It only gets things added to it, all the way to the end. So the "shortcut-only path" from section 3 stays whole from the first layer to the last. In Post-LN every add is followed by layer normalization, so that path also gets multiplied by the normalization's slope at each layer. Both shapes are in use.

8. When to use what

ChoiceGoodBadUse it when
No shortcut y = F(x)Simplest. No need to keep x around for the addHard to train when deep. The gradient is multiplied at every layer and tends to vanishSmall networks with only a few layers
Plain add y = x + F(x)No weights, one addition. A 1 is added to the slopex and F(x) must have the same shapeBetween blocks of the same shape. Most blocks of a deep network
Projection shortcut y = F(x) + W_s·xWorks even when the shapes differMore weights and compute, and the shortcut is no longer a pure 1Blocks where the channels or the width and height change
Zero paddingShapes can differ with no extra weightsThe new channels carry nothing from the shortcutSize-changing blocks when you want to save weights
Join instead of add (DenseNet style)Earlier values stay unmixedThe size grows at every layer, so memory and compute growWhen later layers should reuse earlier features as they are
Transformer, Pre-LNThe shortcut skips normalization, so it runs straight to the endDiffers from the original paper, so old settings may not carry overA common pick when writing a new transformer
Transformer, Post-LNSame as the original paper, so its settings and results are easy to followThe shortcut also goes through normalization at each layerReproducing the original paper's setup

Where it is used

FAQ

With residual connections, can I stack as many layers as I like?

No. They only keep the gradient from vanishing. Each block multiplies by 1 + ∂F/∂x, so the gradient can grow instead (6.727 times after 20 layers in this page's toy), and more layers also cost more compute and memory. Real ResNets also use batch normalization inside each block.

Are residual connections a way to stop overfitting?

No. The problem in the original paper was that the training error itself went up as layers were added. That is a problem with learning (optimization). Overfitting is when the training error is low but the test error is high, which is a different problem.

Why add? Could you join the two side by side instead?

You can. Networks like DenseNet join (concatenate) the outputs of earlier layers. Joining keeps the earlier values unmixed, but the size grows at every layer, so memory and compute grow. Adding keeps the size the same, so it is easier to stack many layers.

Words

Plain words used hereTextbook term
shortcutresidual connection, skip connection, shortcut connection
a shortcut that adds x unchangedidentity mapping, identity shortcut
a shortcut that matches the shapeprojection shortcut, W_s
F, how much to changeresidual function. F plus the shortcut is a residual block
the gradient gets close to 0 near the inputvanishing gradient
the gradient gets large near the inputexploding gradient
deeper means higher training errordegradation problem
multiply and pass backwardchain rule, backpropagation
the table of slopes of a layerJacobian
normalize after adding, add after normalizingPost-LN, Pre-LN

Sources

← Batch norm and layer normObject detection →