pytorch

How to Find the Mean Squared Error in PyTorch?

PyTorch is a free-to-use deep-learning library that helps in building and training neural networks. Mean squared error (MSE) is defined as the average of squared differences between related elements of two tensors. Sometimes, users may need to find/compute the mean squared error between two tensors to see how close the predictions are to the targets. PyTorch provides the “MSELoss()” function to perform this operation.

This blog will exemplify the methods to find the mean squared error in PyTorch.

How to Find/Calculate the Mean Squared Error in PyTorch?

To find the mean squared error in PyTorch, follow the provided steps:

  • Import required libraries
  • Create and print input tensor
  • Create and print target tensor
  • Define a criterion to compute MSE
  • Find/compute the mean squared error
  • Print mean squared error

Go through the following examples for a practical demonstration:

Example 1: Find Mean Squared Error of 1D Tensors in PyTorch

In the first section, we will create 1D input and target tensors and compute their mean squared error. Let’s explore the below steps:

Step 1: Import Required Libraries
First, import the “torch” library and “torch.nn” module that will be used to measure MSE:

import torch
import torch.nn as nn

Step 2: Create Input Tensor
Then, define the input tensor and print its elements. For instance, we are creating a simple 1D tensor from a list “input_tensor” using the “torch.tensor()” function:

input_tensor = torch.tensor([0.50, 0.20, 0.80, 0.70])

print("Input Tensor:", input_tensor)

This has created the input tensor as seen below:

Step 3: Create Target Tensor
After that, create the target tensor and print its elements. For instance, we are creating the following 1D target tensor in the “target_tensor” variable:

target_tensor = torch.tensor([0.03, 0.5, 0.41, 0.82])

print("Target Tensor:", target_tensor)

This has created a target tensor:



Step 4: Define Criterion to Compute MSE

Now, use the “MSELoss()” function to create a criterion for computing mean squared error:

Mean_squ_err = nn.MSELoss()

Step 5: Find/Compute Mean Squared Error
Next, measure the mean squared error by passing the “input_tensor” and “target_tensor”:

output = Mean_squ_err(input_tensor, target_tensor)

Step 6: Print Mean Squared Error
Finally, print the mean squared error that is stored in the “output” variable:

print("Mean Squared Error:", output)

In the below output, the mean squared error can be seen i.e., “0.1194”:

Example 2: Find Mean Squared Error of 2D Tensors in PyTorch

In the second section, we will create 2D input and target tensors and compute their mean squared error. Let’s explore the below steps:

Step 1: Import Required Libraries
First, import the following required libraries:

import torch
import torch.nn as nn

Step 2: Create Input Tensor
Then, create the 2D input tensor and print its elements. For instance, we are creating a 2D tensor with random values using the “torch.randn()” function:

input_tensor = torch.randn(3, 4)

print("Input Tensor:", input_tensor)

This has created the 2D input tensor with random values:

Step 3: Create Target Tensor
Next, create the target tensor and print its elements. For instance, we are creating the following 2D target tensor with random values:

target_tensor = torch.randn(3, 4)

print("Target Tensor:", target_tensor)

This has created the 2D target tensor:

Step 4: Define Criterion to Compute MSE
Now, create a criterion for computing the mean squared error using the “MSELoss()” function:

Mean_squ_err = nn.MSELoss()

Step 5: Find/Compute Mean Squared Error
After that, measure the mean squared error by passing the “input_tensor” and “target_tensor”:

output = Mean_squ_err(input_tensor, target_tensor)

Step 6: Print Mean Squared Error
Lastly, print the mean squared error that is stored in the “output” variable:

print("Mean Squared Error:", output)

The below output shows the mean squared error of 2D tensors i.e., “1.2083”:

We have efficiently explained the methods of finding the mean squared error in PyTorch using different examples.

Note: You can access our Google Colab Notebook at this link.

Conclusion

To find the mean squared error in PyTorch, first, import the necessary torch libraries. Then, create the input and target tensors, and print their elements. After that, use the “MSELoss()” function to create a criterion for computing mean squared error. Next, measure the mean squared error and print it. This blog has exemplified the methods to find the mean squared error in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.