Linear regression using PyTorch. Linear regression is a simple machine learning algorithm used for predicting continuous values based on input features. In PyTorch, we can create a linear regression model using the `torch.nn` module. Let's go through the steps:
Step 1: Import the required libraries.
```python
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
```
Step 2: Prepare the data.
For this example, let's create some random data points for demonstration purposes. In practice, you would use your actual dataset.
```python
# Generate some random data for training
np.random.seed(42)
X_train = np.random.rand(100, 1)
y_train = 2 * X_train + 3 + 0.1 * np.random.randn(100, 1)
# Convert data to PyTorch tensors
X_train = torch.tensor(X_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.float32)
```
Step 3: Define the linear regression model.
We will create a simple linear regression model that takes one input feature and produces one output.
```python
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):
return self.linear(x)
```
Step 4: Instantiate the model and define the loss function and optimizer.
```python
# Define the model
input_dim = 1
output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
# Define the loss function (mean squared error)
criterion = nn.MSELoss()
# Define the optimizer (stochastic gradient descent)
learning_rate = 0.01
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
```
Step 5: Train the model.
```python
# Set the number of training epochs
num_epochs = 1000
# Training loop
for epoch in range(num_epochs):
# Forward pass
outputs = model(X_train)
loss = criterion(outputs, y_train)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
# Print the final model parameters
print("Final model parameters:")
for name, param in model.named_parameters():
if param.requires_grad:
print(name, param.data)
```
In this example, we use Mean Squared Error (MSE) as the loss function and Stochastic Gradient Descent (SGD) as the optimizer. You can experiment with different loss functions and optimizers as needed.
After training, the model parameters should approximate the true values of the underlying data generation process: weight=2 and bias=3.
That's it! You've now implemented a simple linear regression model using PyTorch.
No comments:
Post a Comment