PyTorch MSELoss – Detailed Guide

In this Python tutorial, we will learn about PyTorch MSELoss in Python and we will also cover different examples related to it. And, we will cover these topics.

  • PyTorch MSELoss
  • PyTorch MSELoss code
  • PyTorch MSELoss implementation
  • PyTorch MSELoss weighted
  • PyTorch MSELoss nan
  • PyTorch MSELoss batch size
  • PyTorch MSELoss with regularization

PyTorch MSELoss

In this section, we will learn about how PyTorch MSELoss works in python.

Before moving forward we should have a piece of knowledge about MSELoss.

  • Mse stands for mean square error which is the most commonly used loss function for regression.
  • The loss is the mean supervised data square difference between true and predicted values.
  • PyTorch MSELoss is a process that measures the average of the square difference between actual value and predicted value.

Code:

In the following code, we will import the torch library for which we can measure the mean square error between each element.

  • nn.MSELoss() is used to measure the mean square error.
  • torch.randn(5, 7, requires_grad=True) is used to generate the random numbers.
  • loss(inputs, targets) is used to calculate the loss.
  • print(output) is used to print the output on the screen.
import torch
from torch import nn
loss = nn.MSELoss()
inputs = torch.randn(5, 7, requires_grad=True)
targets = torch.randn(5, 7)
output = loss(inputs, targets)
output.backward()
print(output)

Output:

After running the above code, we get the following output in which we can see that the mean square error loss is printed on the screen.

PyTorch mse loss
PyTorch MSELoss

Read: What is PyTorch and how to use it

PyTorch MSELoss code

In this section, we will learn about the PyTorch MSELoss code in python.

PyTorch MSELoss code is defined as a process to measure the average of the square difference between the actual value and predicted value.

Code:

In the following code, we will import a torch module from which we can calculate the MSELoss.

  • torch.randn(2, 4, requires_grad=True) is used to generate the random numbers.
  • nn.MSELoss() is used to calculate the mean square error.
  • mseloss(inputs, targets) is used to get the output value.
  • print(‘input: ‘, inputs) is used to print the input value.
  • print(‘target: ‘, targets) is used to print the target value.
  • print(‘output: ‘, outputs) is used to print the output value.
import torch.nn as nn

inputs = torch.randn(2, 4, requires_grad=True)
targets = torch.randn(2, 4)
mseloss = nn.MSELoss()
outputs = mseloss(inputs, targets)
outputs.backward()

print('input: ', inputs)
print('target: ', targets)
print('output: ', outputs)

Output:

READ:  Python NumPy 3d array + Examples

In the following output, we can see that the mean square error loss value is printed on the screen.

PyTorch mse loss code
PyTorch MSELoss code

Read: PyTorch Save Model – Complete Guide

PyTorch MSELoss implementation

In this section, we will learn about how to implement the MSELoss in python.

PyTorch MSELoss implementation is done to calculate the average of the square difference between the actual value and the predicted value.

Code:

In the following code, we will import the torch module from which we implement the MSELoss.

  • target = torch.randn(3, 5) is used to generate the random numbers for the target variable.
  • nn.MSELoss() is used to calculate the mean square loss error.
  • mseloss(input, target) is used to calculate the output value.
  • print(‘input_value : ‘, input) is used to print the input variables.
  • print(‘target_value : ‘, target) is used to print the target variables.
  • print(‘output_value : ‘, outputval) is used to print the output value.
import torch 
import torch.nn as nn
target = torch.randn(3, 5)
mseloss = nn.MSELoss()
outputval = mseloss(input, target)
outputval.backward()
print('input_value : ', input)
print('target_value : ', target)
print('output_value : ', outputval)

Output:

After running the above code, we get the following output where we get the average square difference between the input value. And the target value is printed on the screen.

PyTorch mse loss implementation
PyTorch MSELoss implementation

Read: Cross Entropy Loss PyTorch

PyTorch MSELoss Weighted

In this section, we will learn about Pytorch MSELoss weighted in Python.

  • PyTorch MSELoss weighted is defined as the process to calculate the mean of the square difference between the input variable and target variable.
  • The MSELoss is most commonly used for regression and in linear regression, every target variable is evaluated to be a weighted sum of the input variable.

Code:

In the following code, we will import some libraries from which we can calculate the mean of the square difference between the input variable and target variable.

  • torch.from_numpy(input_vals) is used as an input variables.
  • torch.from_numpy(target_vals) is used as an target variables.
  • print(input_vals) is used to print the input variables.
  • print(target_vals) is used to print the target variables.
  • weights = torch.randn(4, 5, requires_grad=True) is used to generate the random weights.
  • biases = torch.randn(4, requires_grad=True) is used to generate the random biases.
  • predictions = model(input_vals) is used to make predictions.
import torch
import numpy as num
input_vals = num.array([[75, 69, 45], 
                   [93, 90, 61], 
                   [89, 136, 70], 
                   [104, 45, 39], 
                   [72, 98, 81]], dtype='float32')
                   

target_vals = num.array([[64, 73], 
                    [93, 104], 
                    [111, 125], 
                    [36, 45], 
                    [110, 113]], dtype='float32')
input_vals = torch.from_numpy(input_vals)
target_vals = torch.from_numpy(target_vals)
print(input_vals)
print(target_vals)
# Weights and biases
weights = torch.randn(4, 5, requires_grad=True)
biases = torch.randn(4, requires_grad=True)
print(w)
print(b)
def model(x):
    return x @ w.t() + b
