How to measure the mean absolute error (MAE) in PyTorch?
Last Updated :
09 Oct, 2022
In this article, we are going to see how to measure the Mean Absolute Error (MAE) in PyTorch.
\text{MAE} = \sum\limits_{i = 1}^n {\left| {{y_i} - \widehat {{y_i}}} \right|}
The Mean absolute error (MAE) is computed as the mean of the sum of absolute differences between the input and target values. This is an objective function in many of the machine learning algorithms used for regression tasks where we try to minimize the value of this error.
Both the input and target values are torch tensors having the same number of elements. The L1Loss() method measures the mean absolute error and creates a criterion that measures the mean absolute error. This method return tensor of a scalar value. This return tensor is a type of loss function provided by the torch.nn module. Before moving further let's see the syntax of the given method.
Syntax: torch.nn.L1Loss(input_tensor, output_tensor)
Parameters:
- input_tensor: input matrix
- output_tensor: Output of some algorithm for the data
Return: This method return tensor of a scalar value
Example 1:
In this example, we measure the mean absolute error between a 1-D tensor and a target tensor.
Python3
# Import required libraries
import torch
import torch.nn as nn
# create input tensors
input_tensor = torch.tensor([1.4725, -0.4241, -0.3799, 0.3451])
# create target tensors
target_tensor = torch.tensor([1.3913, -0.4572, -0.2346, 1.4708])
# print above created tensors
print("\n Input tensor: \n", input_tensor)
print("\n Target tensor: \n", target_tensor)
# use L1Loss() method to create a criterion
# to measure the mean absolute error.
MAE = nn.L1Loss()
# compute the mean absolute error
output_tensor = MAE(input_tensor, target_tensor)
# print result
print("\n MAE loss: ", output_tensor)
Output:
Input tensor:
tensor([ 1.4725, -0.4241, -0.3799, 0.3451])
Target tensor:
tensor([ 1.3913, -0.4572, -0.2346, 1.4708])
MAE loss: tensor(0.3463)
Example 2:
In this example, we measure the mean absolute error between a 2D tensor and a target tensor.
Python3
# Import required libraries
import torch
import torch.nn as nn
# create input tensors
input_tensor = torch.tensor([[-1.4576, 0.6496, 0.6783],
[0.4895, 1.9454, -0.5443],
[1.9491, -0.3825, 0.7235]])
# create target tensors
target_tensor = torch.tensor([[0.2432, -0.1579, -1.0325],
[-1.3464, 1.2442, 1.3847],
[0.4528, 0.0876, 0.0499]])
# print above created tensors
print("\n Input tensor: \n", input_tensor)
print("\n Target tensor: \n", target_tensor)
# use L1Loss() method to create a criterion
# to measure the mean absolute error.
MAE = nn.L1Loss()
# compute the mean absolute error
output_tensor = MAE(input_tensor, target_tensor)
# print result
print("\n MAE loss: ", output_tensor)
Output:
Input tensor:
tensor([[-1.4576, 0.6496, 0.6783],
[ 0.4895, 1.9454, -0.5443],
[ 1.9491, -0.3825, 0.7235]])
Target tensor:
tensor([[ 0.2432, -0.1579, -1.0325],
[-1.3464, 1.2442, 1.3847],
[ 0.4528, 0.0876, 0.0499]])
MAE loss: tensor(1.2584)
Example 3:
In this example, we measure the mean absolute error loss (MAE) between a 2D input tensor and a target tensor.
Python3
# Import required libraries
import torch
import torch.nn as nn
# create input tensors
input_tensor = torch.tensor([[-0.3272, 1.7495, -0.6783],
[0.4894, 0.4455, 1.5443],
[0.3493, 1.3825, -0.7235], ])
# create target tensors
target_tensor = torch.tensor([[-0.4431, 1.7679, -1.0325],
[-1.3464, 1.2442, 0.3847],
[1.1528, 0.0876, 0.0499], ])
# print above created tensors
print("\n Input tensor: \n", input_tensor)
print("\n Target tensor: \n", target_tensor)
# use L1Loss() method to create a criterion
# to measure the mean absolute error.
MAE = nn.L1Loss()
# compute the mean absolute error
output_tensor = MAE(input_tensor, target_tensor)
# print result
print("\n MAE loss: ", output_tensor)
Output:
Input tensor:
tensor([[-0.3272, 1.7495, -0.6783],
[ 0.4894, 0.4455, 1.5443],
[ 0.3493, 1.3825, -0.7235]])
Target tensor:
tensor([[-0.4431, 1.7679, -1.0325],
[-1.3464, 1.2442, 0.3847],
[ 1.1528, 0.0876, 0.0499]])
MAE loss: tensor(0.7949)
Similar Reads
How to Calculate Mean Absolute Error in Python?
When building machine learning models, our aim is to make predictions as accurately as possible. However, not all models are perfect some predictions will surely deviate from the actual values. To evaluate how well a model performs, we rely on error metrics. One widely used metric for measuring pred
4 min read
How to Compute the Pseudoinverse of a Matrix in PyTorch
In this article, we are going to discuss how to compute the pseudoinverse of a matrix in Python using PyTorch. torch.linalg.pinv() method torch.linalg.pinv() method accepts a matrix and a batch of matrices as input and returns a new tensor with the pseudoinverse of the input matrix. if the input is
2 min read
How to adjust the contrast of an image in PyTorch
In this article, we are going to see how to adjust the contrast of an image in PyTorch using Python. We can adjust the contrast of an image by using the adjust_contrast() method. adjust_contrast() method adjust_contrast() method accepts the PIL and tensor images as input. tensor image is a tensor wi
2 min read
How to Find Mean Across the Image Channels in PyTorch?
In this article, we are going to see how to find mean across the image channels in PyTorch. We have to compute the mean of an image across the channels Red, Green, and, Blue. we can find the mean across the image channel by using torch.mean() method. torch.mean() method torch.mean() method is used t
2 min read
How to Make a grid of Images in PyTorch?
In this article, we are going to see How to Make a grid of Images in PyTorch. we can make a grid of images using the make_grid() function of torchvision.utils package. make_grid() function: The make_grid() function accept 4D tensor with [B, C ,H ,W] shape. where B represents the batch size, C repres
3 min read
How to compute the inverse of a square matrix in PyTorch
In this article, we are going to cover how to compute the inverse of a square matrix in PyTorch. torch.linalg.inv() method we can compute the inverse of the matrix by using torch.linalg.inv() method. It accepts a square matrix and a batch of the square matrices as input. If the input is a batch of
2 min read
How to get the rank of a matrix in PyTorch
In this article, we are going to discuss how to get the rank of a matrix in PyTorch. we can get the rank of a matrix by using torch.linalg.matrix_rank() method.torch.linalg.matrix_rank() methodmatrix_rank() method accepts a matrix and a batch of matrices as the input. This method returns a new tenso
2 min read
How to compute the inverse hyperbolic sine in PyTorch?
In this article, we are going to discuss how to compute the inverse hyperbolic sine in PyTorch. torch.asinh() method: The torch.asinh() method is used to compute the inverse hyperbolic sine of each element present in a given input tensor. This method accepts both real and complex-valued as input. I
3 min read
How to calculate the F1 score and other custom metrics in PyTorch?
Evaluating deep learning models goes beyond just training them; it means rigorously checking their performance to ensure they're accurate, reliable, and efficient for real-world use. This evaluation is critical because it tells us how well a model has learned and how effective it might be in real-li
7 min read
How to access the metadata of a tensor in PyTorch?
In this article, we are going to see how to access the metadata of a tensor in PyTorch using Python. PyTorch in Python is a machine learning library. Also, it is free and open-source. It was firstly introduced by the Facebook AI research team. A tensor in PyTorch is similar to a NumPy array. But it
3 min read