Adapted from an appendix of my MS thesis.
Convolutional Neural Network
The convolutional neural network (CNN) is used to apply neural networks for images. In this case, we replace matrix multiplication with a convolution operation. The basic idea is to divide the input into overlapping 2D image patches, and to compare each patch with a set of small weight matrices, or filters, which represents part of an object [1].
This can be thought of as a form of template matching. We learn these templates from data. Since the templates are small (often just or ), the number of parameters needed for learning is small. Additionally, since we use convolution to do template matching, instead of matrix multiplication, the model will be translationally invariant. This is useful for tasks such as image classification, where the goal is to classify if an object is present regardless of its location [1].
The 1D convolution between two function is defined as follows [1].
Now suppose we replace the functions with finite-length vectors, which we can think of as functions defined on a finite set of points. Let be evaluated at the points to yield the weight vector (also called a filter or kernel) up to . Now let be evaluated at the points to yield the feature vector up to . Then the equation becomes the following. We see that we flip the weight vector , and then drag it over the the vector, summing up the local windows at each point [1].
When we do not flip first, then we get the cross correlation [1].
If the weight vector is symmetric, then cross correlation and convolution are the same. In the deep learning literature, the term “convolution” is often used to mean cross correlation. We can also evaluate the weights on the domain and the features on the domain , to eliminate negative indices [1].
In 2D, the equation becomes the following where the 2D filter has size [1].
We can think of 2D convolution as template matching, since the output at a point will be large if the corresponding image centered on is similar to . More generally, we can think of convolution as a form of feature detection. The resulting output is therefore called a feature map [1].
In the figure we see that convolving a image with a filter results in a output. This is called valid convolution, since we only apply the filter to valid parts of the input, and we do not let it slide off the ends. If we want the output to have the same size as the input, we can use zero-padding, where we add a border of zeros to the image. This is called same convolution [1].
Since each output pixel is generated by a weighted combination of inputs in its receptive field, neighboring outputs will be very similar in value, since their inputs are overlapping. We can reduce this redundancy and speedup computation by skipping every number of inputs. This is called strided convolution [1].
Convolution will preserve information about the location of input features, a property known as equivariance. In some cases we want to be invariant to the location. For example, when performing image classification, we may just want to know if an object of interest is present anywhere in the image. A simple way to achieve this is called max pooling, which computes the maximum over its incoming values. An alternative is to use average pooling, which replaces the maximum with the mean. A common design pattern is to create a CNN by alternating convolutional layers with max pooling layers, followed by a final linear classification layer at the end [1].
References
- Kevin P. Murphy (2022) Probabilistic Machine Learning: An Introduction. MIT Press.
- Dumoulin, Vincent, Visin, Francesco (2016) A guide to convolution arithmetic for deep learning. ArXiv e-prints.
- Aphex34 (2015) Max pooling.


![The first 6 of 25 convolutions with padding [2].](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1ipx0hcrok3gktun9old.png)
![The first 6 in 9 convolutions with padding and strides [2].](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2gek6ks3jcqzgd9m8wja.png)
![Max pooling [3].](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7bmunmq4n0vwd8n8cmgz.png)
Top comments (0)