Search

Search papers and lessons

Residual Connection

Level 3 · 10 min read

A residual connection adds a block's input back to its output, so each block only learns a correction and gradients get a direct route through deep networks.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Neural network basics
  • Probability
  • Architectures
  • Losses & regularization
  • Sign in to see your progress

TL;DR

A residual connection takes the input of a block and adds it to the block's output, instead of letting the block replace the input outright. The block then only has to learn a correction to its input, and the identity path carries the signal and the gradient straight through. This is what makes networks with hundreds of layers trainable.

Where you'll see it: in every block of a Convolutional Neural Network image classifier such as ResNet, and around each sub-layer of a Transformer, which is why almost every large language model has them.

Intuition

A draft passed through editors

Imagine a document passed down a chain of editors. The naive design makes each editor rewrite the whole page from scratch, so whatever the first editor wrote has to survive every rewrite intact, and a single careless editor can wreck it. The residual design gives each editor the current draft and asks only for a marked-up change. If an editor has nothing to add, the draft passes through untouched.

A residual block does the same thing to a feature vector. Its output is the input plus a learned change. If the best thing for that block is to do nothing, it only has to make the change close to zero, which is easy to learn. Identity is the default instead of something that must be learned from scratch.

Why deep plain networks get worse

Before residual connections, people noticed a puzzle. Stacking more layers in a plain network, with each layer replacing its input, eventually made the training error go up, not just the test error. A 56-layer plain network did worse than a 20-layer one on the same training data. The deeper model could in principle copy the shallow one and add identity layers on top, but gradient descent did not find such solutions. The optimisation itself got harder.

Two things contribute. Gradients are multiplied by one layer's weights at each step backwards, so they can shrink toward zero or blow up (the Vanishing Gradient Problem). And every layer must learn to reproduce its input before it can add anything useful. A residual connection addresses both: the identity path gives gradients a route that is not multiplied by weights, and each block starts as a near-identity map.

flowchart TD
  X[Input x] --> W1[Weight layer]
  W1 --> R[ReLU]
  R --> W2[Weight layer]
  W2 --> ADD((plus))
  X --> ADD
  ADD --> Y[Output y equals x plus F of x]

The right-hand edge from the input straight to the addition is the shortcut. The block computes F(x)F(x) on the left and adds xx back.

The math

The block

Definition: Residual block

Given an input vector x\mathbf{x}, a residual block computes

y=x+F(x;W),\mathbf{y} = \mathbf{x} + F(\mathbf{x}; W),

where FF is a small stack of learned layers with weights WW, and F(x;W)F(\mathbf{x}; W) has the same shape as x\mathbf{x}. The function FF is called the residual branch, and the term x\mathbf{x} is the shortcut or identity path.

Here y\mathbf{y} is the block's output, x\mathbf{x} its input, and WW the trainable weights inside the branch. A typical branch for a Feedforward Neural Network is two linear layers with a Rectified Linear Unit (ReLU) in between, F(x)=W2 σ(W1x)F(\mathbf{x}) = W_2\,\sigma(W_1 \mathbf{x}), where σ\sigma is the activation. Convolutional blocks use convolutions in place of the linear maps.

The gradient through one block

Let LL be the loss, and write gy=∂L/∂y\mathbf{g}_y = \partial L / \partial \mathbf{y} for the gradient arriving from above. The chain rule from Backpropagation gives the gradient at the input:

∂L∂x=∂L∂y∂y∂x=gy(I+∂F∂x).\frac{\partial L}{\partial \mathbf{x}} = \frac{\partial L}{\partial \mathbf{y}} \frac{\partial \mathbf{y}}{\partial \mathbf{x}} = \mathbf{g}_y \left(I + \frac{\partial F}{\partial \mathbf{x}}\right).

The term II is the identity matrix. It comes from the shortcut and it passes gy\mathbf{g}_y through unchanged. The other term, ∂F/∂x\partial F/\partial\mathbf{x}, is the Jacobian of the branch and is the only part that involves the weights. Even if that Jacobian is tiny, the gradient is still gy\mathbf{g}_y.

Stacking many blocks

Write xℓ\mathbf{x}_\ell for the input of block ℓ\ell, so xℓ+1=xℓ+F(xℓ;Wℓ)\mathbf{x}_{\ell+1} = \mathbf{x}_\ell + F(\mathbf{x}_\ell; W_\ell). Unrolling from block ℓ\ell up to block LL gives