# Generate predictions
predictions = model(input_vals)
print(predictions)

print(target_vals)
# MSEloss
def mse(t1, t2):
    diff = t1 - t2
    return torch.sum(diff * diff) / diff.numel()

loss = mse(predictions, target_vals)
print(loss)

Output:

READ:  Python Turtle Graphics

After running the above code, we get the following output in which we can see that PyTorch MSELoss weighted is printed on the screen.

PyTorch mse loss weighted
PyTorch MSELoss weighted

Read: Keras Vs PyTorch – Key Differences

PyTorch MSELoss ignore index

In this section, we will learn about the PyTorch MSELoss ignore index in python.

Before moving forward we should have a piece of knowledge about ignore index. Ignore index is a parameter that specifies a target variable that is ignored and does not donate to the input gradients.

Syntax:

torch.nn.MSELoss(size_average=None,reduce=None,ignore_index=None,reduce=None,reduction='mean')

Parameters:

  • size_average is the losses are mean over every loss element in the batch.
  • reduce is the losses are mean and summed over observation for each mini-batch depending upon size_average.
  • ignore_index is a parameter that specifies a target variable that is ignored and does not donate to input gradients.
  • reduction is that specifies the reductions is applied to the output.

Also, check: PyTorch Pretrained Model

PyTorch MSELoss batch size

In this section, we will learn about how to implement PyTorch MSELoss batch size in python.

PyTorch MSELoss batch size is defined as the process in which a number of training examples are used in one iteration.

Code:

In the following code, we will import some libraries from which we can maintain the batch size.

  • torch.from_numpy(input_var) is used as an input variables.
  • torch.from_numpy(target_var) is used as target variables.
  • train_dataset = TensorDataset(input_var, target_var) is used to train dataset.
  • batchsize = 3 is used to define size of batch.
  • train_dl = DataLoader(train_dataset, batchsize, shuffle=True) is used to load the data.
  • nn.Linear(3, 2) is used to create the feed forward network.
  • print(model.weight) is used to printthe model weights.
  • predictions = model(input_var) is used to generate the predictions.
import torch
import torch.nn as nn
import numpy as num

input_var = num.array([[75, 69, 45], [93, 90, 66], [89, 136, 60], 
                   [104, 45, 39], [71, 98, 72], [75, 69, 45], 
                   [93, 90, 66], [89, 136, 60], [104, 45, 39], 
                   [71, 98, 72], [75, 69, 45], [93, 90, 66], 
                   [89, 136, 60], [104, 45, 39], [71, 98, 72]], 
                  dtype='float32')


target_var = num.array([[58, 72], [83, 103], [121, 135], 
                    [24, 39], [105, 121], [58, 72], 
                    [83, 103], [121, 135], [24, 39], 
                    [105, 121], [58, 72], [83, 103], 
                    [121, 135], [24, 39], [105, 121]], 
                   dtype='float32')

input_var = torch.from_numpy(input_var)
target_var = torch.from_numpy(target_var)
input_var
from torch.utils.data import TensorDataset

# Define dataset
train_dataset = TensorDataset(input_var, target_var)
train_dataset[0:3]
from torch.utils.data import DataLoader

# Define data loader
batchsize = 3
train_dl = DataLoader(train_dataset, batchsize, shuffle=True)
for x_b, y_b in train_dl:
    print(x_b)
    print(y_b)
# Define model
model = nn.Linear(3, 2)
print(model.weight)
print(model.bias)
# Parameters
list(model.parameters())
# Generate predictions
predictions = model(input_var)
predictions
import torch.nn.functional as f
# Define loss function
loss_func = f.mse_loss
loss = loss_func(model(input_var), target_var)
print(loss)

Output:

READ:  How to Parse CSV in Python Django

In the following output, we break the large dataset into smaller ones with a batch size of 3, and the PyTorch mse bach size values are printed on the screen.

PyTorch mse loss batch size
PyTorch MSELoss batch size

Read PyTorch Model Eval

PyTorch MSELoss with regularization

In this section, we will learn about PyTorch MSELoss with regularization in python.

PyTorch MSELoss with regularization is a process that puts sum of the absolute values of all the weights in the model.

Code:

In the following code, we will import the torch library from which we can calculate the PyTorch MSELoss with regularization.

  • nn.MSELoss() is used to calculate the mean square error loss.
  • input_var = torch.randn(10, 12, requires_grad=True) is used to generate the random input variables.
  • target_var = torch.randn(10, 12) is used to generate the random target variables.
  • MSE_loss(input_var, target_var) is used to puts the sum of the absolute value of weights.
  • print(“Output: “, output_var) is used to print the output variables.
import torch
import torch.nn as nn
MSE_loss = nn.MSELoss()
input_var = torch.randn(10, 12, requires_grad=True)
target_var = torch.randn(10, 12)
output_var = MSE_loss(input_var, target_var)
output_var.backward()
print("Output: ", output_var)

Output:

After running the above code we get the following output in which we can see that the PyTorch MSELoss regularization variable is printed on the screen.

PyTorch mse loss with regularization
PyTorch MSELoss with regularization

You may like the following PyTorch tutorials:

So, in this tutorial, we discussed PyTorch MSELoss and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • PyTorch MSELoss
  • PyTorch MSELoss code
  • PyTorch MSELoss implementation
  • PyTorch MSELoss weighted
  • PyTorch MSELoss nan
  • PyTorch MSELoss batch size
  • PyTorch MSELoss with regularization