Logistic Regression is a very commonly used statistical method that allows us to predict a binary output from a set of independent variables. The various properties of logistic regression and its Python implementation has been covered in this article previously. Now, we shall find out how to implement this in PyTorch, a very popular deep learning library that is being developed by Facebook.
Now, we shall see how to classify handwritten digits from the MNIST dataset using Logistic Regression in PyTorch. Firstly, you will need to install PyTorch into your Python environment. The easiest way to do this is to use the
conda tool. Visit pytorch.org and install the version of your Python interpreter and the package manager that you would like to use.
With PyTorch installed, let us now have a look at the code. Write the three lines given below to import the reqiored library functions and objects.
Here, the torch.nn module contains the code required for the model, torchvision.datasets contains the MNIST dataset. It contains the dataset of handwritten digits that we shall be using here. The torchvision.transforms module contains various methods to transform objects into others. Here, we shall be using it to transform from images to PyTorch tensors. Also, the torch.autograd module contains the Variable class amongst others, which will be used by us while defining our tensors.
Next, we shall download and load the dataset to memory.
Now, we shall define our hyper parameters.
In our dataset, the image size is 28*28. Thus, our input size is 784. Also, 10 digits are present in this and hence, we can have 10 different outputs. Thus, we set num_classes as 10. Also, we shall train for five times on the entire dataset. Finally, we will train in small batches of 100 images each so as to prevent the crashing of the program due to memory overflow.
After this, we shall be defining our model as below. Here, we shall initialise our model as a subclass of torch.nn.Module and then define the forward pass. In the code that we are writing, the softmax is internally calculated during each forward pass and hence we do not need to specify it inside the forward() function.
Having defined our class, now we instantiate an object for the same.
Next, we set our loss function and the optimiser. Here, we shall be using the cross entropy loss and for the optimiser, we shall be using the stochastic gradient descent algorithm with a learning rate of 0.001 as defined in the hyper parameter above.
Now, we shall start the training. Here, we shall be performing the following tasks:
- Reset all gradients to 0.
- Make a forward pass.
- Calculate the loss.
- Perform backpropagation.
- Update all weights.
Finally, we shall be testing out model by using the following code.
Assuming that you performed all steps correctly, you will get an accuracy of 82%, which is far off from today’s state of the art model, which uses a special type of neural network architecture. For your reference, you can find the entire code for this article below:
- ML | Logistic Regression using Python
- ML | Why Logistic Regression in Classification ?
- Understanding Logistic Regression
- ML | Logistic Regression using Tensorflow
- ML | Cost function in Logistic Regression
- Linear Regression using PyTorch
- ML | Logistic Regression v/s Decision Tree Classification
- ML | Kaggle Breast Cancer Wisconsin Diagnosis using Logistic Regression
- Python | Classify Handwritten Digits with Tensorflow
- Handwritten Equation Solver in Python
- Python | PyTorch tan() method
- Python | PyTorch cos() method
- Python | PyTorch sin() method
- Deep Learning with PyTorch | An Introduction
- Python | PyTorch atan() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.