Convolutional Neural Networks

Convolutional Neural Networks (CNNs) are a class of deep learning models designed to process grid-structured data — most famously images — by applying learned filters that detect local patterns such as edges, textures, and shapes. They matter enormously in modern AI because they power nearly every computer vision system in production today, from medical imaging to self-driving cars. The counterintuitive fact: a CNN trained on cats and dogs spontaneously learns to detect edges in its first layer and fur textures in its second — nobody told it to; it discovered these features entirely on its own.

The core insight

The key insight is that images have local structure — a pixel is most related to its neighbors, not to a pixel on the opposite side of the image. Instead of connecting every pixel to every neuron (which would be astronomically expensive), a CNN slides a small window called a filter across the image and applies the same learned weights everywhere. This means the network learns to recognize a feature — say, a vertical edge — in the top-left corner, and that same detector automatically works in the bottom-right corner too. This property is called translation equivariance, and it's why CNNs can generalize so efficiently from so few parameters.

How It Works

A CNN processes an image through a sequence of operations. The two most important are convolution and pooling.

The Convolution Operation

A filter (also called a kernel) is a small grid of learnable weights — typically 3×3 or 5×5. It slides across the input image and at each position computes a dot product between the filter weights and the patch of pixels underneath.

1
O(i,j)=∑m∑nI(i+m,  j+n)⋅K(m,n)\quad O(i,j) = \sum_{m}\sum_{n} I(i+m,\; j+n) \cdot K(m,n)

Explanation: At output position (i,j), multiply each weight in kernel K by the corresponding input pixel, then sum them all up to get a single number.

Symbols:

  • O(i,j)O(i,j) — the output value at row ii, column jj
  • I(i+m,j+n)I(i+m, j+n) — the input pixel at the corresponding shifted position
  • K(m,n)K(m,n) — the filter weight at position (m,n)(m,n)
  • m,nm, n — indices that iterate over the filter's height and width

Adding a Bias and Activation

In practice, a bias term is added and the result is passed through an activation function (almost always ReLU in modern CNNs):

2
A(i,j)=ReLU ⁣(O(i,j)+b)=max⁡ ⁣(0,  O(i,j)+b)\quad A(i,j) = \text{ReLU}\!\left(O(i,j) + b\right) = \max\!\left(0,\; O(i,j) + b\right)

Explanation: Shift the convolution output by a learned bias b, then clip any negative values to zero, introducing the non-linearity the network needs to learn complex patterns.

Symbols:

  • bb — a scalar bias term, one per filter
  • ReLU\text{ReLU} — Rectified Linear Unit; outputs 0 for negatives, unchanged for positives

Output Size After Convolution

3
Wout=Win−F+2PS+1\quad W_{out} = \frac{W_{in} - F + 2P}{S} + 1

Explanation: The output width depends on the input width, filter size, how much zero-padding you add around the border, and how many pixels you skip between each filter position.

Symbols:

  • WinW_{in} — input width in pixels
  • FF — filter size (e.g. 3 for a 3×3 filter)
  • PP — padding (zeros added around the border; often 1 to keep size the same)
  • SS — stride (step size between filter positions; usually 1 or 2)

Quick Python Example

```python import torch import torch.nn as nn

# A minimal CNN for 28x28 grayscale images (e.g., MNIST) class SimpleCNN(nn.Module): def __init__(self): super().__init__()

# 1 input channel (grayscale), 32 filters, each 3x3 self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1) self.relu = nn.ReLU()

# Halves spatial dimensions: 28x28 -> 14x14 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)

# Flatten then classify into 10 digit classes self.fc = nn.Linear(32 14 14, 10) def forward(self, x): # x shape: (batch, 1, 28, 28) x = self.pool(self.relu(self.conv1(x))) # -> (batch, 32, 14, 14) x = x.view(x.size(0), -1) # flatten -> (batch, 6272) return self.fc(x) # -> (batch, 10) model = SimpleCNN() print(model) ```

Max Pooling

After convolution, a pooling layer reduces the spatial size of feature maps. Max pooling takes the largest value in each small window: ```python

# Visualizing what max pooling does on a tiny example import numpy as np feature_map = np.array([[1, 3, 2, 4], [5, 6, 1, 2], [3, 1, 4, 7], [2, 8, 3, 5]])

# 2x2 max pool with stride 2

# Top-left 2x2 window -> max(1,3,5,6) = 6

# Top-right 2x2 window -> max(2,4,1,2) = 4

# Bottom-left 2x2 window -> max(3,1,2,8) = 8

# Bottom-right 2x2 window -> max(4,7,3,5) = 7 pooled = np.array([[6, 4], [8, 7]]) print("After 2x2 max pool:\n", pooled) ``` Pooling achieves two things: it shrinks the feature map (reducing computation) and makes detections slightly robust to small shifts in position.

researcher · Yann LeCun | b. 1960 | Inventor of practical Convolutional Neural Networks

Yann LeCun, a French-American computer scientist, developed the first practically successful CNN — LeNet-5 — while at AT&T Bell Labs in 1989, applying it to handwritten digit recognition for the US Postal Service and eventually for reading bank checks. His work demonstrated that gradient-based learning could train multi-layer convolutional architectures end-to-end, a radical idea at the time when most researchers believed neural networks could not scale. Remarkably, LeNet was processing over ten percent of all checks in the United States by the late 1990s, making CNNs one of the earliest deep learning technologies deployed at massive commercial scale.

Figure
CNN Architecture: Image to Prediction
Input Image 224×224×3 Conv+ReLU 64 filters 3×3 MaxPool 2×2 ↓ ↘ Conv+ReLU 128 filters Flatten FC Layer cat 0.82 dog 0.11 bird 0.04 car 0.02 ... 0.01 Softmax Output Convolutional Neural Network — Forward Pass

Intuition

The Mental Model

Imagine you are trying to identify a handwritten letter "A" in a photograph, and your only tool is a small magnifying glass that you slide across the image. At each position, you ask: "Does this patch look like a diagonal line going up-left? Does it look like a crossbar?" You don't need to look at the whole image at once — you collect local evidence patch by patch, then combine those clues. That's exactly what a CNN filter does. Now imagine you have 32 different magnifying glasses, each trained to notice a different micro-pattern — one for horizontal edges, one for color blobs, one for curves. Stack several rounds of this process (each layer looking at increasingly large-scale patterns), and by the final layer your network has assembled micro-clues into parts ("this looks like a pointy top and two legs") and then into a full object ("that's an 'A'"). The magic is that nobody labeled which magnifying glass should detect which feature — the training data and gradient descent sorted that out automatically.

Figure
What a Filter Learns: 3×3 Kernel Detecting a Vertical Edge
How a 3×3 Vertical Edge Filter Works Input Patch active window ↑ × 3×3 Filter (learned weights) -1 0 +1 -2 0 +2 -1 0 +1 = Output Value high edge detected! (large positive value) dark left / bright right = vertical edge present

Historical Development

Origins · 1 minhow this was discovered
History
1979
Kunihiko Fukushima introduces the Neocognitron, a hierarchical model inspired by the cat visual cortex with alternating convolution-like and pooling-like layers — the architectural ancestor of modern CNNs.
1989
Yann LeCun, Boser, Denker, Henderson, and others at Bell Labs train the first CNN using backpropagation, applied to handwritten digit recognition for ZIP