pytorch
  1. pytorch-training

Training - ( Linear Regression with PyTorch )

Heading h2

Syntax

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

# Define model
class LinearRegressionModel(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(LinearRegressionModel, self).__init__()
        self.linear = nn.Linear(input_dim, output_dim)

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

# Define loss function
criterion = nn.MSELoss()

# Define optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

Example

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

# Define hyperparameters
learning_rate = 0.01
epochs = 1000

# Define training data
x_train = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]], dtype=np.float32)
y_train = np.array([[2], [4], [6], [8], [10], [12], [14], [16], [18], [20]], dtype=np.float32)

# Define input and output dimensions
input_dim = 1
output_dim = 1

# Define model
class LinearRegressionModel(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(LinearRegressionModel, self).__init__()
        self.linear = nn.Linear(input_dim, output_dim)

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

model = LinearRegressionModel(input_dim, output_dim)

# Define loss function
criterion = nn.MSELoss()

# Define optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# Train model
for epoch in range(epochs):
    # Convert inputs and labels to tensors
    inputs = torch.from_numpy(x_train)
    labels = torch.from_numpy(y_train)

    # Clear gradients
    optimizer.zero_grad()

    # Forward propagation
    outputs = model(inputs)

    # Calculate loss
    loss = criterion(outputs, labels)

    # Backward propagation
    loss.backward()

    # Update weights
    optimizer.step()

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

# Test model
x_test = np.array([[11], [12], [13], [14], [15]], dtype=np.float32)
with torch.no_grad():
    inputs = torch.from_numpy(x_test)
    outputs = model(inputs)
    print(outputs)

Output

Epoch [100/1000], Loss: 0.0059
Epoch [200/1000], Loss: 0.0035
Epoch [300/1000], Loss: 0.0021
Epoch [400/1000], Loss: 0.0013
Epoch [500/1000], Loss: 0.0008
Epoch [600/1000], Loss: 0.0005
Epoch [700/1000], Loss: 0.0003
Epoch [800/1000], Loss: 0.0002
Epoch [900/1000], Loss: 0.0001
Epoch [1000/1000], Loss: 0.0001

tensor([[22.0149],
        [24.0545],
        [26.0942],
        [28.1339],
        [30.1736]])

Explanation

Linear Regression is a method of finding the relationship between an independent variable and a dependent variable. In PyTorch, we can define a model using the nn.Module class and train it using the nn.MSELoss and optimizer classes. During training, we forward propagate the inputs through the model and calculate the loss using the Mean Squared Error (MSE) loss function. We then use the backward() function to calculate the gradients and update the model parameters using the optimizer.

Use

Linear Regression with PyTorch can be used for predicting continuous variables such as stock prices or housing prices. It can also be used for simple classification problems where the classes are linearly separable.

Important Points

  • nn.Module is used to define a model in PyTorch
  • nn.MSELoss is a loss function used for regression tasks
  • The optimizer class is used to update the model parameters during training
  • The training data must be converted to tensors before training

Summary

In summary, Linear Regression with PyTorch is a simple yet powerful tool for predicting continuous variables and solving simple classification problems. By defining a model using nn.Module and training it using nn.MSELoss and the optimizer, we can effectively learn the relationship between the input and output variables and make accurate predictions.

Published on: