How to implement logistic regression using pytorch

This recipe helps you implement logistic regression using pytorch

Recipe Objective

How to implement logistic regression using pytorch?

As we all know what is regression it is nothing but the relationship between dependent and independent variable, the logistic regression is used when the dependent variable is categorical. So we can say logistic regrression is a relationship between the one dependent categorical variable with one or more nominal, ordinal, interval variables. Lets understand with practical implementation how to use logistic with pytorch. There are various operations that we have performed here.

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

Step 1 - Import library

import torch
from torch.autograd
import Variable
import torchvision.transforms as transforms
import torchvision.datasets as datasets

Step 2 - Loading our dataset

mnist_train = datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
mnist_test = datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor())

From the torchvision.datasets package we have loaded the most popular datasets which is MNIST data set to perform our further operations onto it.

Step 3 - Make our data iterable

mnist_train_loader = torch.utils.data.DataLoader(dataset=mnist_train, batch_size=64, shuffle=True)
mnist_test_loader = torch.utils.data.DataLoader(dataset=mnist_test, batch_size=64, shuffle=False)

To make our dataset iterable we have used dataloader over here whoch help this operation to get performed.

Step 4 - Discover the model class

batch_size = 100
n_iters = 3000
epochs = n_iters / (len(mnist_train) / batch_size)
input_dim = 784
output_dim = 10
lr_rate = 0.001

Here we have defined some of the important parameters which are requiered in our model building as well as in training of our model.

Step 5 - Create model class

class LogisticRegression_class(torch.nn.Module):
     def __init__(self, input_dim, output_dim):
         super(LogisticRegression_class, self).__init__()
         self.linear = torch.nn.Linear(input_dim, output_dim)
     def forward(self, x):
         outputs = self.linear(x)
         return outputs

Step 6 - Initialize model

logistic_model = LogisticRegression_class(input_dim, output_dim)

Step 7 - Compute loss

criteria = torch.nn.CrossEntropyLoss()

Step 8 - Discover optimizer class

optim = torch.optim.SGD(logistic_model.parameters(), lr=lr_rate)

Step 9 - Training our model

iterations = 0
for epoch in range(int(epochs)):
    for i, (images, labels) in enumerate(mnist_train_loader):
        images = Variable(images.view(-1, 28 * 28))
        labels = Variable(labels)
        optim.zero_grad()
        outputs = logistic_model(images)
        compute_loss = criteria(outputs, labels)
        compute_loss.backward()
        optim.step()
        iterations+=1
        if iterations%500==0:
            correct = 0
            total = 0
            for images, labels in mnist_test_loader:
                images = Variable(images.view(-1, 28*28))
                outputs = logistic_model(images)
                val, prediction = torch.max(outputs.data, 1)
                total+= labels.size(0)
                correct+= (prediction == labels).sum()
            accuracy = 100 * correct/total
            print("Iteration: {}. Loss: {}. Accuracy: {}.".format(iterations, compute_loss.item(), accuracy))

Iteration: 500. Loss: 1.8662378787994385. Accuracy: 67.4000015258789.
Iteration: 1000. Loss: 1.5380983352661133. Accuracy: 75.1500015258789.
Iteration: 1500. Loss: 1.3004997968673706. Accuracy: 78.56999969482422.
Iteration: 2000. Loss: 1.2053605318069458. Accuracy: 80.9800033569336.
Iteration: 2500. Loss: 1.0875484943389893. Accuracy: 81.94999694824219.
Iteration: 3000. Loss: 1.001572847366333. Accuracy: 82.83000183105469.
Iteration: 3500. Loss: 0.8407369256019592. Accuracy: 83.43000030517578.
Iteration: 4000. Loss: 1.0187973976135254. Accuracy: 83.97000122070312.
Iteration: 4500. Loss: 0.8073099851608276. Accuracy: 84.33999633789062.

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python