Identifying handwritten digits using Logistic Regression in PyTorch

Identifying handwritten digits using Logistic Regression in PyTorch

Using logistic regression for handwritten digit classification in the context of the MNIST dataset is an introductory approach to neural networks. Though more advanced models like CNNs perform better for this task, starting with logistic regression is a good way to grasp the basics.

Let's implement handwritten digit classification using logistic regression in PyTorch:

  1. Set Up

    Start by importing necessary libraries:

    import numpy as np import torch import torch.nn as nn import torch.optim as optim import torchvision.transforms as transforms from torchvision.datasets import MNIST from torch.utils.data import DataLoader 
  2. Load Data

    Use torchvision to load the MNIST dataset:

    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) train_dataset = MNIST(root='./data', train=True, transform=transform, download=True) test_dataset = MNIST(root='./data', train=False, transform=transform) train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True) test_loader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=False) 
  3. Define the Logistic Regression Model

    Though it's called logistic regression, it's essentially a single-layer neural network:

    class LogisticRegression(nn.Module): def __init__(self, input_dim, output_dim): super(LogisticRegression, self).__init__() self.linear = nn.Linear(input_dim, output_dim) def forward(self, x): return self.linear(x) input_dim = 28 * 28 # size of MNIST image output_dim = 10 # 10 classes model = LogisticRegression(input_dim, output_dim) 
  4. Loss and Optimizer

    criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.01) 
  5. Training the Model

    num_epochs = 10 for epoch in range(num_epochs): for images, labels in train_loader: # Flatten the image images = images.view(-1, 28*28) # Forward pass outputs = model(images) loss = criterion(outputs, labels) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}') 
  6. Evaluate the Model

    with torch.no_grad(): correct = 0 total = 0 for images, labels in test_loader: images = images.view(-1, 28*28) outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print(f'Accuracy: {100 * correct / total}%') 

This logistic regression model will give you a basic understanding of the workflow in PyTorch and the fundamentals of classification. For higher accuracy on tasks like MNIST, deep neural networks or convolutional neural networks are typically used.


More Tags

asp.net-identity bash-completion unique-id internet-explorer-11 strlen treeview maven-surefire-plugin itunes cross-join sylius

More Programming Guides

Other Guides

More Programming Examples