Residual Networks

A residual connection, or skip connection, adds the input of a block back to the block’s output. The block then computes an additive change to its input instead of a new representation. A residual block with input and processing function outputs

where are the block’s weights and biases. The output of must have the shape of its input. A residual network chains such blocks. Substituting each block into the next shows that the output is the input plus several smaller networks, for example for two blocks. A network of blocks contains paths from input to output, because each block can be passed through or skipped.

For a block with two weight layers and no biases, , the Jacobian is

where is diagonal with if and otherwise. The identity term gives every gradient a direct path backward. When the block changes the height, width or number of channels, the skip carries a learned projection instead, . In a CNN, is a $1 \times 1$ convolution, with stride when the resolution halves.

A block should end with a linear layer or convolution, not a ReLU. A block that ends with a ReLU can only add nonnegative values to its input. The usual order is therefore normalization, then ReLU, then convolution, repeated twice, and the network starts with a plain convolution before the first block. With He initialization, each block keeps the variance of its input, so adding the input back doubles the variance at every block. After blocks the variance has grown by . Two fixes exist:

  • Rescaling: multiplying the sum by after each block keeps the variance constant.
  • Normalization: batch normalization at the start of each block makes the variance grow only linearly, from to . This is the usual fix.

Residual connections alone roughly double the depth that can be trained. With batch normalization, ResNets of about layers still train effectively.

A ResNet is a CNN made of residual blocks. A bottleneck block uses three convolutions, each preceded by batch normalization and a ReLU:

  • A $1 \times 1$ convolution reduces the channels.
  • A convolution works on the reduced channels.
  • A convolution restores the original channel count.

ResNet-200 starts with a convolution of stride and a max pool. Bottleneck blocks follow, with periodic stride-2 downsampling and doubling of the channels. Global average pooling and a -way dense layer end the network. It reached top-5 and top-1 error on ImageNet, against a human top-5 error of .

Two Conventions for the Block

The original ResNet applied convolution, batch normalization and ReLU, then a second convolution and batch normalization, and applied the last ReLU after the addition. The order above, with normalization and ReLU first, is the later “pre-activation” block. Pre-norm, which places the normalization at the start of each block, is the convention in modern networks, including transformers.

Intuition

Deep plain networks are hard to train even when their gradients have a sensible size. A 56-layer CNN trains to a worse error than a 20-layer one, on the training set as well as the test set. The extra layers could in principle copy their input and match the shallow network, but optimization does not find that solution. Each layer of a plain network must pass everything forward and must pass every gradient backward through all the layers after it. A small change to an early layer changes the input to every later layer. The gradient then changes unpredictably, a problem called shattered gradients.

A residual block changes the default. Doing nothing is easy, because the block only has to output zero. Each block learns a correction to the running representation, a “change in the function” rather than a replacement. The skip also gives each layer a short route to the output, so its gradient no longer depends only on a long product of Jacobians.

\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex]
\node (x) at (0,0) {$\mathbf{h}_{k-1}$};
\node[draw,thick,minimum height=0.7cm,align=center] (bn1) at (2.0,0) {\small norm};
\node[draw,thick,minimum height=0.7cm] (r1) at (3.3,0) {\small ReLU};
\node[draw,cold,very thick,minimum height=0.7cm] (c1) at (4.7,0) {\small conv};
\node[draw,thick,minimum height=0.7cm] (bn2) at (6.1,0) {\small norm};
\node[draw,thick,minimum height=0.7cm] (r2) at (7.4,0) {\small ReLU};
\node[draw,cold,very thick,minimum height=0.7cm] (c2) at (8.8,0) {\small conv};
\node[circle,draw,thick,inner sep=1pt] (plus) at (10.2,0) {$+$};
\node (y) at (11.6,0) {$\mathbf{h}_{k}$};
\draw[->,thick] (x) -- (bn1);
\draw[->,thick] (bn1) -- (r1);
\draw[->,thick] (r1) -- (c1);
\draw[->,thick] (c1) -- (bn2);
\draw[->,thick] (bn2) -- (r2);
\draw[->,thick] (r2) -- (c2);
\draw[->,thick] (c2) -- (plus);
\draw[->,thick] (plus) -- (y);
\draw[->,hot,very thick] (0.75,0) -- (0.75,1.1) -- (10.2,1.1) -- (10.2,0.27);
\node[hot] at (5.5,1.45) {skip connection: identity, or a $1 \times 1$ convolution if the shape changes};
\node[cold] at (5.5,-0.9) {residual branch $\mathbf{f}_k$ computes the change};
\end{tikzpicture}
\end{document}

Properties

(Shattered Gradients) In a deep plain network the gradient for an early layer changes almost randomly from one input to the next, and residual connections reduce this.

For a plain network , the derivative with respect to the first layer’s output is the product

Changing moves the point at which every later factor is evaluated, so after a finite update step the product can be completely different. Measured on a network with one input and one output, the gradient of a shallow network changes slowly with . For layers it looks like noise, and its autocorrelation drops to zero almost immediately. Gradient descent assumes that the gradient after a step resembles the gradient before it, so this breaks training. The residual version of the same derivative is

