Skip to content

Convolutional Neural Networks

Slide a small pattern detector over an image and build a feature map with far fewer weights than a dense layer

Quick recap
  • An image is a grid of numbers, and a kernel is a small grid of learnable weights.
  • A convolution slides the kernel over the image and records one dot product per position.
  • Weight sharing and stacking turn edges into parts, and parts into objects.

This lesson follows Overfitting, Bias–Variance & Regularization and leads into Representation Learning & Embeddings. The Fourier view lives in Fourier transforms.

1. A picture is a grid

A grayscale image is a rectangular grid of numbers. A bright pixel is close to 1 and a dark pixel is close to 0. Color images add a channel dimension, but the spatial idea is the same: nearby pixels are nearby numbers.

Suppose we want to find a vertical bright line. A fully connected layer would give every hidden unit its own weight for every pixel. It could learn the detector, but it would have to learn the same detector again when the line moves one pixel to the right.

A 5×5 image with a vertical line

Before reading on, ask: what small pattern would distinguish the bright line from the dark background? The answer is a local comparison, not a summary of the whole image.

Try it

Why is a fully connected layer a wasteful choice for finding the same edge at many positions?

2. A kernel is a pattern detector

A kernel is a small grid of weights. The one below compares the right column with the left column. It gives a large positive response when the right side is brighter than the left side.

A vertical-edge kernel

Put the kernel on a 3×3 patch of the image, multiply matching cells, and add the nine products. Then move the window one pixel to the right and repeat. The result is the feature map.

For an output at row r and column c, the kernel covers the patch that starts at that position:

One output value

X is the image, K is the kernel, and y is one feature-map value. The double sum visits all nine kernel cells. A negative result is allowed; a ReLU is usually applied afterwards.

In this lesson the kernel is fixed so the numbers are readable. In a real CNN, the kernel values are learned by backpropagation, just like any other weight.

3. Slide and share

With an 8×8 image and a 3×3 kernel, there are 6×6 = 36 valid positions. The output size follows a simple counting formula:

Output size

n is the input size, k the kernel size, p the zero padding on each side, and s the stride. Padding adds a border of zeros; stride skips positions. With n = 8, k = 3, p = 0, and s = 1, the output is 6.

The parameter saving is large. The kernel has 9 weights plus 1 bias: 10 parameters. A dense hidden layer with 36 units reading all 64 pixels would have 64 × 36 + 36 = 2,340 parameters:

Kernel parameters
Dense-layer parameters

Weight sharing gives translation equivariance: if the input shifts, the feature map shifts by the same amount. It does not give full translation invariance. A pooled or global summary can make the final prediction less sensitive to position, but the internal feature map still moves.

Try it

Does weight sharing mean the network ignores where a feature is?

4. Worked example

Use the 5×5 image and the 3×3 kernel from the start of the lesson. The valid output is 3×3.

Image
Kernel

Worked example: three convolution positions

At the top-left position, the kernel sees a dark left column and a bright right column. Each row contributes −1·0 + 0·0 + 1·1 = 1, and three rows give 3:

Moving one pixel right puts the line under the middle kernel column, where the weight is 0. The response is 0. Moving right again puts the line under the left column, where the weight is −1. The response is −3.

Feature map
positionpattern under the kerneloutput
(0, 0)dark → bright3
(0, 1)bright line centred0
(0, 2)bright → dark−3

The positive column marks one edge and the negative column marks the other. A ReLU would keep the positive band and zero the negative one. The sign is information: it tells the network which side is brighter.

If you flip the kernel left-to-right, the signs reverse. Deep learning libraries usually compute cross-correlation, which does not flip the kernel, even though the operation is called convolution. The Fourier transform chapter explains the mathematical convolution that does flip it.

5. Pooling and stacking

A convolution produces one feature map per kernel. A real layer has many kernels, so its output is a stack of channels: height × width × channels. Each channel responds to a different local pattern.

Pooling summarizes a small neighbourhood. Max pooling keeps the largest value in each window, which reduces the spatial size and makes the next layer less sensitive to tiny shifts:

2×2 max pooling

Pooling has no learned weights. It is a fixed operation, unlike a convolution. Average pooling is the other common choice; both trade spatial detail for a smaller, more robust summary.

Stack the pieces and the receptive field grows. A neuron in the second layer sees a larger patch of the original image than a neuron in the first layer. Early channels can learn edges and color blobs; later channels can combine them into textures, parts, and objects. The network learns the hierarchy from data rather than being handed a list of edges.

6. Where it fails

Convolution is good at local patterns, not at arbitrary global relationships. A kernel does not know that the same object appears at two distant locations, and a small receptive field cannot relate the top of an image to the bottom without many layers or a global operation.

The usual CNN bias is toward translation, not rotation or scale. A rotated cat may look different to a stack of small kernels unless the training data or augmentation teaches the network to handle it. Padding can also leak border artifacts, and stride can miss a pattern that falls between sampled positions.

CNNs are also expensive in memory and compute, and they can be fooled by adversarial perturbations that are invisible to a person. A high test accuracy is evidence about the test set, not a guarantee about a new camera, a new hospital, or a new distribution.

The next question is how a model can represent discrete things — words, users, products — as vectors that carry similarity. That is the embeddings lesson.

Next on the recommended path: Representation Learning & Embeddings

7. Going deeper

The convolution theorem says that convolution in the spatial domain becomes multiplication in the frequency domain. That is the connection to the Fourier transform. It also explains why large-kernel convolution can be accelerated with FFTs, and why the distinction between convolution and cross-correlation matters when you compare formulas.

Receptive-field arithmetic is worth knowing. Two stacked 3×3 convolutions see a 5×5 patch, and three see 7×7, before pooling. Dilation inserts gaps in the kernel to grow the field without growing the parameter count. Depthwise separable convolutions split a spatial filter from a channel mix, which is common in mobile models.

Padding mode matters at the border. Zero padding invents darkness outside the image; reflection padding mirrors the edge. Stride greater than one reduces the output size but can alias small patterns. These are engineering choices, not details that disappear in a large model.

Modern vision systems often combine convolutions with attention or use vision transformers. The useful question is not “CNN or transformer?” but “which inductive bias matches the data and the compute budget?”

8. Practice

Try it

A 3×3 kernel has 9 weights and 1 bias. How many parameters does it add to a layer with 16 output channels?

Try it

What does a kernel with weights [[1,1,1],[0,0,0],[-1,-1,-1]] respond to?

Try it

An 8×8 image uses a 3×3 kernel with padding 1 and stride 2. What is the output size?

9. Resources

The MIT chapter covers convolution, pooling, and CNN architecture; the CS231n notes go deeper into layer sizing, parameter counts, and case studies. Both links were opened and checked for this lesson.

Continue the track

Open the playground for this topic

Convolution Playground →

Draw an 8×8 image, edit a 3×3 kernel, and sweep it across the image to build a feature map.

The mathematics behind this topic: Fourier transforms.

All Machine Learning lessons

Site soundtrack