Volume 06 built the feedforward stack: the perceptron, fully-connected ANNs, backpropagation, and deployment concerns like quantization. Those dense networks treat every input pixel as an independent feature—which explodes parameter counts and ignores the spatial structure of images.
Convolutional Neural Networks (CNNs) open Volume 07. They replace dense connectivity with small, shared kernels that slide across an image, exploiting locality and translation equivariance. This lecture is the map for Module 7.1: every term you will study next—convolution, filters, feature maps, pooling, padding, stride, flatten, and transfer learning—is a building block of the architecture introduced here.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain why fully-connected ANNs scale poorly on images and how CNNs fix it.
- Describe the three core CNN ideas: local receptive fields, weight sharing, and spatial hierarchy.
- Identify the standard CNN block: convolution → activation → pooling, ending in a classifier head.
- Build a small CNN in PyTorch with
nn.Conv2d,nn.MaxPool2d, andnn.Linear. - Trace how tensor shapes change through a CNN, from input image to class logits.
- Connect CNN training and deployment back to Volume 06 skills (backprop, GPUs, quantization).
A Convolutional Neural Network is a neural network that uses convolutional layers—learnable filters slid across the input—to extract spatially local features, typically stacked with pooling and nonlinear activations to build a hierarchy from edges to objects.
Why Not Just Use a Dense Network?
A single fully-connected layer on a modest 224×224 RGB image needs 224 × 224 × 3 = 150,528 inputs. Connecting that to just 1,000 hidden units costs over 150 million weights in one layer—before any depth. Worse, a dense layer has no notion that neighboring pixels are related, so it must relearn the same edge detector at every location.
CNNs solve both problems at once. A convolution reuses one small filter across the whole image (weight sharing), so a 3×3×3 filter has only 27 weights yet scans every position. This slashes parameters, encodes the assumption that useful patterns are local and position-independent, and lets the network generalize from far less data.
| Property | Dense ANN | CNN |
|---|---|---|
| Connectivity | Every input to every unit | Local receptive field |
| Parameters per layer | Grows with image size | Fixed by kernel size |
| Spatial structure | Discarded (input flattened first) | Preserved until the head |
| Translation handling | Must relearn per position | Equivariant by design |
| Data efficiency on images | Low | High |
The Three Core Ideas
Local Receptive Fields
- Each unit sees only a small patch.
- Matches how edges and textures are local.
- Deeper layers see larger regions.
Weight Sharing
- One filter scans all positions.
- Massive parameter reduction.
- Detects a pattern anywhere.
Spatial Hierarchy
- Early layers: edges, colors.
- Middle layers: textures, parts.
- Late layers: objects, scenes.
Anatomy of a CNN
A classic CNN alternates feature extraction and downsampling, then hands a compact representation to a dense classifier:
Filters produce feature maps.
ReLU adds nonlinearity.
Pooling shrinks spatial size.
Flatten then dense layers output logits.
A Minimal CNN in PyTorch
This network classifies small RGB images. Notice how Conv2d keeps the 2D structure while Linear layers only appear after flattening.
Following the Shapes
Shape tracking is the single most useful CNN debugging skill. For the network above with a (4, 3, 32, 32) input:
| Stage | Output shape (N, C, H, W) |
|---|---|
| Input | 4, 3, 32, 32 |
| Conv2d(3→16, pad 1) | 4, 16, 32, 32 |
| MaxPool2d(2) | 4, 16, 16, 16 |
| Conv2d(16→32, pad 1) | 4, 32, 16, 16 |
| MaxPool2d(2) | 4, 32, 8, 8 |
| Flatten | 4, 2048 |
| Linear → logits | 4, 10 |
Strengths and Tradeoffs
Strengths
- Parameter-efficient via weight sharing.
- Exploits spatial locality and translation equivariance.
- Transferable features (see transfer learning).
Tradeoffs
- Assumes grid-structured data (images, spectrograms).
- Limited receptive field per layer; needs depth or dilation.
- Not naturally rotation- or scale-invariant.
“A CNN is fully translation invariant.” Convolution is translation equivariant—shift the input and the feature map shifts too. Approximate invariance comes later, from pooling and the final classifier, not from convolution alone.
How Volume 06 Skills Carry Over
CNNs are still trained by backpropagation and optimizers like Adam. They benefit heavily from GPUs, and their large kernel stacks are prime candidates for INT8 quantization when deploying to phones or browsers. Nothing you learned is discarded—CNNs simply change the layer, not the training loop.
Knowledge Check
- Short Answer: Name the three core ideas behind CNNs. Answer: Local receptive fields, weight sharing, and spatial hierarchy.
- True/False: A dense layer's parameter count grows with image size, while a conv layer's does not. Answer: True.
- Multiple Choice: Weight sharing means: (a) all layers share one optimizer, (b) one filter is reused across all spatial positions, (c) weights are frozen. Answer: (b).
- Short Answer: What is the typical order inside a CNN block? Answer: Convolution → activation (ReLU) → pooling.
- True/False: Convolution is translation invariant on its own. Answer: False—it is translation equivariant.
- Multiple Choice: In PyTorch, image tensors use the layout: (a) (N, H, W, C), (b) (N, C, H, W), (c) (C, N, H, W). Answer: (b).
- Short Answer: Why must we flatten before the dense classifier? Answer: Linear layers expect a 1D feature vector per sample, not a spatial grid.
- Short Answer: Give one reason CNNs are more data-efficient than dense nets on images. Answer: Shared filters generalize a pattern to every position, reducing what must be learned.
- Multiple Choice: Deeper CNN layers tend to represent: (a) raw pixels, (b) edges only, (c) higher-level parts and objects. Answer: (c).
- True/False: CNNs still train with backpropagation. Answer: True.
Key Takeaways
- CNNs swap dense connectivity for small, shared filters that exploit spatial locality.
- The core block is convolution → activation → pooling, ending in a dense classifier head.
- Tracking tensor shapes (N, C, H, W) is the key to reading and debugging CNNs.
- Training and deployment reuse Volume 06 tools—backprop, GPUs, and quantization.
- Next, Convolution details the operation at the heart of every conv layer.
Hands-on idea: Have students print .shape after each layer of SmallCNN and predict the next shape before running.
Discussion prompt: Ask why a dense network on 224×224 images is impractical, then estimate the parameter savings from a single 3×3 filter.
Recap: CNNs bring spatial awareness to neural networks by sliding shared filters across the input. Continue with Convolution.