← Master Index
Vol. 07 Module 7.1 Lecture

Convolutional Neural Network (CNN)

CNN Core Concepts

How This Lesson Fits the Module & Volume

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, and nn.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).
Definition

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.

PropertyDense ANNCNN
ConnectivityEvery input to every unitLocal receptive field
Parameters per layerGrows with image sizeFixed by kernel size
Spatial structureDiscarded (input flattened first)Preserved until the head
Translation handlingMust relearn per positionEquivariant by design
Data efficiency on imagesLowHigh

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:

1. Convolution

Filters produce feature maps.

2. Activation

ReLU adds nonlinearity.

3. Pooling

Pooling shrinks spatial size.

4. Classifier

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.

import torch from torch import nn class SmallCNN(nn.Module): def __init__(self, num_classes=10): super().__init__() self.features = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, padding=1), # 3 -> 16 channels nn.ReLU(), nn.MaxPool2d(2), # 32x32 -> 16x16 nn.Conv2d(16, 32, kernel_size=3, padding=1), # 16 -> 32 channels nn.ReLU(), nn.MaxPool2d(2), # 16x16 -> 8x8 ) self.classifier = nn.Sequential( nn.Flatten(), nn.Linear(32 * 8 * 8, 128), nn.ReLU(), nn.Linear(128, num_classes), ) def forward(self, x): x = self.features(x) return self.classifier(x) model = SmallCNN() x = torch.randn(4, 3, 32, 32) # batch of 4 CIFAR-sized images print(model(x).shape) # torch.Size([4, 10])

Following the Shapes

Shape tracking is the single most useful CNN debugging skill. For the network above with a (4, 3, 32, 32) input:

StageOutput shape (N, C, H, W)
Input4, 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
Flatten4, 2048
Linear → logits4, 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.
Common Misconception

“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

  1. Short Answer: Name the three core ideas behind CNNs. Answer: Local receptive fields, weight sharing, and spatial hierarchy.
  2. True/False: A dense layer's parameter count grows with image size, while a conv layer's does not. Answer: True.
  3. 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).
  4. Short Answer: What is the typical order inside a CNN block? Answer: Convolution → activation (ReLU) → pooling.
  5. True/False: Convolution is translation invariant on its own. Answer: False—it is translation equivariant.
  6. 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).
  7. Short Answer: Why must we flatten before the dense classifier? Answer: Linear layers expect a 1D feature vector per sample, not a spatial grid.
  8. 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.
  9. Multiple Choice: Deeper CNN layers tend to represent: (a) raw pixels, (b) edges only, (c) higher-level parts and objects. Answer: (c).
  10. 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.
Trainer’s Guide

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.