with one term per path through the four blocks that contains . The identity and the short chains are well behaved and dominate the sum.

(An Ensemble of Paths) A residual network behaves like a sum of many shallower networks, and most of its gradient flows through the short ones.

Unraveling blocks gives paths, and block lies on half of them. Path lengths follow a binomial distribution, so most paths are of medium length. Experiments on trained ResNets show that almost all of the gradient travels along short and medium paths. In a network of blocks, paths of to blocks carried nearly all of it, although they are only of all paths. Deleting one block from a trained ResNet barely changes its accuracy, while deleting one layer from a plain network such as VGG ruins it. Adding blocks therefore adds more parallel shallow paths. The network does not become much deeper in effect.

(The Identity Is Not a Guarantee) The skip keeps the block’s Jacobian near the identity only when the residual branch is small.

The Jacobian has singular values near when is small in operator norm. Then the block passes gradients back with their size roughly unchanged. Nothing forces this. If , the Jacobian is zero and no gradient passes at all. Small initial weights in the residual branch, or a learned scale on the branch initialized near zero, keep each block close to the identity at the start of training. Gradient clipping limits exploding gradients, but it does nothing for vanishing ones. The skip connection addresses the vanishing case.

(Variance Doubles per Block) Adding a block’s output to its input adds their variances, so without normalization the activations grow exponentially with depth.

He initialization keeps the variance of a linear-plus-ReLU layer equal to the variance of its input. The residual branch and the skip are roughly uncorrelated at initialization, so their sum has twice the variance of the input. The figure shows the growth: over three blocks, and after . The values eventually exceed floating-point range in the forward pass, and the gradients explode in the backward pass. Batch normalization at the start of each block, with scale and offset , gives every branch input unit variance. Each block then adds exactly one unit of variance, so block outputs variance . A side effect is that late blocks change the representation little at initialization, so the network starts effectively shallow. Training can increase the scales and so control its own effective depth.

\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex]
\foreach \row/\y/\c/\vals/\lab in {0/1.6/hot/{1,2,4,8}/{no normalization},1/0/cold/{1,2,3,4}/{normalization first in each block}} {
\foreach \v [count=\k from 0] in \vals {
\node[\c] at (\k*2.6,\y) {\large $\v$};
}
\foreach \k in {0,1,2} {
\draw[->,thick] (\k*2.6+0.35,\y) -- (\k*2.6+0.8,\y);
\node[draw,thick,minimum width=0.6cm,minimum height=0.5cm] at (\k*2.6+1.3,\y) {\small $\mathbf{f}$};
\draw[->,thick] (\k*2.6+1.8,\y) -- (\k*2.6+2.25,\y);
}
\node[right] at (8.3,\y) {\lab};
}
\node[mid] at (3.9,-0.9) {variance after each block, starting from unit variance};
\end{tikzpicture}
\end{document}

(Order of Operations) Putting the ReLU at the end of a residual branch makes every block push its input upward.

A standard layer applies a linear map and then a ReLU, so its output is nonnegative. In a residual block that order means , and the representation can only grow. Reversing the order, ReLU first and linear map last, lets each block add positive and negative values. A network built this way must start with a plain linear layer or convolution. Otherwise a negative input would be clipped to zero by the first block’s ReLU, and that block would contribute nothing. Most blocks contain two or three such layers, and all of them end with the linear step.

(Changing Shape Across a Skip) When a block halves the resolution or changes the channels, the skip must transform its input to the new shape.

A block that maps a input to a output cannot add the two directly. The common fix is a convolution with weights and stride on the skip path, the matrix in the callout. An alternative with no parameters subsamples the input and appends zero channels. Either way the skip is no longer an exact identity at these few points, which is why ResNets change shape only a handful of times. A U-Net uses a different kind of skip. It concatenates encoder maps with decoder maps across the whole network, and it does not replace the block-by-block residual connections inside each stage.

(More Than Depth) Residual connections smooth the loss surface, which helps even when the network is not very deep.

The early explanation for ResNets’ success was depth. Two observations weaken it. Shallow and wide residual networks, for instance with layers and many channels, can beat much deeper and narrower ones with the same number of parameters. And the gradient flows mostly along short paths, so a very deep ResNet acts like a combination of shallower networks. The current view is that the skips change the loss surface itself. Consider the loss plotted around the minimum that SGD finds. For a 56-layer network with skips it is a smooth bowl. Without the skips, the same network has a chaotic surface with many sharp peaks. A smoother surface is easier to descend and makes the final weights more robust to small errors.

Examples

(Easy) Paths and Variance in Three Blocks

A residual network has three blocks with He-initialized branches and unit-variance input.

(Medium) A Scalar Residual Block

A block with scalar weights computes , with and . A plain block computes with the same weights.

(Medium) A Projection Shortcut

A block takes a map (channels, height, width) and outputs .

(Hard) Why Bottlenecks

A deep ResNet stage has channels. One design uses two convolutions with channels in each block. The bottleneck design uses a convolution from to channels, a convolution with channels, and a convolution back to .

See Also