Building A Simple Convolution Layer From Scratch

Saurav Suresh
The Startup
Published in
4 min readMay 17, 2020

I am going to walk through a way to implement convolution layers in C++ without the aid of any machine learning libraries. I will go over how feedforwarding and backpropagation will work so that by the end we will be able to construct a convolution layer that can be connected to a fully connected neural network. In most situations, models involving convolution layers are significantly more complex, but for now, we will keep it simple.

c
Layers of a Convolution Neural Network — Meghna Asthana — Medium

What is a Convolution Layer?

A convolution layer provides a method of producing a feature map from a two-dimensional input. This is accomplished by running a filter over the input data. The filter is just a set of weights that must be trained to identify a feature in regions of the input data. These features can be things like edges, points, or more complex information. The filter will have dimensional constraints that indicate width and height, and it will scan over the input data.

Outline of The Convolution Layer — Hiromu Yakura

The image above is an excellent illustration of how the filter operates. It will traverse over the input data and in the process output a new layer composed of the dot products between the weights and portions of the input data.

Laying the Groundwork

Before we can get into the meat of the task at hand, we must plan out the architecture of our code. When we first create our layer, we need to know the kernel size, the stride length, and the learning rate. We must then initialize our filter weights.

Feed Forward

Now we can move on to feeding forward. This is relatively simple, as all we need to do is convolve over the input data with our filter. In order to do this, we will keep shifting our x and y coordinates based on our stride length. From these x and y coordinates, we will consider the data points in the input data layer which overlap with our filter. We will then store the output as it will be necessary for seeing the results of our convolution layer.

Backpropagation

There is one more major step in our venture: the backpropagation. This is the learning part of our convolution layer as it will work to change our filter weights to minimize error. I will be writing the code in the context of a convolution layer that will be part of a larger model and therefore connected to a fully connected net or another convolution layer. As a result, my backpropagation will involve using the gradient of another layer in order to compute the gradient of the current layer.

This may seem confusing at a glance, but once broken down it becomes easier to digest. The loops serve the same purpose as the loops in the feed-forward function. The difference this time is that we are using the gradient associated with a certain output node and the input associated with a certain weight in the filter in order to change the weight. This change will serve to minimize the error of the model.

Scope

So far, I have only covered a small but fundamental part of a convolution network. Usually, a convolution network will be composed of multitudes of layers in order to extract complex features from data. But it remains important for these small parts to be understood. I encourage readers to build on this existing code in order to gain an even better understanding of convolution networks. On a future date, I will elaborate on how to implement this code within a more complete model.

I hope this walkthrough has provided some insight into how convolution layers operate and how they can be constructed from scratch. If you are endeavoring to develop your own machine learning projects from scratch, it may be useful for you to make use of convolution layers and their incredible ability to process two-dimensional data. There remain many details about convolution networks that I have not touched upon. Things like padding, pooling layers, channels, etc. are common features of deep learning models that use convolution networks. If you wish to learn more about these details I would recommend this video. If you want to test your skills on real-world projects, I recommend checking out Omdena´s collaborative AI projects.

--

--

Saurav Suresh
The Startup

Community Builder for Omdena | Writing about Machine Learning Algorithms and Mathematics.