Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Linear Regression with PyTorch

Introduction

Linear regression is a fundamental statistical method for modeling the relationship between a dependent variable and one or more independent variables. In this lesson, we will learn how to implement linear regression using PyTorch, a powerful library for deep learning.

Key Concepts

Definitions

  • **Dependent Variable (Target)**: The variable we want to predict.
  • **Independent Variable (Feature)**: The variable(s) used for prediction.
  • **Loss Function**: A method to measure how well the model is performing.
  • **Optimizer**: An algorithm to update the weights of the model.
**Note**: Linear regression assumes a linear relationship between input variables and the output variable.

Step-by-Step Guide

Step 1: Import Libraries


import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
            

Step 2: Prepare Data

We will create synthetic data for demonstration.


# Generating synthetic data
np.random.seed(42)
X = np.random.rand(100, 1) * 10  # Features
y = 2.5 * X + np.random.randn(100, 1) * 2  # Target with noise
            

Step 3: Create a Linear Regression Model


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

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

Step 4: Define Loss Function and Optimizer


model = LinearRegressionModel()
criterion = nn.MSELoss()  # Mean Squared Error Loss
optimizer = optim.SGD(model.parameters(), lr=0.01)  # Stochastic Gradient Descent
            

Step 5: Train the Model


# Training Loop
num_epochs = 100
for epoch in range(num_epochs):
    model.train()
    optimizer.zero_grad()  # Zero the gradients
    outputs = model(torch.from_numpy(X).float())  # Forward pass
    loss = criterion(outputs, torch.from_numpy(y).float())  # Compute loss
    loss.backward()  # Backward pass
    optimizer.step()  # Update weights
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
            

Step 6: Visualize Results


# Visualizing the results
predicted = model(torch.from_numpy(X).float()).detach().numpy()
plt.scatter(X, y, color='blue', label='Actual data')
plt.plot(X, predicted, color='red', label='Fitted line')
plt.legend()
plt.show()
            

Best Practices

  • Always normalize your data before training.
  • Experiment with different learning rates and optimizers.
  • Use a validation set to monitor model performance and avoid overfitting.
  • Visualize your results to better understand model performance.

FAQ

What is linear regression?

Linear regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables by fitting a linear equation to observed data.

Why use PyTorch for linear regression?

PyTorch provides a flexible and efficient framework for building and training machine learning models, making it a great choice for implementing linear regression and other algorithms.

What is the role of the loss function?

The loss function quantifies how well the model's predictions match the actual data, guiding the optimization process to improve the model's accuracy.