Simple CNN using PyTorch

Srimanth Tenneti
Analytics Vidhya
Published in
4 min readMay 1, 2020

This article is a simple guide that will help you build and understand the concepts behind building a simple CNN.By the end of this article you will be able to build a simple CNN based on the PyTorch API and will classify clothing using the FashionMNIST dateset. This is assuming you have prior knowledge to Artificial Neural Networks.

CNN

The concept of CNN or Convolution Neural Networks was popularized by Yann André LeCun who is also known as the father of the convolution nets.

A CNN works very similar to how our human eye works. The core operations that are behind the CNN’s are matrix additions and multiplications.So, there is no need to get worried about them.

But to know about the working of the CNN’s we need to know how the image gets stored in the computer.

The above example shows us how the image is stored which is in the form of arrays.

But these are only grey scale images. So an RGB or a color image is 3 such matrices stacked on one other.

The above multi-dimension matrix represents a colored image. But in this article we would discuss the classification of grey scale images.

CNN Architecture

The core function behind a CNN is the convolution operation. It is multiplication of the image matrix with a filter matrix to extract some important features from the image matrix.

The above diagram shows the convolution operation on an image matrix. The convolution matrix is filled by moving the filter matrix through the image matrix.

Another important component of a CNN is called the Max-pool layer. This helps us in reducing the number of features i.e. it sharpens them so that our CNN performs better.

To all of the convolutional layers we apply the RELU activation function.

While mapping the convolutional layers to the output we need to use a linear layer. So we use layers called the fully connected layers abbreviated as fc.

The final fc’s activation mostly is a sigmoid activation function.

We can clearly see the output maps between 0 and 1 for all input values.

So now you are aware of the layers we are going to use. This knowledge is enough for building a simple CNN but one optional layer call the dropout will help the CNN perform well. Dropout layer is placed in between the fc layers and this randomly drops the connection with a set probability which will help us in training the CNN better.

Our CNN architecture , but at the end we will add a dropout between the fc layers.

Without wasting anymore time we will get into the code

Code

The full code is in my git hub and can be accessed from the above link. I have put comments in the code that will help you navigate through it. Dont panic looking at it just go step by step it is simple and can be easily understood.

Conclusion

So with the end of this you will be able to build simple CNN’s. I recommend you checkout the other datasets from PyTorch and implement CNN’s.

In case you need my help my Linkedin link is below.

--

--