Foundations to Frontier
Tests
E. Deep Learning › E0 Core Concepts (concept pages)

Residual Connections

A residual connection adds a block's input to its output, h_out = h + F(h), so the block only has to learn a correction to the identity. The block's Jacobian becomes the identity plus the branch's Jacobian, giving gradients a direct path back through any number of blocks; with small initial branches the whole network starts close to the identity and is easy to optimise. Residual connections made networks with hundreds of layers trainable (ResNet, 2015) and are part of every transformer. This page covers the idea, the Jacobian argument, projection shortcuts, pre- and post-activation placement, branch scaling and zero initialisation, and the view of a residual network as an ensemble of many paths. In the exercises you measure gradient flow through 50 plain and residual blocks, then replicate the lesion experiment of Veit et al.: deleting a single block wrecks a trained plain network but barely affects a residual one.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Edit, don’t rewrite

Asking each layer to produce a completely new representation is like asking every editor of a document to rewrite it from scratch. A residual connection lets each layer keep what it received and only add a correction: output = input + change. If a layer has nothing useful to add, it can simply pass its input through.

Residual connections: an identity path through every block
block 1 → output · log scale · blue plain, orange residualpaths by branches used (0…20), first 20 blocks
gradient at block 1 ÷ at the output: plain 2.4e-1 · residual 2.7e+0 · residual paths: 2^40 = 1.1e+12
Each residual block computes h + F(h), so its Jacobian is I + J_F: gradients always have an identity path back. With small branches (scale near 0, as in Fixup and zero-init) the gradient reaches block 1 almost unchanged, and the network starts close to the identity. Large branches compound (1 + something)^depth and the gradient grows instead, which is why deep residual nets scale or normalise their branches. Unrolled, n blocks contain 2^n paths of mostly middling length. Deleting one block removes only the paths through it, which is why trained residual networks survive such lesions (exercise 2).
Why engineers care

Residual connections are in nearly every modern architecture: ResNet backbones in detection and segmentation, U-Nets, and every transformer block. They make very deep networks trainable and robust. They also allow tricks such as stochastic depth and early exits, which save computation on constrained hardware.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A residual block computes…
  2. 2. If a residual branch outputs zero, the block…
  3. 3. Residual connections mainly make…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The block by hand

Basic block. h_out = h + W₂ ReLU(W₁h + b₁). For h = (1, −1), W₁ = I, b₁ = 0 and W₂ = 0.5 I: ReLU(h) = (1, 0), so the output is (1, −1) + (0.5, 0) = (1.5, −1). With W₂ = 0 it is exactly h.

Jacobian. ∂h_out/∂h = I + J_F. With J_F = diag(0.1, −0.2), the Jacobian is diag(1.1, 0.8), with singular values near 1. Through L blocks the gradient is multiplied by Π(I + J_F,l). If every J_F is small, this stays near I, so neither vanishing nor explosion.

Shapes. The branch must output the same shape as h. When a stage changes the width (or, in CNNs, the spatial size), the shortcut becomes a projection, a linear map or 1×1 convolution. A 64 → 128 projection has 64 × 128 = 8 192 weights.

Paths. Expanding the product shows that n blocks contain 2ⁿ input-to-output paths (each block taken or skipped). For n = 16 that is 65 536 paths. C(16, 8) = 12 870 of them use exactly 8 branches.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Jacobian of a residual block?
  2. 2. Paths in a 10-block residual network?
  3. 3. When width changes, the shortcut becomes…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Gradient flow through 50 blocks

Exercise 1. Fifty blocks of width 64, with the gradient of the sum of the outputs backpropagated:

network gradient at block 1 ÷ at block 50 RMS of final activations
plain, He initialisation 2.94 0.725
residual, branch scale 1 1.59 × 10⁷ 1.7 × 10⁷
residual, branch scale 0.1 1.19 1.36

The Jacobian of one block with branch scale 0.1 had singular values between 0.846 and 1.172.

  • With small branches, the gradient passed through 50 blocks almost unchanged.
  • Full-size branches compound like (1 + ε)⁵⁰, so activations and gradients grew by 10⁷. Hence the practice of scaling branches down (by about 1/√L), zero-initialising the last layer of each branch, or normalising inside the branch.
  • A He-initialised plain network was also fine at initialisation. Residual connections earn their keep during training and in robustness (exercise 2), not only at the start.
class Block(nn.Module):
    def __init__(self, d):
        super().__init__(); self.f = nn.Sequential(nn.Linear(d, d), nn.ReLU(), nn.Linear(d, d))
        nn.init.zeros_(self.f[2].weight)          # start as the identity
    def forward(self, h): return h + self.f(h)
🐍 Exercise 1: gradient flow through residual blocks
Build 50-block plain and residual ReLU networks, backpropagate a gradient through them, measure its size at every block and inspect one residual block's Jacobian.
📝 Quick check (Level 2)
0/3 answered
  1. 1. With branch scale 1, the 50-block residual network's gradient…
  2. 2. Zero-initialising each branch's last layer makes the network start as…
  3. 3. A plain He network at initialisation in exercise 1 was…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory and variants

