Topic 4 of 5 · 4 min read
Why deeper networks learn richer features
Goal: Explain how residual connections and normalization let deep networks train and name the problem each one solves.
Depth lets a network build features in stages, simple ones first and complex ones on top; residual connections and normalization are the two fixes that made very deep networks trainable.
Check yourself · 1 of 3
What does a residual connection compute?
Why it matters
You will hear that a model is "a 50-layer network" or "a deep transformer". Knowing why depth helps, and what breaks when it grows, lets you read an architecture and understand why it has skip connections and normalization layers in every block.
What depth buys
In an image classifier, early layers tend to respond to edges and colors, middle layers to textures and parts, and later layers to whole objects. Each layer reuses the features below it, so a deep network can express a complex function with fewer units than a very wide shallow one would need. Language models show the same pattern of low-level to high-level features across their layers.
The problem: vanishing gradients
Backpropagation multiplies one derivative per layer. A sigmoid's slope is at most 0.25. Ten sigmoid layers can multiply to 0.25^10, about one in a million, so the first layers get almost no learning signal. Weights that barely move do not learn. Gradients can also grow instead (exploding), with the same multiplication but factors above 1.
Fix 1: residual connections
A residual block outputs y = x + F(x), where F is a few layers. The input is added back to the output. During backpropagation the gradient has a direct path through the addition, so it reaches early layers without passing through every F. The ResNet paper (2015) showed that very deep plain networks had higher training error than shallower ones, and that skip connections removed the problem. Transformers use the same idea around every attention and feed-forward block.
Fix 2: normalization
Batch normalization rescales a layer's inputs using the mini-batch mean and variance. Layer normalization does the same per example, which suits transformers and small batches. Both keep activations at a steady scale from layer to layer, so gradients stay in a workable range and higher learning rates become safe.
Before and after
Weak: "We stacked 40 layers and accuracy got worse, so deep networks do not work."
Better: "A 40-layer plain network trains worse than a 20-layer one because gradients vanish. Adding skip connections and normalization lets the extra layers learn."
What goes wrong
- Treating depth as free. More layers cost compute and memory and can overfit.
- Removing the skip connection to "simplify" a block. Training of the deep stack often stalls.
- Batch norm with tiny batches. The batch statistics get noisy. Use layer norm or group norm.
- Different behavior in training and evaluation. Batch norm uses running statistics at test time, so call
model.eval().
Examples
A residual block in code
class Block(nn.Module):
def __init__(self, d):
super().__init__()
self.norm = nn.LayerNorm(d)
self.ff = nn.Sequential(nn.Linear(d, 4 * d), nn.GELU(), nn.Linear(4 * d, d))
def forward(self, x):
return x + self.ff(self.norm(x)) # skip connection
The x + is the residual path. Remove it and a stack of dozens of these blocks becomes much harder to train.
Reading a training report
A team reports that their 56-layer plain convolutional network reaches a higher training loss than a 20-layer one. This is not overfitting, since the training loss itself is worse. It points to an optimization problem, and the standard remedy is skip connections plus normalization.
Key points
- Depth builds features in stages, from edges to parts to objects in vision models.
- Backpropagation multiplies one derivative per layer, so small factors such as a sigmoid's slope of at most 0.25 shrink the gradient quickly.
- Residual connections compute x + F(x), which gives gradients a direct path to earlier layers.
- Normalization keeps activations at a steady scale, which stabilizes gradients and allows higher learning rates.
- Batch norm needs reasonably large batches and model.eval() at test time; layer norm does not depend on the batch.
Common mistakes
- Assuming a deeper network is always more accurate without skip connections or normalization.
- Calling a worse training loss in a deeper model overfitting; it points to a training problem.
- Using batch norm with batch size of one or two and wondering why results swing.
- Forgetting model.eval() so batch norm keeps using batch statistics at test time.
Check yourself · 2 of 3
Why can many stacked sigmoid layers make early layers learn very slowly?
Check yourself · 3 of 3
Layer normalization is often preferred to batch normalization in transformers. Why?
Practice items, not counted towards your mastery.
Start here
- Neural networks series (3Blue1Brown) (opens in a new tab)VideoFree
Animated walk through neurons, layers, gradient descent and backpropagation with the chain rule drawn out.
- Neural Networks and Deep Learning (Michael Nielsen) (opens in a new tab)BookFree
Free online book that derives backpropagation step by step and builds a digit classifier in Python.
- Deep Learning (Goodfellow, Bengio, Courville) (opens in a new tab)BookFree
The standard textbook; chapters on feedforward networks, optimization and regularization.
More for this topic
- Deep Residual Learning for Image Recognition (He et al.) (opens in a new tab)PaperFree
The paper that introduced skip connections and showed deeper plain networks training worse than shallow ones.
- Batch Normalization (Ioffe and Szegedy) (opens in a new tab)PaperFree
Introduces normalizing layer inputs per mini-batch to make deep networks train faster and more stably.