Dev.to AI 🤖 Ai 👁 0 📖 10 min read

Residual Connections: Why Adding the Input Back Makes Very Deep Networks Trainable

Before 2015, making a neural network deeper was a gamble. Up to a point, depth helped. Past that point, training accuracy got worse, not better, and the culprit was not overfitting. A 56-layer network performed worse on

Before 2015, making a neural network deeper was a gamble. Up to a point, depth helped. Past that point, training accuracy got worse, not better, and the culprit was not overfitting. A 56-layer network performed worse on the training set than a 20-layer network trained on the same data. The deeper model could in principle copy the shallower one exactly, by setting its extra layers to the identity function, yet gradient descent could not find that solution. Optimisation itself was failing.

The fix that broke this logjam was almost embarrassingly simple: add the input of a layer back to its output. That one addition, the residual connection, let networks reach hundreds and then thousands of layers, and it now sits inside almost every architecture you will meet, from ResNets to transformers. This article explains why such a small change has such a large effect, what the maths of gradient flow says about it, and where its limits lie.

The degradation problem

To see why depth was hard, consider a plain feedforward network, a stack of layers where each layer transforms the output of the one before it. Training error should fall, or at worst stay flat, as layers are added, because a deeper network contains every shallower network as a special case. In practice, researchers observed the opposite. He and co-authors reported that a 56-layer plain network had higher training error than a 20-layer plain network on ImageNet, even though the deeper network had strictly more capacity.

This is the degradation problem, and it is distinct from overfitting. Overfitting shows up as a gap between training and test error. Degradation shows up on the training set itself, which means the optimiser is failing to fit data it has already seen. Something about a long chain of nonlinear transformations makes the loss landscape hostile to gradient descent.

Two mechanisms were suspected. First, gradients shrink as they propagate backwards through many layers, the vanishing gradient problem. Second, each layer must learn a full transformation from scratch, including the parts of the transformation that should simply pass information through unchanged. If the ideal behaviour of a layer is close to the identity, a plain layer has to learn the identity mapping with a stack of nonlinearities, which is a surprisingly awkward thing to ask of it.

The residual reparameterisation

The residual connection changes what a layer is asked to learn. Instead of asking a stack of layers to learn a desired mapping H(x) directly, it asks the stack to learn the difference between that mapping and the identity:

H(x)=F(x)+x

Here F(x) is the residual function, implemented by the usual stack of convolutions or linear layers and nonlinearities, and x is added back through a shortcut connection that skips the stack entirely. If the ideal mapping is close to the identity, the residual function only has to learn something close to zero, and pushing the weights of a layer towards zero is far easier for an optimiser than shaping them into an identity matrix.

This is a reparameterisation, not extra capacity. A residual block has the same number of learnable parameters as the plain block it replaces. The shortcut adds no parameters at all. What changes is the geometry of the optimisation problem: the function class is the same, but gradient descent now navigates a much friendlier landscape.

The idea had a close ancestor in highway networks, which used learned gates to control how much of the input passed through unchanged. Residual connections can be seen as a highway network with the gate permanently open. Removing the gate removed the parameters and the training difficulty that came with it, and the simpler version worked better.

Why the gradient survives

The clearest argument for residual connections comes from following a gradient backwards through the network. Consider a chain of residual blocks, where block l computes:

xl+1​=xl​+F(xl​,Wl​)

Unrolling this recurrence from layer l to a deeper layer L gives:

xL​=xl​+i=l∑L−1​F(xi​,Wi​)

The feature at layer L is the feature at layer l plus the sum of every residual contribution in between. Now apply the chain rule to the loss L :

∂xl​∂L​=∂xL​∂L​(1+∂xl​∂​i=l∑L−1​F(xi​,Wi​))

The crucial term is the 1. The gradient reaching layer l splits into two parts: one that flows directly through the shortcut path, untouched by any weight matrix, and one that flows through the residual branches. Even if every residual branch has a tiny gradient, the direct term guarantees that the gradient never vanishes entirely. In a plain network, the corresponding expression is a product of layer Jacobians, and a product of many small numbers collapses towards zero. In a residual network, the gradient is a sum, and a sum always keeps its direct term.

This does not mean gradients cannot vanish at all. The gradient inside a residual branch can still shrink, and if the direct term dominates too strongly the network may barely use its deeper layers. But the catastrophic exponential decay that stopped plain networks from training is gone, and that is what matters.

There is a second, complementary way to see the same result. Expanding the unrolled sum shows that a residual network with n blocks implicitly contains 2n paths from input to output, one for each subset of blocks a signal can pass through. Most of those paths are short. Gradient flows mainly through the short paths, which is why very deep residual networks behave, in training terms, more like ensembles of shallow networks than like a single network of their full depth. The depth is available when the task needs it, but the optimiser is never forced to push gradients through the full chain.

A dynamical systems view

The unrolled recurrence also invites a different interpretation. Rewrite the block update with an explicit step size:

xl+1​=xl​+hF(xl​,Wl​)

This is exactly one step of the forward Euler method for the ordinary differential equation dx/dt=F(x,W) , with the layer index playing the role of time. A residual network is a discretised dynamical system, and depth is integration time.

