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.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:
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:
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:
Step 5: Find/Compute Mean Squared Error
Next, measure the mean squared error by passing the “input_tensor” and “target_tensor”:
Step 6: Print Mean Squared Error
Finally, print the mean squared error that is stored in the “output” variable:
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.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:
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:
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:
Step 5: Find/Compute Mean Squared Error
After that, measure the mean squared error by passing the “input_tensor” and “target_tensor”:
Step 6: Print Mean Squared Error
Lastly, print the mean squared error that is stored in the “output” variable:
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.