Skip to content

3.7 Residual networks

ready≈ 11.5 hoursNeeds: 3.6, 3.4, 0.3Unlocks: 4.5
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Residual gradient flow)
  4. 4 Do
  5. 5 Check

Degradation. Stack more layers onto a working network and, past some depth, the training error gets worse. That is not overfitting, which would show up only on validation data. A deeper network could always copy the shallow one and set its extra layers to the identity, so the problem is that the optimizer cannot find such a solution (He et al. 2016). With BatchNorm the gradients do not vanish; in fact, at initialisation they grow towards the input of a deep plain network (Yang et al. 2019). Training is hard because the layers’ gradients differ by orders of magnitude and change chaotically from input to input (Balduzzi et al. 2017).

Residual connections make each block learn a correction, h←h+f(h)\mathbf{h} \leftarrow \mathbf{h} + f(\mathbf{h}), instead of a whole new representation. Setting ff to zero gives the identity, so extra depth can do no harm. Backwards, the Jacobian of each block is I+f′\mathbf{I} + f': the gradient has a direct path through every block, which keeps gradient sizes similar across depth, and the loss surface becomes far smoother (UDL §11.2–11.3).

Inside a block. The CIFAR ResNet uses two 3×3 convolutions, each followed by BatchNorm, with a ReLU between them and another after the addition. When a block changes the number of channels or halves the resolution, the shortcut must change shape too, and this course uses a 1×1 convolution with the same stride to project it. He et al. instead pad the extra channels with zeros (their option A, no parameters); the projection is simpler to write and adds about 2,700 parameters. Three stages of blocks with 16, 32 and 64 channels, global average pooling and one linear layer make up ResNet-20, with about 0.27 million parameters (UDL §11.5; He et al. 2016, §4.2). Since 2016 residual connections have been in nearly every deep architecture, transformers included.

Residual gradient flow

Gradient size per layer, why ResNets train

Static view on small screens. Open this page on a laptop to interact.

1e01e11e21e31e41e5layer 1 (input side) → 40: ‖∂L/∂W‖, log scale. Dashed: skip connections on activation scale per layer (log): first 1.0e+0, last 1.2e+0
largest ÷ smallest layer gradient: 3.6e+3 with skip connections on: 5.5e+0

Each block is h ← h + relu(norm(h) W) with skip connections on, or h ← relu(norm(h) W) without, for 32 units and a batch of 32; the gradients are computed exactly by backprop at initialisation. One learning rate has to suit every layer, so what matters is how far apart the layers' gradients are. With normalization but no skips, the spread grows by orders of magnitude with depth. The skip path carries the gradient straight through every block (the derivative of h + f(h) is I + f′), which keeps the layers within a small factor. Without normalization, residual activations grow with depth instead, which is why ResNets put BatchNorm in every block and transformers put LayerNorm.

Bars show the gradient norm of every layer’s weights in a deep network at initialisation, on a log scale, with a dashed line for the same network with skip connections toggled the other way. A second chart shows the activation scale after each layer. Controls switch skip connections and LayerNorm on or off and set the depth and the initialisation gain; the readout compares the ratio of the largest to the smallest layer gradient. Keyboard: Tab to the toggles and press Enter; arrow keys adjust the depth slider.

Two parts. First, build the blocks and the network in PyTorch and pass the tests locally on a CPU: block shapes, projection shortcuts, the 272,474-parameter ResNet-20, and the gradient comparison from the widget on a real 56-layer ResNet.

Residual networks: edit exercises/stubs/m3_7_resnet.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 3.7
Hints · ResidualBlock 0/3 revealed
Hints · resnet_small 0/3 revealed

Then train it on a GPU. Open notebooks/3.7-resnet-cifar10.ipynb on Kaggle as in module 0.3, paste in your finished file, turn the GPU on and run everything. It plots the plain-versus-residual gradients at 56 layers, then trains ResNet-20 for 30 epochs with data augmentation, a one-cycle schedule and mixed precision. The target is above 85% test accuracy in under 30 minutes. Without a GPU the notebook runs a one-minute smoke test on random images instead.

1. A 56-layer plain network has higher training error than a 20-layer one. This is…
2. Why does a residual block make it easy to represent the identity?
3. When does a block need a projection shortcut?
4. In the backward pass, what does the skip connection contribute to ∂h_out/∂h_in?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.