What is a convolution

AI from scratch · 10 of a series

What is a convolution: lesson transcript

1. A picture is numbers

Zoom into any photograph far enough and it becomes a grid of numbers: one per pixel, 0 for black, 1 for white. This one is twelve by twelve. A bright square, a dim bar, a soft background. Everything a network will ever know about the picture is in these 144 numbers.

2. A 3×3 window

Now a second, much smaller grid of numbers: the kernel, nine of them. Lay it over the top-left corner of the picture. Multiply each picture pixel by the kernel number sitting on top of it, add the nine products up, and you get one number. That number becomes the first pixel of a new picture.

3. Slide it

Move the window one pixel to the right and do it again. Then again, across the whole row, then down to the next. A hundred positions, a hundred numbers, and the new picture fills in. The same nine kernel numbers are used at every single position.

4. What the new picture shows

Look at the result with this kernel: negative numbers on the left, positive on the right, zero in the middle. Wherever the picture goes from dark to bright as you move right, the output is strongly positive; bright to dark gives strongly negative; flat regions give nothing. The kernel is a vertical-edge detector. The new picture is a map of where the edges are.

5. Pick a kernel

Nine numbers decide what the new picture shows. Blur averages neighbours; sharpen exaggerates differences; the two edge kernels find edges in one direction each; emboss fakes a light source. Same sliding, same multiply-and-add, completely different results.

6. Nine dials, not a hundred

Here is why this matters for learning. A neuron that looked at the whole picture would need 144 weights, and a fresh set for every output pixel. A kernel needs nine, reused everywhere: a square in the corner and a square in the middle are found by the same nine numbers. And nothing says the nine must be hand-picked. Make them dials, and gradient descent will find kernels of its own.

7. Name it: convolution

Sliding a kernel across a picture and writing down the sums is a *convolution*, and the output is a *feature map*. A convolutional network learns dozens of kernels, each producing its own map of where its pattern occurs, and then does it again on the maps. That stacking is the next explainer.

8. Did it land?

Same kernel, picture moved.

Check your understanding

You shift the whole picture two pixels to the right and run the same kernel. What happens to the feature map?

The kernel does the same multiply-and-add at every position, so whatever it found at column c it now finds at column c + 2. A convolution does not care where a pattern is, only that it is there; the map just moves with the picture. That is exactly what makes it good at pictures.