xL=xℓ+∑i=ℓL−1F(xi;Wi).\mathbf{x}_L = \mathbf{x}_\ell + \sum_{i=\ell}^{L-1} F(\mathbf{x}_i; W_i).

The proof is one line by induction: adding each block's branch to the running sum reproduces the recursion. This says the deep output is the early input plus a sum of branch outputs. Taking the gradient of the loss with respect to xℓ\mathbf{x}_\ell then gives

∂L∂xℓ=∂L∂xL(1+∑i=ℓL−1∂F(xi;Wi)∂xℓ).\frac{\partial L}{\partial \mathbf{x}_\ell} = \frac{\partial L}{\partial \mathbf{x}_L}\left(1 + \sum_{i=\ell}^{L-1} \frac{\partial F(\mathbf{x}_i; W_i)}{\partial \mathbf{x}_\ell}\right).

The leading 1 is a route that does not multiply by any weight, so the gradient at early layers always contains a copy of the gradient at the top. In a plain network the product of Jacobians has no such term.

Shapes that do not match

The addition needs F(x)F(\mathbf{x}) and x\mathbf{x} to have the same shape. When a block changes the number of channels or the spatial resolution, the shortcut must also change. The usual fix is a learned projection y=Wsx+F(x)\mathbf{y} = W_s \mathbf{x} + F(\mathbf{x}), where WsW_s is a linear map or a 1 by 1 convolution with the appropriate stride. ResNet uses this only at the few blocks where the shape changes; everywhere else the shortcut is a bare identity, which costs no parameters and no computation.

Worked examples

Example
Ten blocks of scalar gain

Each block has a branch F(x)=0.1 xF(x) = 0.1\,x, a single weight of 0.1. Start with input x=2x = 2.

  1. Plain block: y=F(x)=0.1xy = F(x) = 0.1x. Its derivative is dy/dx=0.1dy/dx = 0.1.
  2. Residual block: y=x+0.1x=1.1xy = x + 0.1x = 1.1x. Its derivative is dy/dx=1.1dy/dx = 1.1.
  3. Stack ten plain blocks: the overall derivative is 0.110=10−100.1^{10} = 10^{-10}. The gradient at the input is essentially zero, so the early layers learn nothing.
  4. Stack ten residual blocks: the overall derivative is 1.110≈2.5941.1^{10} \approx 2.594. The gradient at the input is about 2.6 times the gradient at the output, so the early layers receive a usable signal.
  5. Interpretation: the plain stack's gradient is a product of ten small numbers, and the residual stack's is a product of ten numbers just above 1. Depth is harmless in the second case and fatal in the first.
Example
A vector block with real numbers

Let x=(1.0, −2.0)\mathbf{x} = (1.0,\ -2.0) and suppose the branch gives F(x)=(0.3, 0.1)F(\mathbf{x}) = (0.3,\ 0.1) at this input, so y=(1.3, −1.9)\mathbf{y} = (1.3,\ -1.9).

  1. Forward pass: y=x+F(x)=(1.0+0.3, −2.0+0.1)=(1.3, −1.9)\mathbf{y} = \mathbf{x} + F(\mathbf{x}) = (1.0 + 0.3,\ -2.0 + 0.1) = (1.3,\ -1.9).
  2. Suppose the branch Jacobian at this input is ∂F/∂x=diag(0.2, −0.1)\partial F/\partial \mathbf{x} = \mathrm{diag}(0.2,\ -0.1).
  3. Arriving gradient: gy=(0.5, −1.0)\mathbf{g}_y = (0.5,\ -1.0).
  4. Gradient at the input: gy(I+∂F/∂x)=(0.5×1.2, −1.0×0.9)=(0.6, −0.9)\mathbf{g}_y (I + \partial F/\partial\mathbf{x}) = (0.5 \times 1.2,\ -1.0 \times 0.9) = (0.6,\ -0.9).
  5. Interpretation: even if the Jacobian were the zero matrix, the gradient at the input would still be (0.5,−1.0)(0.5, -1.0). The branch only adjusts that gradient, it never has to carry it alone.
Example
Projection when the shape changes

