← Master Index
Vol. 07 Module 7.1 Lecture

Convolution

CNN Core Concepts

How This Lesson Fits the Module

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.
Definition

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:

Input Kernel 1 2 3 1 0 4 5 6 0 1 7 8 9 Top-left window [1 2 / 4 5] . [1 0 / 0 1] = 1*1 + 2*0 + 4*0 + 5*1 = 6 Slide right [2 3 / 5 6] = 2*1 + 3*0 + 5*0 + 6*1 = 8 Slide down-left [4 5 / 7 8] = 4 + 8 = 12 Slide down-right[5 6 / 8 9] = 5 + 9 = 14 Output feature map: 6 8 12 14
Convolution vs. Cross-Correlation

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.

SymbolMeaningExample
C_inInput channels3 (RGB)
C_outOutput channels (# filters)16
kKernel spatial size3 (a 3×3 window)
WeightsC_out × C_in × k × k16 × 3 × 3 × 3 = 432

Output-Size Formula

Formula

For input size W, kernel k, padding p, and stride s:

W_out = floor((W - k + 2p) / s) + 1

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

import torch from torch import nn conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1) x = torch.randn(1, 3, 32, 32) # (N, C, H, W) y = conv(x) print(y.shape) # torch.Size([1, 16, 32, 32]) # Reproduce a single hand computation with functional API import torch.nn.functional as F inp = torch.tensor([[[[1.,2,3],[4,5,6],[7,8,9]]]]) # 1x1x3x3 ker = torch.tensor([[[[1.,0],[0,1]]]]) # 1x1x2x2 print(F.conv2d(inp, ker)) # tensor([[[[ 6., 8.], [12., 14.]]]])

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

  1. 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.
  2. True/False: PyTorch flips the kernel like textbook convolution. Answer: False—it performs cross-correlation (no flip).
  3. Multiple Choice: A filter on a 3-channel input spans: (a) one channel, (b) all input channels, (c) all output channels. Answer: (b).
  4. Short Answer: For W=28, k=5, p=0, s=1, what is the output size? Answer: (28-5)/1 + 1 = 24.
  5. Short Answer: How many weights in a Conv2d(3→8, k=3)? Answer: 8 × 3 × 3 × 3 = 216 (plus 8 biases).
  6. True/False: Padding 1 with a 3×3 kernel and stride 1 preserves the spatial size. Answer: True.
  7. Multiple Choice: The number of output channels equals: (a) input channels, (b) number of filters, (c) kernel size. Answer: (b).
  8. Short Answer: Why does the kernel flip not matter in a CNN? Answer: Kernel weights are learned, so any flip is absorbed during training.
  9. Short Answer: What tensor layout does PyTorch expect for conv inputs? Answer: (N, C, H, W).
  10. 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.
Trainer’s Guide

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.