Logistic Regression on MNIST with PyTorch

Logistic Regression on MNIST with PyTorch

Implementing logistic regression for the MNIST dataset using PyTorch involves several steps, including loading the dataset, defining the logistic regression model, specifying the loss function and optimizer, and training the model. Here��s a step-by-step guide:

1. Import Required Libraries

First, ensure you have PyTorch and torchvision installed. If not, you can install them using pip:

pip install torch torchvision 

Then, import the necessary modules:

import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms from torch.utils.data import DataLoader 

2. Load and Prepare the MNIST Dataset

MNIST is a dataset of handwritten digits. Use torchvision to load and prepare this dataset.

# Define a transform to normalize the data transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) # Download and load the training data trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform) trainloader = DataLoader(trainset, batch_size=64, shuffle=True) # Download and load the test data testset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=False, transform=transform) testloader = DataLoader(testset, batch_size=64, shuffle=True) 

3. Define the Logistic Regression Model

In PyTorch, logistic regression is just a single-layer neural network with no hidden layers.

class LogisticRegression(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(28 * 28, 10) # 28*28 input features and 10 outputs def forward(self, x): x = x.view(x.shape[0], -1) # Flatten the image tensor return self.linear(x) model = LogisticRegression() 

4. Define the Loss Function and Optimizer

Use Cross-Entropy as the loss function and SGD (Stochastic Gradient Descent) as the optimizer.

# Loss function criterion = nn.CrossEntropyLoss() # Optimizer optimizer = optim.SGD(model.parameters(), lr=0.01) 

5. Train the Model

Train the model using the training dataset.

epochs = 15 for e in range(epochs): running_loss = 0 for images, labels in trainloader: # Training pass optimizer.zero_grad() output = model(images) loss = criterion(output, labels) # Backward pass loss.backward() # Optimizer step optimizer.step() running_loss += loss.item() else: print(f"Training loss: {running_loss/len(trainloader)}") 

6. Test the Model

Evaluate the model's performance using the test dataset.

correct = 0 total = 0 with torch.no_grad(): for images, labels in testloader: outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print(f'Accuracy of the model on the 10000 test images: {100 * correct / total}%') 

Conclusion

This is a basic implementation of logistic regression for the MNIST dataset using PyTorch. The model can be improved and extended in various ways, such as by adding regularization, tuning hyperparameters, or using more complex neural network architectures.


More Tags

tcsh docker kendo-ui-angular2 pywinauto apache-commons-httpclient asp.net-apicontroller bufferedimage cpanel nstimeinterval unity-webgl

More Programming Guides

Other Guides

More Programming Examples