A block takes 64 channels at height 32 and must output 128 channels at height 16 (stride 2). The branch FF produces the new shape, so a plain identity shortcut cannot be added: the shapes are 64×32×3264 \times 32 \times 32 and 128×16×16128 \times 16 \times 16.

  1. Use a 1 by 1 convolution with stride 2 and 128 output channels on the shortcut. It maps the input to the new shape with 64×128=819264 \times 128 = 8192 weights.
  2. Compute y=Wsx+F(x)\mathbf{y} = W_s \mathbf{x} + F(\mathbf{x}), now with matching shapes.
  3. Interpretation: the shortcut is no longer free, but it is cheap compared with the 3×33 \times 3 convolutions in the branch, and it keeps the identity-like gradient path at the one place where the shape changes.

Tradeoffs and failure modes

Residual connections cost almost nothing and are close to a default in deep networks. The tradeoffs are about what they do to the signal's size and where the normalisation goes.

SituationProblemBetter choice
Shape change inside a residual stackIdentity shortcut cannot be addedProjection shortcut (1 by 1 convolution or linear map)
Very deep stack with no normalisationActivations grow as branches add upNormalise each branch, or scale the last weights of each branch down at initialisation
Transformer with normalisation after the addition (post-norm)Gradients can be unstable in very deep stacksPre-norm, which normalises inside the branch before FF
Shallow networkResiduals add parameters for little gainPlain stack is fine
Need a learned gate on the shortcutFixed identity may be too rigidGated shortcuts, as in highway networks

Some practical warnings:

  • The sum grows with depth. Every branch adds to the signal, so the variance of activations can grow with the number of blocks. Normalisation or small initial branch weights keep this under control.
  • Pre-norm and post-norm change behaviour. Where the layer norm sits relative to the addition changes both trainability and the scale of the final representation. Check which one a model uses before comparing results.
  • A residual block is not a guarantee of good training. It removes one obstacle to optimisation. Learning rate, initialisation, and data still matter.

History

Before 2015, deep networks such as the VGG family had reached about 19 layers and the next step was hard. Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun at Microsoft Research published Deep Residual Learning for Image Recognition in 2015. They reported that a plain network got worse as it went from 20 to 56 layers, even on training error, and proposed adding the input back to each block. Their network, called ResNet, had 152 layers and won the ImageNet classification challenge that year.

Shortly before, Rupesh Srivastava, Klaus Greff and Jürgen Schmidhuber had published highway networks, which used a learned gate to mix the shortcut and the branch. The gate adds parameters, and the ungated identity shortcut of ResNet caught on because it was simpler and, in the ResNet authors' experiments, worked better at great depth.

In 2016, the same authors published Identity Mappings in Deep Residual Networks, which studied the ordering of normalisation and activation inside the block and showed that keeping the shortcut as a clean identity matters most. In 2017, the transformer paper by Vaswani and colleagues wrapped every attention and feed-forward sub-layer in a residual connection, and that pattern has been kept by nearly every large language model since. It caught on because it is simple, free of extra parameters, and makes depth usable.

Check yourself

QuestionWrite the equation for a residual block and say what the branch F is asked to learn.Show answer

The block computes y=x+F(x)\mathbf{y} = \mathbf{x} + F(\mathbf{x}). The branch FF learns a correction to the input. If the best behaviour is to leave the input alone, FF only needs to output something close to zero, which is easy to learn.

QuestionA residual block has branch F(x) = 0.1x. Start with x = 3 and an incoming gradient dL/dy = 2. Find the output y and the gradient dL/dx.Show answer

The output is y=3+0.1×3=3.3y = 3 + 0.1 \times 3 = 3.3. The derivative of the block is 1+0.1=1.11 + 0.1 = 1.1, so ∂L/∂x=2×1.1=2.2\partial L/\partial x = 2 \times 1.1 = 2.2.

QuestionWhy can a 56-layer plain network do worse on training data than a 20-layer one, and what about a residual network removes that problem?Show answer

Each plain layer replaces its input, so the optimiser must learn every layer's transformation from scratch, and gradients pass through a product of weight-dependent Jacobians that can vanish or become unstable. A residual network starts each block near the identity and gives the gradient an identity path that is not multiplied by weights, so the optimiser can make progress at great depth.

QuestionA new block maps 64 channels to 128 channels and halves the spatial size. What breaks if you keep the identity shortcut, and what do you do?Show answer

The addition needs matching shapes, and the identity shortcut has 64 channels at the old resolution, so the sum is undefined. Replace the shortcut with a projection, such as a 1 by 1 convolution with stride 2 mapping to 128 channels, so that the shortcut and the branch output have the same shape before they are added.

Sources