Sling Academy
Home/PyTorch/An End-to-End Guide to PyTorch Linear Regression

An End-to-End Guide to PyTorch Linear Regression

Last updated: December 14, 2024

Linear regression is one of the simplest yet most powerful techniques in machine learning. In this guide, we walk through building a linear regression model using PyTorch, a popular deep learning library. We'll cover essential steps including data preparation, model creation, loss calculation, optimization, and evaluation.

Setting Up PyTorch

Before starting, ensure PyTorch is properly installed in your environment. You can install it using pip:

pip install torch torchvision

Import Libraries

To begin, let's import necessary libraries including PyTorch and others for data manipulation:

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

Creating and Loading Data

For simplicity, assume we create a synthetic dataset representing a linear relationship:

# Create synthetic data
np.random.seed(42)
x = np.linspace(0, 10, 100)
y = 2.5 * x + np.random.normal(0, 1, 100)

# Convert to PyTorch tensors
x_train = torch.tensor(x, dtype=torch.float32).unsqueeze(1)
y_train = torch.tensor(y, dtype=torch.float32).unsqueeze(1)

Define the Linear Regression Model

We’ll create a simple linear regression model in PyTorch:

class LinearRegressionModel(nn.Module):
    def __init__(self):
        super(LinearRegressionModel, self).__init__()
        self.linear = nn.Linear(1, 1)  # Single input and output
    
    def forward(self, x):
        return self.linear(x)

This defines our model with a single input and a single output feature.

Initialize the Model

Next, we initialize the model, define a loss function, and choose an optimizer:

model = LinearRegressionModel()
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

Training the Model

We can now start training. The process involves multiple epochs where the model weights are optimized to minimize the loss:

epochs = 1000
for epoch in range(epochs):
    # Forward pass
    predictions = model(x_train)
    
    # Compute loss
    loss = criterion(predictions, y_train)
    
    # Backward pass and optimization
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    # Print statistics
    if (epoch+1) % 100 == 0:
        print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')

Ensure that you track the loss over epochs to validate the model's learning process.

Model Evaluation

Once training is complete, it’s crucial to evaluate the model to ensure it has learned correctly. Plotting the predicted vs actual data is a useful visualization:

# Detach operation is necessary to remove gradient tracking
predicted = model(x_train).detach().numpy()

plt.plot(x, y, 'ro', label='Original data')
plt.plot(x, predicted, label='Fitted line')
plt.legend()
plt.show()

This plot should show the original data points and the line of best fit through them.

Conclusions and Next Steps

Through this process, we have built and trained a linear regression model using PyTorch. From here, you can explore extending this model by using polynomial terms or more features in the dataset. You could also compare to other models in PyTorch like logistic regression if classification tasks are of interest.

This guide serves as a foundation upon which more complex models and techniques using PyTorch can be built.

Next Article: Putting Together Everything You Learned in PyTorch

Previous Article: Combining Data Preparation, Model Training, and Prediction in PyTorch

Series: The First Steps with PyTorch

PyTorch

You May Also Like

  • Addressing "UserWarning: floor_divide is deprecated, and will be removed in a future version" in PyTorch Tensor Arithmetic
  • In-Depth: Convolutional Neural Networks (CNNs) for PyTorch Image Classification
  • Implementing Ensemble Classification Methods with PyTorch
  • Using Quantization-Aware Training in PyTorch to Achieve Efficient Deployment
  • Accelerating Cloud Deployments by Exporting PyTorch Models to ONNX
  • Automated Model Compression in PyTorch with Distiller Framework
  • Transforming PyTorch Models into Edge-Optimized Formats using TVM
  • Deploying PyTorch Models to AWS Lambda for Serverless Inference
  • Scaling Up Production Systems with PyTorch Distributed Model Serving
  • Applying Structured Pruning Techniques in PyTorch to Shrink Overparameterized Models
  • Integrating PyTorch with TensorRT for High-Performance Model Serving
  • Leveraging Neural Architecture Search and PyTorch for Compact Model Design
  • Building End-to-End Model Deployment Pipelines with PyTorch and Docker
  • Implementing Mixed Precision Training in PyTorch to Reduce Memory Footprint
  • Converting PyTorch Models to TorchScript for Production Environments
  • Deploying PyTorch Models to iOS and Android for Real-Time Applications
  • Combining Pruning and Quantization in PyTorch for Extreme Model Compression
  • Using PyTorch’s Dynamic Quantization to Speed Up Transformer Inference
  • Applying Post-Training Quantization in PyTorch for Edge Device Efficiency