Module 6.1 built the training stack: forward pass, backprop, optimizers, batch norm, and dropout. Stacking plain layers still made very deep networks hard to train—gradients vanished and accuracy saturated.
Residual Networks (ResNets) introduced skip connections: learn residual mappings F(x) so the block outputs x + F(x). This capstone shows how architecture and training mechanics combine to enable 50+ layer models that actually work.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain skip connections and the residual formulation y = x + F(x).
- Implement a
ResidualBlockin PyTorch with dimension matching. - Relate ResNets to gradient highways through backpropagation.
- Place BN and ReLU in a pre-activation residual block.
- Recognize ResNet as the backbone pattern for vision before transformers dominated some tasks.
The Vanishing Depth Problem
Plain deep CNNs: add layers, validation error gets worse. Gradients shrink through long chains of nonlinearities. ReLU and BatchNorm help but do not fully solve identity learning—a deep layer should be able to pass its input forward unchanged if that is optimal.
A residual (skip) connection adds the block input directly to the block output: y = x + F(x). The subnetwork F learns the residual correction rather than the full mapping H(x). If the optimal H is identity, F ≈ 0 is easy to learn.
| Design | Plain Deep Net | ResNet Block |
|---|---|---|
| Output | H(x) | x + F(x) |
| Gradient path | Through all layers | Direct + through F |
| Identity learning | Hard (weights → identity) | Easy (F → 0) |
| Typical depth | < 20 layers (era-dependent) | 18–152+ layers common |
PyTorch Residual Block
Projection Shortcuts
When spatial size or channels change, use a 1×1 conv on the skip path so x and F(x) shapes match before addition.
Reality: Gradients flow through both the addition (identity path) and F(x). The identity path preserves signal magnitude—it does not bypass learning in F when F is needed.
Reality: Residual ideas appear in audio, video, and transformer blocks (residual around attention/FFN). The pattern is universal: stable depth.
out + identity requires identical shapes. Forgetting downsample on the skip when stride=2 causes runtime errors—or silent bugs if shapes accidentally broadcast wrong in sloppy code.
Knowledge Check
- Short Answer: ResNet block output formula? Answer: y = x + F(x).
- True/False: Residual connections help gradient flow. Answer: True.
- Multiple Choice: When channels change, skip path needs: (a) dropout, (b) projection conv, (c) softmax, (d) larger lr only. Answer: (b).
- Short Answer: What does F learn if optimal mapping is identity? Answer: Near-zero residual.
- True/False: ResNets were introduced primarily for NLP transformers. Answer: False—vision (2015).
- Multiple Choice: BN in example block sits: (a) after skip add only, (b) on conv outputs before add, (c) nowhere, (d) on loss. Answer: (b).
- Short Answer: Why 1×1 conv on downsample path? Answer: Match channels and spatial size for addition.
- True/False: Deeper ResNets always need more dropout. Answer: False—task and data dependent.
- Multiple Choice: Next module topic: (a) Training Loop, (b) Perceptron, (c) Dataset, (d) Sigmoid. Answer: (a).
- Short Answer: One benefit of skip connections for optimization? Answer: Easier identity mapping / improved gradient flow.
Key Takeaways
- ResNets learn residuals: y = x + F(x).
- Skip connections enable identity paths and healthier gradients.
- Use projection shortcuts when shapes change.
- Combine with BN, ReLU, and tuned optimizers from this module.
- Next module: 6.2 Training Loop — end-to-end training engineering.
Capstone project: Train plain CNN vs TinyResNet (same param budget) on CIFAR-10; compare depth achievable before val accuracy drops.
Whiteboard: Draw backward paths through x+F(x) with chain rule—highlight +1 term on identity branch.