D · Modern ideas
Convolution
Slide one small set of weights across an image: the same pattern detector everywhere, with far fewer weights than a dense layer.
Try it: D1 · Convolution
Intuition
A photo of 256 × 256 pixels has 65,536 inputs per colour. A dense layer connecting each to a neuron needs that many weights per neuron, and it ignores what we know about images: nearby pixels belong together, and a cat's ear is a cat's ear wherever it appears in the picture.
A convolutional layer builds both facts in. Each neuron looks only at a small patch, say 3 × 3 pixels. And all neurons share the same small set of weights, the kernel (or filter). So the layer is one tiny pattern detector slid across the whole image, answering at every position "how much does this patch look like my pattern?" The answers form a new image, the feature map. A layer has many kernels, each producing its own map: one finds vertical edges, another horizontal ones, another a colour.
The math
For an input image \(X\) and a \(k \times k\) kernel \(W\) with bias \(b\), the feature map is
\[ Y_{i,j} = b + \sum_{u=0}^{k-1}\sum_{v=0}^{k-1} W_{u,v}\, X_{i+u,\,j+v} \]followed by an activation, usually ReLU. (Strictly this is cross-correlation; a mathematical convolution flips the kernel first. Since the kernel is learned, the difference does not matter, and deep learning libraries call it convolution anyway.)
Channels
Colour images have 3 channels, and hidden layers have many. A kernel spans all input channels, so it has \(k \times k \times C_{\text{in}}\) weights, and its output sums over all of them. A layer with \(C_{\text{out}}\) kernels produces \(C_{\text{out}}\) feature maps, which are the channels of the next layer.
Output size
With input size \(n\), kernel size \(k\), padding \(p\) (zeros added around the border) and stride \(s\) (the step between positions):
\[ n_{\text{out}} = \left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1 \]"Same" padding, \(p = (k-1)/2\) with \(s = 1\), keeps the size; stride 2 halves it.
Pooling
Pooling layers shrink a feature map by summarizing each small window, typically taking its maximum (max pooling, 2 × 2 with stride 2). The network becomes less sensitive to exact positions, and deeper layers see a larger part of the image.
Worked example. The kernel in the animation has columns \((1, 1, 1)\), \((0, 0, 0)\), \((-1, -1, -1)\): it adds the left column of the window and subtracts the right one.
A window on a flat area (all 0 or all 1): the two columns cancel, output 0. A window whose right column is inside the square and the rest outside: \(0 - 3 = -3\). With its left column inside the square and its right column outside: \(3 - 0 = 3\).
Size: \(n = 8\), \(k = 3\), no padding, stride 1: \((8 - 3)/1 + 1 = 6\). Weights: 9, however large the image. A dense layer from an 8 × 8 image to a 6 × 6 map would need \(64 \times 36 = 2304\).
Why it works so well
- Few weights. Weight sharing makes the parameter count independent of image size, which reduces overfitting and memory.
- Translation equivariance. Shift the input and the feature map shifts the same way: a pattern learned in one place is recognized everywhere.
- A hierarchy. Stacked layers see ever larger regions (their receptive field grows): edges combine into textures, textures into parts, parts into objects. Visualizations of trained networks show exactly this progression.
Beyond images
Any data on a grid works: 1-D convolutions over audio and time series, 3-D over video and medical scans. Landmark architectures include LeNet (digits), AlexNet, VGG and ResNet, whose residual connections allowed networks hundreds of layers deep. Since 2020 vision transformers, built on attention, compete with convolutional networks, often combined with them.
Pitfalls
- Border effects. Without padding, each layer shrinks the map and the border pixels are seen by fewer windows.
- Not rotation-invariant. A kernel detects its pattern at one orientation; rotated or scaled objects need more kernels, data augmentation, or pooling.
- Shapes. Most bugs are shape mismatches: check channels-first versus channels-last, and compute sizes with the formula above.
- Initialization. The fan-in of a kernel is \(k \times k \times C_{\text{in}}\), not the number of channels (see Initialization).
History
David Hubel and Torsten Wiesel found in the 1960s that neurons in the cat's visual cortex respond to edges at particular positions and orientations. Kunihiko Fukushima's Neocognitron (1980) turned that into a layered network. Yann LeCun trained convolutional networks with backpropagation in 1989 and deployed LeNet to read handwritten cheques. In 2012 AlexNet (Krizhevsky, Sutskever and Hinton) won ImageNet by a wide margin on GPUs, starting the deep learning boom.
Connections
Builds on
- Deep NetworksA convolutional layer is a dense layer where each neuron sees only a small patch and all neurons share their weights.
Further reading
- Image Kernels explained visually (Setosa)Try blur, sharpen and edge kernels on a photo, with the window sliding just as it does here.
- CNN Explainer (Polo Club, Georgia Tech)A whole small image network in the browser: how stacked convolutions turn pixels into a class.
- But what is a convolution? (3Blue1Brown)Convolution from its mathematical roots, including the kernel flip that separates convolution from cross-correlation.