How do convolutional neural networks actually work?
Convolutional neural networks (CNNs) work by sliding small grids of weights, called filters, over input data to detect local patterns like edges, textures, and shapes. They process these local features through multiple layers of mathematical operations and spatial reduction to build complex, high-level representations. This hierarchical feature extraction makes them highly effective for analysing spatial data like images and audio spectrograms.
To understand why this approach is necessary, you have to consider how a standard feedforward neural network handles an image. A standard network flattens a two-dimensional image into a single one-dimensional vector. This destroys the spatial relationships between the pixels. If a standard network learns to identify a dog in the top left corner of a photograph, it will completely fail to recognise that same dog if it appears in the bottom right corner. The network has tied specific learned weights to specific pixel locations.
Convolutional neural networks solve this problem through a concept called weight sharing. Instead of learning separate weights for every single pixel, a CNN learns a set of small filters. Because the exact same filter sweeps across the entire image, a feature learned in one location is automatically recognised anywhere else. This property is known as translation invariance, and it drastically reduces the number of parameters the model needs to learn while vastly improving its ability to generalise to new data.
The core mechanism of convolution
The foundational building block of a CNN is the convolutional layer. The input to this layer is typically a three-dimensional tensor representing the image height, width, and colour channels. A standard photograph has three channels representing red, green, and blue pixel intensities. A filter is a much smaller three-dimensional tensor, such as a 3x3x3 grid of weights. The depth of the filter always matches the depth of the input volume it is looking at.
During the forward pass, the network places this filter over the top-left corner of the input image. It performs an element-wise multiplication between the weights in the filter and the pixel values in that specific patch of the image. The network then sums all these products together, adds a single bias term, and outputs a single number. This operation is a standard mathematical dot product. The filter then slides over by a set amount, repeats the calculation, and generates the next number.
As the filter completes its sweep across the entire image, it produces a new two-dimensional grid of numbers called a feature map. A single convolutional layer will contain multiple filters, perhaps 64 or 128 of them. Each filter learns to look for a different specific pattern. One filter might output high values when it encounters a vertical edge, while another might activate strongly for a specific colour gradient. The output of the entire layer is a stack of these feature maps, which becomes the input for the next stage of the network.
Controlling spatial dimensions with stride and padding
When designing a convolutional layer, engineers must define how the filter moves across the image. The step size of the filter is called the stride. A stride of one means the filter shifts by a single pixel at a time. A stride of two means the filter skips a pixel with every move. Increasing the stride is a deliberate design choice used to reduce the spatial dimensions of the feature maps, summarising the information and reducing the computational burden for subsequent layers.
Another critical consideration is what happens at the edges of the image. If you apply a 3x3 filter to a 32x32 image with a stride of one, the filter cannot centre itself on the outermost pixels without falling off the edge of the data. By default, the convolution operation simply stops before this happens. This is known as “valid” padding, and it results in an output feature map that is slightly smaller than the input, specifically 30x30 in this case. This can be problematic in deep networks because the image shrinks with every layer, and the pixels on the very edge of the original image are barely processed.
To prevent this shrinking, we use a technique called “same” padding. This involves adding a border of artificial pixels, usually containing zeros, around the outside of the input image. By padding the input, the filter has enough room to centre itself on the original edge pixels. If we add a one-pixel border of zeros to our 32x32 image, the input effectively becomes 34x34. Applying the 3x3 filter then produces an output feature map that perfectly retains the original 32x32 spatial dimensions.
Introducing non-linearity and pooling
A convolutional operation is strictly linear. It is just a series of multiplications and additions. If a network consisted only of convolutional layers, it would mathematically collapse into a single linear transformation and fail to learn complex real-world patterns. To break this linearity, we pass the output of the convolution through an activation function. The standard choice in modern networks is the Rectified Linear Unit, usually referred to as ReLU. ReLU simply takes every number in the feature map and replaces any negative value with a zero, leaving positive values unchanged.
Immediately following the activation function, CNNs typically employ a pooling layer. Pooling systematically downsamples the spatial dimensions of the feature maps. The most common variation is max pooling. A max pooling layer slides a small window, usually 2x2 with a stride of two, across the feature map. At each step, it simply extracts the maximum value within that window and discards the rest. This halves the height and width of the feature map, throwing away 75 percent of the spatial data.
While discarding data sounds counterproductive, pooling serves a vital purpose. It forces the network to retain only the most prominent features while building spatial invariance. If an edge in a photograph shifts by one or two pixels, the max pooling operation will likely still capture the exact same maximum activation value. Furthermore, as the spatial dimensions shrink, the subsequent filters effectively look at a larger proportion of the original image. This growing receptive field allows deeper layers to combine simple edges into complex shapes, like combining curves and lines to recognise an eye or a wheel.
Flattening and the fully connected layers
The convolutional and pooling layers act as the feature extraction base of the network. By the end of this pipeline, the initial wide and flat image has been transformed into a deep, narrow block of high-level feature maps. However, this three-dimensional tensor cannot directly output a final classification. To bridge the gap between feature extraction and classification, the network must reshape the data.
This is achieved using a flatten layer. This layer takes the three-dimensional tensor and unrolls it into a single, long one-dimensional vector. If the final pooling layer outputs 64 feature maps that are each 4x4 in size, the flatten layer will string them end-to-end to create a vector containing 1,024 numbers. This vector now represents all the high-level features extracted from the image, completely detached from their original spatial grid.
This flat vector is then fed into one or more fully connected layers, identical to those found in standard artificial neural networks. These dense layers act as the classification head. They learn which combinations of the high-level features correspond to specific output classes. The very last dense layer contains one neuron for every possible category. For a multi-class problem, this final layer uses a Softmax activation function to convert the raw numerical outputs into a clean probability distribution, ensuring all class probabilities sum to exactly one.
How to answer this in an exam
When examiners ask you to explain how convolutional neural networks function, they are testing your understanding of their architectural purpose alongside the mathematics. You will lose marks if you only describe the mechanics of sliding a filter. You must explicitly state that CNNs solve the parameter explosion problem of standard dense networks by using weight sharing. You should also state that the alternating convolution and pooling layers create a spatial hierarchy, allowing the network to build complex representations from simple local features.
A very common exam question requires you to calculate the number of learnable parameters in a specific convolutional layer. You must memorise the formula for this. The number of weights is the filter height multiplied by the filter width, multiplied by the number of input channels. You then add one for the bias term. Finally, you multiply that entire sum by the number of filters in the layer.
For example, if a layer has twenty 5x5 filters operating on an RGB image, the calculation is: (5 x 5 x 3 weights + 1 bias) x 20 filters. This equals 1,520 parameters. Always write down the full working before providing the final number, as mark schemes frequently award method marks even if your final arithmetic is wrong. We cover exactly how to lay out these calculations to guarantee full marks in the Full Marks Press Neural Networks guide, which includes step-by-step worked exam questions.
Frequently Asked Questions
What is the difference between a filter and a kernel? In introductory texts, these terms are often treated as synonyms. Strictly speaking, a kernel is a two-dimensional array of weights. A filter is a three-dimensional collection of those kernels stacked together. If an input image has three colour channels, the filter looking at it will contain three distinct two-dimensional kernels.
Why do we use ReLU instead of Sigmoid in CNNs? ReLU is computationally cheaper because it only requires checking if a number is greater than zero, rather than calculating an exponential function. More importantly, ReLU prevents the vanishing gradient problem during backpropagation. Its gradient is a constant one for positive inputs, allowing errors to propagate strongly through very deep networks.
Can convolutional networks process one-dimensional or three-dimensional data? Yes. One-dimensional CNNs are highly effective for time-series data and audio signals, sliding a one-dimensional filter across a time axis. Three-dimensional CNNs are used for video data or volumetric medical scans, such as MRI data, where the filter moves across height, width, and depth simultaneously.
What does a 1x1 convolution actually do? A 1x1 convolution acts as a channel-wise pooling layer. It does not look at spatial neighbours, but instead computes a linear combination of the channels at a single pixel location. It is primarily used to reduce the depth of feature maps, which drastically cuts down the computational cost before passing the data into more expensive 3x3 or 5x5 convolutional layers.
Looking for more complete examples and exam-specific practice? You can download a free copy of our revision materials at fullmarkspress.com/free.