Lesson 5.1core3 h⚡ live demo

Convolution: the operation and its math

Local dot products, shared everywhere

A 224×224 color image is 150,528 numbers; a dense layer mapping it to just 1,000 units would need 150 million weights. Convolution exploits two facts about images — nearby pixels are related, and a feature is a feature wherever it appears — to extract rich features with a few thousand parameters. It is the operation inside every CNN (5.3 Architectures: LeNet → AlexNet → VGG → Inception → ResNet → EfficientNet → ConvNeXt), the patch embedding of Vision Transformers (5.7 Vision Transformers), the 1-D WaveNet model you build in 6.8 Project: character-level language model, and the U-Net denoiser inside diffusion models (8.9 Generative models II: Diffusion and flow matching). Mastering its arithmetic (output sizes, receptive fields, parameter and FLOP counts) is what lets you read any architecture table.

You'll be able to

  • Compute a 2-D cross-correlation by hand and in NumPy, including multi-channel inputs and multiple filters
  • Apply the output-size formula and derive receptive-field growth for stacked layers
  • Explain weight sharing, locality and translation equivariance, and quantify the parameter savings
  • Derive the gradients of a convolution layer with respect to its kernel and its input
  • Count parameters and FLOPs for a convolution layer

Key ideas

  1. Deep-learning 'convolution' is cross-correlation: a dot product between a k×k×C_in patch and a learned kernel at every position; flipping is irrelevant because kernels are learned.
  2. Output size = ⌊(n + 2p − k)/s⌋ + 1; 'same' padding for stride 1 is p = (k−1)/2.
  3. Parameters per layer = C_out(C_in·k² + 1), independent of image size; FLOPs ≈ 2·H_out·W_out·C_out·C_in·k².
Show 4 more key ideas
  1. Locality + weight sharing give translation equivariance and a ~1000× parameter saving over dense layers — an inductive bias ViTs must learn from data.
  2. Receptive field grows as r_l = r_{l−1} + (k_l − 1)·∏ strides; two 3×3 layers see 5×5 with fewer weights than one 5×5.
  3. Convolution is a sparse, weight-tied linear map (im2col makes it a matmul); its kernel gradient is a cross-correlation and its input gradient is a transposed convolution.
  4. Stride-2 convolutions and pooling trade resolution for receptive field and compute.

Suggested path through this lesson