Logistic regression is a commonly applicable statistical method which allows prediction of binary output from a set of independent variables. We have covered the properties of logistic regression and its implementation in the previous article. Now we will learn the implementation of PyTorch which is a very popular deep learning library being developed by Facebook in Python for Data Science.
We will now see process of classifying handwritten digits from the MNIST dataset using Logistic regression in PyTorch. First we have to install PyTorch into the Python environment. We can do this easily using the pip or conda tool. Below we have provided three lines of code for importing the required library functions and objects.
In the above code, torch.nn module contains code required for model, torchvision.datasets contains MNIST dataset. It houses the dataset of handwritten digits that we shall use here. The torchvision.transforms module contains methods for transformation of objects into others. We will use it here for the transformation of images to PyTorch tensors. The torch.autograd module contains variable class amongst others. This will be there for defining our tensors.
Now we will download and load the dataset to memory.
Now we will define our hyper parameters.
In the dataset provided the image size is 28*28. So, our input size is 784. But also it consists of 10 digits so, we can have 10 different outputs. Thus we will set the num_classes as 10. It also means we should train for five times on the entire dataset. At last, we will be training in small batches of 100 images in order to prevent crashing of program due to memory overflow.
Now we will define our model as below. We shall initialize our model as a subclass of torch.nn module and define the forward pass. In the code, the softmax is internally calculated during each forward pass, and there is no need for specifying it in the forward() function.
As we have defined our class, we will instantiate an object for the same.
Now we will set our loss function and optimizer. We shall use the cross-entropy loss and for optimizer, we shall ue the stochastic gradient descent algorithm with a learning rate of 0.001 defined in hyperparameter.
Now in training we will perform the following tasks:
- We will reset all gradients to 0.
- Making a forward pass.
- Calculating the loss.
- Performing backpropagation.
- Updating all the weighs.
We will finally test our model using the code given below.
If we perform all steps correctly, we will get an accuracy of 82%. The entire code of the article is given below.