Linear Regression in PyTorch: A Beginner’s Guide

Posted by


Linear regression is a commonly used technique in machine learning and statistics for predicting a continuous variable based on one or more input variables. In this tutorial, I will walk you through how to perform linear regression using the popular deep learning framework PyTorch.

Step 1: Install PyTorch
Before we get started, make sure you have PyTorch installed on your system. You can install PyTorch by running the following command in your terminal or command prompt:

pip install torch

Step 2: Import Necessary Libraries
Once PyTorch is installed, you can start by importing the necessary libraries for linear regression. In this tutorial, we will also use the torch.nn and torch.optim modules.

import torch
import torch.nn as nn
import torch.optim as optim

Step 3: Create a Dataset
Next, let’s create a simple synthetic dataset for our linear regression model. We will generate random input data x and corresponding output data y using the following code:

# Generate random input data
x = torch.rand(100, 1)

# Generate corresponding output data
y = 3*x + 2 + torch.randn(100, 1)*0.1

Step 4: Define the Model
Now, let’s define our linear regression model. We will create a custom class called LinearRegression that inherits from nn.Module and implement the __init__ and forward methods:

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

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

Step 5: Instantiate the Model
Next, we will instantiate an instance of our LinearRegression model and define a loss function and optimizer for training the model:

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

Step 6: Train the Model
Now, we can train our linear regression model on the synthetic dataset we created earlier. We will iterate over the dataset for a certain number of epochs and update the model parameters using gradient descent:

num_epochs = 100
for epoch in range(num_epochs):
    optimizer.zero_grad()
    output = model(x)
    loss = criterion(output, y)
    loss.backward()
    optimizer.step()

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

Step 7: Test the Model
Finally, let’s test the trained model on some new input data and see how well it performs in predicting the output:

new_x = torch.tensor([[0.5], [0.7], [1.0]])
predicted_y = model(new_x)
print(predicted_y)

And that’s it! You have successfully performed linear regression in PyTorch. You can further customize and optimize your model by experimenting with different hyperparameters, loss functions, and optimizers. I hope this tutorial was helpful for beginners getting started with linear regression in PyTorch. Happy coding!

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x