Neural Networks
Convolution / CNN
A convolutional neural network doesn't look at an image all at once. It scans a small window over it, looking for specific local patterns. Slide the kernel across the image and watch how simple math extracts meaningful features.
How a convolutional kernel slides over an image to compute a dot product at each step, building an output feature map cell by cell.
Stage 1 of 4: The Input
Kernel at position 0, computing dot product sum 0
- Input Pixel
To a computer, an image is just a grid of numbers. We can think of these as pixel intensities.
Check your understanding
4 questions in the bank. Each attempt draws a fresh set in a fresh order, so a second go is a real second go.
What you are looking at
On the left is an input image, shown as a grid of pixel intensities. On the right is the output feature map. In the middle is the operation connecting them: the dot product.
When a convolutional layer processes an image, it slides a small grid of weights — the kernel or filter — across the input. At every step, it performs a simple calculation to produce one number in the output grid.
The Dot Product
The calculation is an element-wise multiplication followed by a sum.
If your kernel is 3x3, it looks at a 3x3 patch of the image. It multiplies the top-left pixel of the patch by the top-left weight of the kernel, the top-middle by the top-middle, and so on. Then it adds all nine results together.
That single sum becomes one pixel in the output feature map.
A magnifying glass for patterns
Think of the kernel as a template or a stencil. When the pattern in the image patch matches the pattern in the kernel weights, the multiplication produces large positive numbers, and their sum is high.
When the image patch doesn't match the kernel at all, the positive and negative results cancel out, leaving a sum near zero. The kernel is literally scanning the image asking, "Is my specific pattern right here?"
Weight Sharing
Notice that as you slide the kernel, the weights inside it never change. The exact same filter is applied to the top-left corner as to the bottom-right corner.
This is the core insight of CNNs, called weight sharing. A vertical edge looks like a vertical edge no matter where it appears in the image. By reusing the same small set of weights across the entire image, the network drastically reduces the number of parameters it needs to learn, while gaining translation invariance (the ability to recognize a feature regardless of its position).
Different Kernels, Different Features
Change the kernel type and the output map changes entirely.
An edge detection kernel uses positive weights on one side and negative weights on the other. It produces a high score only when it sits exactly on a boundary where dark pixels meet light pixels.
A blur kernel acts like a local average, smoothing out sharp transitions.
In a real neural network, you don't hand-code these weights. The network starts with random kernels and, through backpropagation, learns the specific numbers that extract the most useful features for its task — perhaps edges in the first layer, textures in the second, and complex shapes in deeper layers.
The Shrinking Image
If you apply a 3x3 kernel to a 6x6 image without adding any artificial borders, the kernel can only fit in 4 horizontal positions and 4 vertical positions before it falls off the edge.
As a result, your 6x6 image produces a 4x4 feature map.
To prevent the image from shrinking at every layer, real networks often use padding — adding a border of zeros around the original image so the kernel has room to slide further, keeping the output map the exact same size as the input.
The failure of the missing feature
Try applying a strictly vertical edge detector to an image that only contains horizontal lines.
The output feature map will be entirely empty (all zeros). This isn't a bug; it is the correct mathematical result. The dot product is telling you that the specific feature this kernel cares about simply does not exist in the input.
In a real network, this is why a single layer uses hundreds of different kernels simultaneously — if one filter finds nothing, another might find exactly what it's looking for.
Explore Next
Reference
- Kernel (Filter)
- A small matrix of weights used to extract features
- Stride
- How many pixels the kernel moves at each step (here, 1)
- Padding
- Adding pixels to the border to control output size (here, valid/none)
- Feature Map
- The output image produced by the convolution
- Dot Product
- Element-wise multiplication followed by a sum
Break it on purpose
Apply an edge-detection kernel to an image that has no matching edges, resulting in a blank feature map, showing that the filter only activates when its specific pattern is present.