The CNN overview promised that shared filters slide across an image. Convolution is that sliding operation—the mathematical core every other Module 7.1 term depends on. Before you can reason about kernels, feature maps, padding, or stride, you must understand exactly what convolution computes.
Learning Objectives
By the end of this lesson, students should be able to:
- Define the 2D convolution (cross-correlation) operation used in deep learning.
- Compute a convolution output by hand for a small input and kernel.
- Explain how input channels and output channels interact in
Conv2d. - Apply the output-size formula for a given kernel, padding, and stride.
- Implement and verify a convolution in PyTorch.
- Distinguish mathematical convolution from the cross-correlation frameworks actually use.
Convolution (in deep learning) slides a small weight grid over the input, and at each position computes the sum of element-wise products between the weights and the overlapping input patch. The result is a feature map of local responses.
The Operation, Step by Step
Place the kernel over the top-left of the input, multiply overlapping values, sum them into one output number, then shift by the stride and repeat. Consider a 3×3 input and a 2×2 kernel:
True mathematical convolution flips the kernel before sliding. Deep-learning frameworks (PyTorch, TensorFlow) implement cross-correlation—no flip—but call it “convolution.” Since kernels are learned, the flip is irrelevant to results; just don’t be surprised the textbook flip is missing.
Channels: The Hidden Third Dimension
Images are not flat grids—an RGB image has 3 channels. A convolution filter spans all input channels. So a filter for a 3-channel input with a 3×3 spatial size is actually 3×3×3 weights; it produces one output channel. To get many output channels, you stack many such filters.
| Symbol | Meaning | Example |
|---|---|---|
| C_in | Input channels | 3 (RGB) |
| C_out | Output channels (# filters) | 16 |
| k | Kernel spatial size | 3 (a 3×3 window) |
| Weights | C_out × C_in × k × k | 16 × 3 × 3 × 3 = 432 |
Output-Size Formula
Example: a 32×32 input, 3×3 kernel, padding 1, stride 1 → (32 - 3 + 2)/1 + 1 = 32. Padding of 1 with a 3×3 kernel preserves spatial size—the “same” convolution.
Convolution in PyTorch
Why Convolution Works for Vision
Advantages
- Detects a pattern regardless of position.
- Few parameters, shared everywhere.
- Preserves spatial relationships.
Limitations
- Fixed receptive field per layer.
- Not rotation/scale invariant.
- Cost grows with channels and resolution.
Knowledge Check
- Short Answer: In words, what does a 2D convolution compute at each position? Answer: The sum of element-wise products between the kernel and the overlapping input patch.
- True/False: PyTorch flips the kernel like textbook convolution. Answer: False—it performs cross-correlation (no flip).
- Multiple Choice: A filter on a 3-channel input spans: (a) one channel, (b) all input channels, (c) all output channels. Answer: (b).
- Short Answer: For W=28, k=5, p=0, s=1, what is the output size? Answer: (28-5)/1 + 1 = 24.
- Short Answer: How many weights in a Conv2d(3→8, k=3)? Answer: 8 × 3 × 3 × 3 = 216 (plus 8 biases).
- True/False: Padding 1 with a 3×3 kernel and stride 1 preserves the spatial size. Answer: True.
- Multiple Choice: The number of output channels equals: (a) input channels, (b) number of filters, (c) kernel size. Answer: (b).
- Short Answer: Why does the kernel flip not matter in a CNN? Answer: Kernel weights are learned, so any flip is absorbed during training.
- Short Answer: What tensor layout does PyTorch expect for conv inputs? Answer: (N, C, H, W).
- True/False: Larger stride generally produces a smaller output. Answer: True.
Key Takeaways
- Convolution slides a kernel over the input, summing element-wise products at each step.
- Frameworks implement cross-correlation; the kernel flip is irrelevant for learned filters.
- Each filter spans all input channels and yields one output channel.
- Output size follows
floor((W - k + 2p)/s) + 1. - Next, the Kernel lecture zooms into the weight grid itself.
Hands-on idea: Give students a 4×4 input and a 3×3 kernel; have them compute the 2×2 output by hand, then verify with F.conv2d.
Discussion prompt: Why is weight sharing across positions a strong, useful prior for natural images?
Recap: Convolution is the sliding sum-of-products that turns an image and a kernel into a feature map. Continue with Kernel.