This view explains several observations at once. It explains why residual networks tolerate large depths: integrating further is a smooth operation. It explains why the features evolve gradually from layer to layer rather than changing abruptly. And it is the starting point for neural ordinary differential equations, which replace the discrete stack of blocks with a continuous-time solver and learn the vector field directly.

The dynamical view also warns against a naive reading of depth. If the vector field changes slowly, adding more layers adds less and less new computation, and the network may be spending capacity on refining a trajectory rather than computing new features. Depth and expressivity are related, but they are not the same thing.

Pre-activation: keeping the identity path clean

The original ResNet placed the shortcut around two convolutions, with batch normalisation and ReLU activations inside the residual branch, and a ReLU after the addition. Later work showed that the placement of the nonlinearity matters. If a ReLU sits on the main path after the addition, the identity path is no longer clean: the signal passing through the shortcut is truncated at zero before reaching the next block.

The pre-activation variant moves batch normalisation and the activation to the start of the residual branch, before the weights, so that the addition feeds directly into the next block with nothing in between. The main path becomes a pure identity from input to output, and the gradient argument above holds exactly rather than approximately. In practice, pre-activation ResNets train more easily and reach slightly better accuracy at the same depth, which is a useful reminder that the details of where nonlinearities sit are not cosmetic.

The same principle guides the transformer, where every sublayer, attention and feedforward alike, is wrapped in a residual connection followed by layer normalisation. The transformer is, in this sense, a residual network whose residual branches happen to compute attention. Much of what makes transformers trainable at depth comes from the same identity path.

A residual block in code

The implementation is as short as the idea. Here is a basic pre-activation residual block in PyTorch:

import torch.nn as nn

class ResidualBlock(nn.Module):
    def __init__(self, channels):
        super().__init__()
        self.norm1 = nn.BatchNorm2d(channels)
        self.conv1 = nn.Conv2d(channels, channels, 3, padding=1)
        self.norm2 = nn.BatchNorm2d(channels)
        self.conv2 = nn.Conv2d(channels, channels, 3, padding=1)
        self.act = nn.ReLU(inplace=True)

    def forward(self, x):
        residual = x
        out = self.act(self.norm1(x))
        out = self.conv1(out)
        out = self.act(self.norm2(out))
        out = self.conv2(out)
        return out + residual

Two things are worth noting. First, the shortcut is a single line, out + residual, and it carries no parameters. Second, the block works with any residual branch: replace the convolutions with linear layers and the same structure serves a transformer or a multilayer perceptron. When the branch changes the number of channels or the spatial resolution, the shortcut needs a matching projection, usually a 1x1 convolution, so the shapes line up for the addition. That projection is the one place a shortcut gains parameters, and it is used only where dimensions change.

What residual connections do not fix

Residual connections solved the degradation problem, but they are not a universal cure, and treating them as one leads to predictable mistakes.

First, they do not remove the need for normalisation. The gradient argument guarantees a direct path, but the residual branches still need well-conditioned activations to learn anything useful. A residual network without batch normalisation or layer normalisation trains poorly, and the interaction between the shortcut and the normalisation layer is part of why pre-activation helps. The shortcut fixes the path; normalisation fixes the scale.

Second, the memory cost of training grows with depth regardless. Backpropagation through a residual network still needs the activations of every block, because each block's residual branch must be differentiated. Techniques such as gradient checkpointing trade compute for memory by recomputing activations, but the shortcut itself saves no memory. A thousand-layer network is trainable, in the optimisation sense, long before it is affordable.

Third, residual connections can encourage feature reuse at the expense of feature learning. If the identity path carries most of the signal, a block can contribute almost nothing and the network still performs well, which means some layers may be nearly redundant. Studies that delete individual residual blocks from a trained network often find accuracy barely changes, which confirms the ensemble view but also suggests that depth is sometimes wasted. The network uses the depth it needs and ignores the rest, which is efficient for training but not for inference.

Finally, the identity shortcut assumes the input is worth preserving. In architectures where the representation should change drastically between stages, for example at a downsampling boundary, the shortcut is less helpful, which is why those boundaries use projection shortcuts or strided convolutions. The residual connection is a prior that the transformation should be a small modification of its input, and like every prior it helps when it matches reality and hurts when it does not.

Where the idea went next

The residual connection has escaped its original setting. DenseNets took the idea further by concatenating the outputs of all previous layers instead of adding them, trading the clean identity path for explicit feature reuse. U-Nets use long skip connections between encoder and decoder to carry fine-grained spatial detail past the bottleneck. Highway networks, the gated predecessor, survive in specialised forms where the network genuinely needs to choose how much to pass through.

In language models, the residual stream has become a central object of study in its own right. Each transformer layer reads from and writes to a shared residual stream, and interpretability work treats that stream as the model's working memory, with attention heads and feedforward layers acting as small updates to it. The same addition that made ResNets trainable now shapes how researchers think about what a transformer is computing.

Takeaway

The residual connection is a reminder that the hardest problems in deep learning are often optimisation problems in disguise. The degradation problem looked like a capacity problem, a modelling problem, and a data problem, but it was none of those. It was the geometry of gradient descent through a long chain of transformations, and a single addition changed that geometry enough to unlock a thousand-layer network. When a model will not train, the fix is sometimes not more data, more parameters, or a cleverer loss, but a shorter path for the gradient to travel.

Connect with me on LinkedIn: linkedin.com/in/umeshtharukaofficial

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.