Skip to main content

RECOGNITION OF HANDWRITTEN DIGITS(MNIST DATA) PART ONE USING PYTORCH



HELLO GUYS, IN THIS BLOG POST I WANT TO SHOW THE BASIC INTRO TO IDENTIFICATION OF HANDWRITTEN DIGITS USING PYTORCH MNIST DATA.
MNIST DATA IS THE COLLECTION OF HANDWRITTEN DIGITS. MNIST CONTAINS 70,000 HANDWRITTEN DIGITS, 60,000 FOR TRAINING AND REMAINING 10,000 ARE FOR TESTING.  THE  IMAGES ARE GRAYSCALE AND 28x28 PIXELS. WE CAN DOWNLOAD THE DATASET USING THE CODE BELOW.

Here, the parameters batch_size is kept to 64 so that the training images are grouped into 64 each and shuffle is kept to TRUE, such that each time we run the code it shuffles the data and returns an iterable with new groups of batch_size.

As the trainloader is iterable, we are iterating through it and collecting the first batch of images and it's corresponding labels into images and labels respectively.

Now, run the above code and see the output.

you will see something like this.

torch.Size([64, 1, 28, 28])
torch.Size([64]) 

It shows that there are 64 images with grayscale and they are 28x28 pixels.

Now see the first image in the first batch by running the below code.


The shape of the first image in the first batch can be known by running the following code.

print(images[0].shape)

OUTPUT: torch.size([1,28,28])

Here, plt.imshow()  plots a 2D-image with the first parameter of the 2D numpy array which contains the values of the image.  To convert the torch tensor to numpy array of 2 dimensions, we use

images[0].numpy().squeeze()

Now, it is time for constructing a neural network.

The first layer contains 28x28 values i.e., 784 values.

The middle/hidden layer contains 256 neurons.

The output layer contains 10 neurons whose values represent the probability of the numbers.

To convert the torch tensor of size [1,28,28] to continuous 784 neurons, we will write

images[0].view(1,-1) 

This will generate a tensor of 1 row and 784 columns.

We will use torch.randn() for generating the weights for these neurons.

In the first layer, we will generate random weights of size (784,256) i.e., with 784 rows and 256 columns.

For the neurons in the second layer, we will generate random weights with size (256,10).

As this is the classification problem with multiple classes, we will apply the softmax function to the values of the second and also the last layer to convert the values into the probability.

While applying the softmax function we will apply the normalization concept to avoid the nan values.

i.e., exp(x)/sigma(exp(x)) is converted into exp(x-y)/sigma(exp(x-y)) , where y is the maximum value of the given vector.

Now, the full code will be:

In the next blog, I will say about the nn module in pytorch which simplifies our work.
Find the code in this blog in https://github.com/VallamkondaNeelima/MachineLearning/blob/master/mnist1.py

Follow me on instagram: https://www.instagram.com/neelima2312/


Comments

Popular posts from this blog

CHATBOT WITH SPEECH RECOGNITION AND PYTTSX3 USING PYTHON

NOTE: VIEW THIS POST IN DESKTOP SITE FOR THE BEST EXPERIENCE Hello guys. Welcome to my blog. Here I will explain to you how to create a chatbot that has speech as it’s both input and also output. So, let us get started. In this tutorial, I am using some of the libraries in Python like SpeechRecognition, Pyaudio, Chatterbot et cetera. I am going to explain to you how to install these libraries and work with them one by one separately and at last how to integrate them. SPEECH TO TEXT At first, you have to install the Speech Recognition library. You should not use the Python3.7 version because it does not support speech recognition. I am using a 3.6 version for this tutorial. Now, you should install the speech recognition library from the command prompt. Use the following command. pip3.6 install SpeechRecognition Now, you should install PyAudio. First, you have to find the version of your Python and also the configuration of your machine. Open your cmd and type p...

LENGTH OF LONGEST VALID SUBSTRING

GIVEN A STRING OF PARENTHESIS, PRINT THE LENGTH OF THE LONGEST BALANCED SUBSTRING . FOR EXAMPLE: GIVEN,                1.  ()() ---> 4                 2.  ()())()()() ---> 6  Brute Force Approach:   It is to find all the substrings, and check whether they are balanced or not, and simultaneously updating the result if we find a larger valid substring than the previous one. But, this takes the time of O(N^3), since finding all the substrings take O(N^2), and for each substring, to find whether it is a valid substring, it takes a time of O(N), so this constitutes a total of O(N^3).  USING STACK:  In this approach, we will maintain a stack to store the indices of the traversed opening brackets i.e., '('. [YOU WILL KNOW THE REASON SOON, KEEP READING THE POST].  And a variable res , to store the length of the longest substring known till now....