Why depth stopped hurting. He et al. (2016a) observed that deeper plain networks had higher training error than shallower ones. This “degradation” is an optimisation problem, not overfitting. Since a deep residual network can represent a shallower one by setting branches to zero, it should never be worse, and in practice it was not.

Pre-activation. He et al. (2016b) placed normalisation and ReLU inside the branch, before the weights: h + W ReLU(BN(h)). This keeps a clean identity path from input to output, so signals and gradients can pass through hundreds of blocks (1001-layer ResNets).

Ensembles of paths. Veit, Wilber and Belongie (2016) showed that trained ResNets behave like ensembles of relatively shallow paths. Deleting individual blocks barely hurts, and most of the gradient flows through short paths.

Branch scaling.

  • Fixup (Zhang et al., 2019) and SkipInit zero-initialise or scale branches, so normalisation can be dropped.
  • Transformers use pre-LayerNorm residual streams. GPT-2 scales residual-branch weights by 1/√(number of layers).
  • Highway networks (Srivastava et al., 2015) gate the shortcut. DenseNets concatenate instead of adding.

Continuous view. h_(l+1) = h_l + F(h_l) is an Euler step of the ODE dh/dt = F(h), which motivated neural ODEs (Chen et al., 2018).

Interview questions.

  • Why does adding the input help optimisation? The identity path keeps gradients near unit size, and each block starts close to the identity, so learning small corrections is easy.
  • Pre-LN or post-LN transformer blocks? Pre-LN keeps an unnormalised residual stream and trains more stably without warmup. Post-LN can reach slightly better results when tuned carefully.
📝 Quick check (Level 3)
0/3 answered
  1. 1. The degradation problem is…
  2. 2. A residual block corresponds to…
  3. 3. Veit et al. found trained ResNets behave like…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Deleting blocks after training

History.

  • R. Srivastava, K. Greff and J. Schmidhuber (2015) introduced highway networks.
  • K. He, X. Zhang, S. Ren and J. Sun (2016) introduced residual networks (ImageNet 2015 winner) and identity mappings.
  • A. Veit, M. Wilber and S. Belongie (2016) showed residual networks behave like ensembles.
  • A. Vaswani et al. (2017) built residual streams into transformers.
  • H. Zhang, Y. Dauphin and T. Ma (2019) introduced Fixup.

Exercise 2. Plain and residual networks with 16 blocks (width 32), trained with Adam for 200 steps on two moons. Then each block was deleted in turn and test accuracy measured on 1000 new points (the plain chain or the residual stream simply bypasses the deleted block).

network seed test accuracy mean accuracy with one block deleted worst single deletion
plain 0 0.971 0.433 0.056
plain 1 0.984 0.521 0.326
plain 2 0.987 0.463 0.119
residual 0 0.981 0.967 0.858
residual 1 0.977 0.969 0.924
residual 2 0.981 0.955 0.790
  • Both architectures learned the task equally well, at about 98% test accuracy.
  • Deleting any single block destroyed the plain network. Its mean accuracy fell to about chance, and some deletions gave accuracy far below chance (0.056): the following layers received inputs in a completely different representation and inverted their decision.
  • The residual network lost only 1–3 points on average. A deletion removes only the paths through that block, half of all paths, and the remaining paths, built to add up, still carry most of the computation.
  • This robustness is what makes stochastic depth (E4-5), early exits and layer pruning possible in residual networks.
🐍 Exercise 2 (research): lesioning trained networks
Train 16-block plain and residual networks with Adam on two moons, then delete each block in turn and measure test accuracy (after Veit, Wilber and Belongie, 2016).

Residual connections in AI, robotics and aerospace

  • Perception backbones: ResNet-family encoders dominate detection and segmentation on aerial, orbital and robot camera imagery.
  • Efficient inference: because blocks can be skipped with little loss, residual networks support early-exit and layer-dropping schemes that adapt computation to an onboard power budget.
  • Learned dynamics: residual updates x_(t+1) = x_t + f(x_t) are the natural form for learned physical simulators and controllers.

References

  1. K. He, X. Zhang, S. Ren, J. Sun (2016). “Deep residual learning for image recognition.” Proc. CVPR.
  2. K. He, X. Zhang, S. Ren, J. Sun (2016). “Identity mappings in deep residual networks.” Proc. ECCV.
  3. A. Veit, M. Wilber, S. Belongie (2016). “Residual networks behave like ensembles of relatively shallow networks.” Advances in NeurIPS 29.
  4. R. K. Srivastava, K. Greff, J. Schmidhuber (2015). “Training very deep networks.” Advances in NeurIPS 28.
  5. H. Zhang, Y. N. Dauphin, T. Ma (2019). “Fixup initialization: residual learning without normalization.” Proc. ICLR.