Tutorial 08: Understanding Logistic Regression with PyTorch

Posted by


In this PyTorch tutorial 08, we will be focusing on implementing logistic regression. Logistic regression is a classification algorithm that is used to predict the probability of a categorical dependent variable. It is considered a basic building block in machine learning and is often used as a baseline model for more complex algorithms.

Let’s start by importing the necessary libraries:

import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np

Next, let’s create a sample dataset for our logistic regression model. For this tutorial, we will use randomly generated data.

# Generate random data
np.random.seed(42)
X = np.random.rand(100, 1)
y = np.random.randint(0, 2, (100, 1))

# Convert data to PyTorch tensors
X_tensor = torch.tensor(X, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32)

Now, let’s define our logistic regression model using PyTorch’s nn.Module class.

class LogisticRegression(nn.Module):
    def __init__(self):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(1, 1)

    def forward(self, x):
        return torch.sigmoid(self.linear(x))

We have created a simple logistic regression model with one input feature and one output. The forward method applies a sigmoid activation function to the output of the linear layer to get the final output between 0 and 1.

Next, let’s define the loss function and optimizer for our model.

model = LogisticRegression()
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

Now, let’s train the model on our dataset.

num_epochs = 100

for epoch in range(num_epochs):
    # Forward pass
    outputs = model(X_tensor)
    loss = criterion(outputs, y_tensor)

    # Backward pass and optimization
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if (epoch + 1) % 10 == 0:
        print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item()}')

In this training loop, we compute the model’s output, calculate the loss using the binary cross-entropy loss function, perform backpropagation to compute the gradients, and update the model parameters using the optimizer.

Finally, we can make predictions using the trained model.

with torch.no_grad():
    test_data = torch.tensor([[0.2], [0.6], [0.8]], dtype=torch.float32)
    predictions = model(test_data)
    print(predictions.round().squeeze())

In this tutorial, we have implemented logistic regression using PyTorch. Logistic regression is a simple yet powerful algorithm for binary classification tasks. You can further extend this tutorial by experimenting with different hyperparameters, using different datasets, or adding more complexity to the model architecture.