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 on the left and adds back.
The math
The block
Given an input vector , a residual block computes
where is a small stack of learned layers with weights , and has the same shape as . The function is called the residual branch, and the term is the shortcut or identity path.
Here is the block's output, its input, and 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, , where is the activation. Convolutional blocks use convolutions in place of the linear maps.
The gradient through one block
Let be the loss, and write for the gradient arriving from above. The chain rule from Backpropagation gives the gradient at the input:
The term is the identity matrix. It comes from the shortcut and it passes through unchanged. The other term, , 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 .
Stacking many blocks
Write for the input of block , so . Unrolling from block up to block gives
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 then gives
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 and 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 , where 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
Each block has a branch , a single weight of 0.1. Start with input .
- Plain block: . Its derivative is .
- Residual block: . Its derivative is .
- Stack ten plain blocks: the overall derivative is . The gradient at the input is essentially zero, so the early layers learn nothing.
- Stack ten residual blocks: the overall derivative is . The gradient at the input is about 2.6 times the gradient at the output, so the early layers receive a usable signal.
- 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.
Let and suppose the branch gives at this input, so .
- Forward pass: .
- Suppose the branch Jacobian at this input is .
- Arriving gradient: .
- Gradient at the input: .
- Interpretation: even if the Jacobian were the zero matrix, the gradient at the input would still be . The branch only adjusts that gradient, it never has to carry it alone.
A block takes 64 channels at height 32 and must output 128 channels at height 16 (stride 2). The branch produces the new shape, so a plain identity shortcut cannot be added: the shapes are and .
- 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 weights.
- Compute , now with matching shapes.
- Interpretation: the shortcut is no longer free, but it is cheap compared with the 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.
| Situation | Problem | Better choice |
|---|---|---|
| Shape change inside a residual stack | Identity shortcut cannot be added | Projection shortcut (1 by 1 convolution or linear map) |
| Very deep stack with no normalisation | Activations grow as branches add up | Normalise 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 stacks | Pre-norm, which normalises inside the branch before |
| Shallow network | Residuals add parameters for little gain | Plain stack is fine |
| Need a learned gate on the shortcut | Fixed identity may be too rigid | Gated 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 . The branch learns a correction to the input. If the best behaviour is to leave the input alone, 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 . The derivative of the block is , so .
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
- He, Zhang, Ren and Sun, Deep Residual Learning for Image Recognition (2015): https://arxiv.org/abs/1512.03385
- He, Zhang, Ren and Sun, Identity Mappings in Deep Residual Networks (2016): https://arxiv.org/abs/1603.05027
- Vaswani et al., Attention Is All You Need (2017): https://arxiv.org/abs/1706.03762