Slide a kernel over a handwritten digit and see edges light up. Step through stride and padding, pool a feature map, then train a tiny CNN in your browser and watch its filters learn.
1Slide one small filter everywhere
A 3×3 grid of weights, the kernel, slides across the picture. At each spot it multiplies the 9 pixels under it by its 9 weights and adds them up: one number of the output, the feature map. The same 9 weights are used at every spot.
2Pool to keep the strongest
Max pooling splits the feature map into 2×2 blocks and keeps only the biggest number in each. The map gets 4 times smaller, and a feature that moves a pixel often stays in the same block.
3Stack layers: edges, then parts, then things
The first layer's kernels find simple edges. The next layer runs its kernels over those edge maps, so it finds combinations: corners and curves. Deeper still, whole parts and objects. Nobody designs these filters: training learns them.
Before you start
Is it right for your data?
Recognises patterns in grids: classifies images, finds objects, reads signals.
- TablesNot a fit
spreadsheets, databases, CSVs
Columns in a table have no neighbours, so sliding kernels make no sense.
- TextWith work
emails, reviews, documents
1-D CNNs work, but Transformers are now the standard for language.
- ImagesGreat fit
photos, scans, video frames
What they were built for: photos, scans, video frames.
- Audio & time seriesGreat fit
sound, sensors, prices over time
Audio spectrograms and sensor signals, with 1-D or 2-D kernels.
- How much data
- Thousands of labelled images if you start from a pretrained network; far more from scratch.
- Preparation
- Resize and normalise pixels; augment with shifts, flips and crops. This page's tiny CNN overfits 500 digits.
- What it can learn
- Local patterns anywhere in the grid, built up into parts and objects. Handles shifts, not rotations or scaling.
- Explainable?
- No, though saliency maps show which pixels mattered.
Use it when
Data lies on a grid: images, video, spectrograms, satellite tiles, game boards.
Avoid it when
Data is a table, or you have few images and no pretrained model.
If not this, try: The Transformer (Vision Transformers, given lots of data) · Gradient Boosting (for tables)
Start here
Slide a filter over a digit
A plain neural network sees a picture as one long list of numbers and forgets which pixels were neighbours. A convolutional layer keeps the picture flat and slides a small grid of weights, a kernel, across it. Pick a kernel (or type your own) and point at the feature map to see where each number comes from.
Picture, 16×16 (dashed: zero padding)
Feature map, 16×16 (teal +, amber −)
Point at (or tap) the feature map to see the multiply-and-add behind one cell. Terms that are 0 are left out.
The Sobel filter: positive where ink ends going right (a left-to-right drop), negative where it starts. Flat areas cancel to 0.
How it works
Convolution, one window at a time
A 6×6 picture of a bar with a bump. At every step the kernel sits on one window: multiply the overlapping pairs, add them up, write the sum into the output. Stride moves the window more than one pixel at a time; padding adds a ring of zeros so the edges get windows of their own.
Input
Kernel
Products
Output, 4×4
- Output size
- (6 + 0 − 3) ÷ 1 + 1 = 4
- This sum
- –
Press Start to slide the kernel across the picture, one window per step.
Shrink and steady
Pooling, and what happens when the digit moves
Pooling keeps one number per 2×2 block: the biggest (max pooling) or the average. The map gets 4 times smaller, so later layers have less to do and each of their cells covers more of the picture. Move the digit to the right and compare: the feature map simply moves with it, and the pooled map changes less.
Picture, moved 1 px
Feature map, 16×16
Max pooled, 8×8
- Feature map cells unchanged
- 31%
- Pooled cells unchanged
- 41%
Pooling helps, a little: 41% of pooled cells hold steady against 31% before pooling. A one-pixel move often stays inside the same 2×2 block. It's tolerance, not immunity: move further and the pooled map changes too.
Learning the kernels
Train a tiny CNN, right here
Nobody hand-writes the kernels of a real CNN: training learns them. This one has 3,010 weights: six 3×3 kernels, then ReLU, 2×2 max pooling and one dense layer that picks the digit. It trains in your browser on 500 handwritten digits that wander up to 3 pixels off-centre, and is tested on 200 it never sees during training. Press Train and watch the kernels change.
The six kernels (random, before training)
- Epoch
- 0 of 12
- Training
- –
- Test
- –
● training digits ● test digits, after each epoch
Untrained, it guesses: about 10% right, and the kernels are random noise.
Same weights, better wiring
Why convolution beats a dense layer on pictures
Two ideas do the work. Locality: a kernel only looks at a small neighbourhood, because nearby pixels belong together. Weight sharing: the same kernel is used at every position, so a stroke is recognised wherever it appears, and the layer needs very few weights. We trained both kinds of network on the same wandering digits for 15 epochs and tested on 400 new ones.
Measured on wandering digits
- Tiny CNN3,010 weights87.8%
- Dense, 12 hidden3,214 weights73.5%
- Dense, 32 hidden8,554 weights91.3%
With about the same number of weights, the CNN gets 14 points more right. The dense network needs nearly three times the weights to edge past it. On bigger pictures the gap grows fast.
Weights in the first layer, 32 units or 32 kernels
| Picture | Dense | Conv 3×3 |
|---|---|---|
| 16×16 grey (these digits) | 8,224 | 320 |
| 28×28 grey (MNIST) | 25,120 | 320 |
| 224×224 colour (ImageNet) | 4,816,928 | 896 |
| 1024×1024 colour (a phone photo, scaled) | 100,663,328 | 896 |
A dense layer needs a weight from every pixel to every unit, so it grows with the picture. A conv layer's weights don't depend on the picture's size at all.
Know their limits
The catch, and where CNNs shine
Shift is easy; turning isn't
Convolution handles a moved pattern, but nothing in it understands a rotated or resized one. Those have to be learned from examples.
Hungry for data
Our tiny CNN overfits 500 digits. Real ones train on millions of pictures, or start from a pretrained network, and use augmentation: random shifts, flips and crops. Overfitting →
Lots of arithmetic
Every kernel visits every position in every layer: billions of multiply-adds per photo. That's why they run on GPUs.
Transformers moved in
Vision Transformers cut a picture into patches and use attention instead. With enough data they match or beat CNNs; on small data CNNs are still hard to beat. Self-attention →
Where they shine: anything laid out on a grid. Photos and medical scans, but also audio spectrograms, satellite images, board games (AlphaGo's networks were CNNs) and even text, with one-dimensional kernels sliding along a sentence.
No magic, just arithmetic
The maths, step by step
A 4×4 picture (1 is ink, 0 is blank) and the vertical-edge kernel from the playground. Compute the feature map, squash it, pool it, then see how a kernel learns.
- 1
One output, by hand
Put the kernel on the window that starts at row 2, column 1. Multiply the 9 overlapping pairs and add them up.window rows 2–4, columns 1–3: [0 1 1] [0 1 1] [0 0 1]
out(2, 1) = 1·0 + 0·1 + (-1)·1 + 2·0 + 0·1 + (-2)·1 + 1·0 + 0·0 + (-1)·1 = -4.0000
Negative: going right, this window goes from blank to ink. The full map is [-4, 3] [-4, 0].
- 2
How big is the output?
A k×k kernel moving s pixels at a time over an n×n picture padded with p rings of zeros fits ⌊(n + 2p − k) ÷ s⌋ + 1 times across.this example = (4 + 0 − 3) ÷ 1 + 1 = 2.0000
16×16 digit, 3×3 = (16 + 0 − 3) ÷ 1 + 1 = 14.0000
with padding 1 = (16 + 2 − 3) ÷ 1 + 1 = 16.0000
padding 1, stride 2 = ⌊(16 + 2 − 3) ÷ 2⌋ + 1 = 8.0000
Padding 1 keeps the size (“same” padding); stride 2 halves it.
- 3
ReLU, then max pool
ReLU sets negatives to 0, keeping only “this edge is here”. A 2×2 max pool then keeps the largest of each 2×2 block.after ReLU: [0, 3] [0, 0]
max pool = max(0, 3, 0, 0) = 3.0000
The 2×2 map became one number. On the 16×16 digits: 14×14 after the 3×3 kernel, 7×7 after pooling.
- 4
Count the weights
A conv layer's weights don't depend on the picture's size: each filter is just its 9 weights and a bias. A dense layer needs a weight from every pixel to every unit.6 filters = 6 × (3·3 + 1) = 60.0000
final dense layer = 10 × (294 + 1) = 2950.0000
the tiny CNN = 60 + 2950 = 3010.0000
dense, 12 hidden = 12 × (256 + 1) + 10 × (12 + 1) = 3214.0000
Almost all of the CNN's weights are in its last layer. The part that looks at pixels has only 60.
- 5
How a kernel weight learns
Backpropagation hands each output cell a gradient δ (how much the loss changes if that cell grows). A kernel weight touched every window, so its gradient adds up δ × the pixel it met, over all windows: another convolution, of the input with δ.input [1 2 0] [0 1 3] [2 1 1], 2×2 kernel, δ at the 2×2 output = [0.5 -1] [0 2]
∂L/∂w(1,1) = 0.5·1 + (−1)·2 + 0·0 + 2·1 = 0.5000
∂L/∂w(1,2) = 0.5·2 + (−1)·0 + 0·1 + 2·3 = 7.0000
Then w ← w − learning rate × gradient, exactly as in any network. Weight sharing just means the gradient is a sum.
- 6
How far can a layer see?
One 3×3 layer sees a 3×3 patch. A second 3×3 layer looks at 3×3 of those outputs, each already covering 3×3, so it sees 5×5 pixels. Each extra layer adds k − 1.1 layer of 3×3 = 1 + 1 × (3 − 1) = 3.0000
2 layers of 3×3 = 1 + 2 × (3 − 1) = 5.0000
3 layers of 3×3 = 1 + 3 × (3 − 1) = 7.0000
5 layers of 3×3 = 1 + 5 × (3 − 1) = 11.0000
Pooling and stride make it grow much faster, which is how deep layers come to see the whole object.
Keep going
Resources
Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.
How it works: Convolutional Neural Network
Step by step
- Positive values
- Negative values
- The kernel's current window
Press Start or Step: the step the animation is on lights up below.
- 1
Size the output
Pad the input with p rings of zeros if asked. A k×k kernel moving s pixels at a time fits ⌊(n + 2p − k) / s⌋ + 1 times across.
m = (n + 2p - k) // s + 1 - 2
Multiply and add
For each output cell, take the k×k window under the kernel, multiply pixel by weight and sum, plus the bias. The same weights are used at every position.
out[i][j] = b + Σ_a Σ_b w[a][b] · x[i·s + a][j·s + b] - 3
Feature map, then ReLU and pool
The finished map shows where the pattern is. ReLU keeps the positive matches; 2×2 max pooling halves each side. Stack more blocks, then a dense layer and softmax.
h = max_pool(relu(conv(x, w)))
A convolutional layer slides a small grid of weights, a kernel, across the picture. At each position it multiplies the pixels under it by its weights and adds them up, writing one number of the output: a feature map that is large wherever the kernel's pattern appears. Libraries compute cross-correlation (no kernel flip) and call it convolution.
Two ideas make it work on pictures. Locality: each output looks only at a small neighbourhood. Weight sharing: the same kernel is used at every position, so a pattern is found wherever it is (moving the input moves the feature map: equivariance), and a layer needs only k·k·channels weights per kernel, whatever the picture's size.
A CNN stacks conv → ReLU → pooling blocks. Pooling (usually 2×2 max) shrinks each map, so deeper layers see more of the picture and combine edges into parts and parts into objects. A dense layer at the end turns the features into class scores. Training learns every kernel by backpropagation; a kernel's gradient is the input convolved with the output's gradient.
Where you'll meet it
- Anything on a grid: photos, medical scans, satellite images, audio spectrograms, game boards; and 1-D kernels for signals and text.
- Small to medium image datasets, or fine-tuning a pretrained CNN (ResNet, EfficientNet), where CNNs' built-in assumptions still beat Vision Transformers.
- Interview favourites: “output size for this kernel, stride and padding?”, “how many parameters in this layer?”, “why weight sharing?”, “what does pooling do?”, “equivariance vs invariance”, “receptive field of stacked layers”, “1×1 convolutions?”.
Every output position of every output channel does k² · C_in multiply-adds. The weights don't depend on the picture's size, but the arithmetic and the stored activations do, which is why CNNs need GPUs and why pooling and stride matter.