pytorch
  1. pytorch-mean-squared-error

Mean Squared Error - ( Linear Regression with PyTorch )

Heading h2

Syntax

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

# Define model
model = nn.Linear(input_size, output_size)

# Define loss function
criterion = nn.MSELoss()

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

Example

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

# Generate some random data
np.random.seed(42)
x = np.random.rand(100, 1)
y = 3*x + 1 + np.random.rand(100, 1)*0.1

# Convert data to tensors
x_tensor = torch.from_numpy(x).float()
y_tensor = torch.from_numpy(y).float()

# Define model
model = nn.Linear(1, 1)

# Define loss function
criterion = nn.MSELoss()

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

# Train model
for epoch in range(100):
    # Forward pass
    y_pred = model(x_tensor)
    loss = criterion(y_pred, y_tensor)
    
    # Backward pass
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if epoch % 10 == 0:
        print(epoch, loss.item())

# Get predicted values
with torch.no_grad():
    predicted = model(x_tensor).detach().numpy()

Output

0 3.5379176139831543
10 0.04535847887420654
20 0.014303005486190796
30 0.008351530045092106
40 0.006713196508765936
50 0.006295012977868557
60 0.00617232136797905
70 0.006133999142587423
80 0.006122869029492378
90 0.006118050098121166

Explanation

Mean Squared Error (MSE) is a commonly used loss function for regression problems. It measures the average squared difference between the predicted values and the actual values. In other words, it tells us how well our model is predicting the target variable.

In this example, we generate some random data that follows a linear relationship y = 3x + 1 with some added noise. We define a linear model with one input and one output, and use the MSE loss function to train the model.

We iterate over the data for a fixed number of epochs, calculate the loss at each step, and update the model weights using stochastic gradient descent (SGD). We print the loss for every 10th epoch to track the progress of the training.

After training, we use the trained model to make predictions on the input data.

Use

MSE is useful in regression problems, where the goal is to predict a continuous numeric target variable. It provides a measure of how well the model is performing, and can be used to compare the performance of different models.

Important Points

  • MSE is a loss function for regression problems
  • It measures the average squared difference between the predicted and actual values
  • It is commonly used in linear regression problems
  • It provides a measure of how well the model is performing

Summary

In conclusion, Mean Squared Error is a useful loss function for regression problems. It provides a measure of how well the model is performing, and can be used to train and evaluate linear regression models in PyTorch.

Published on: