Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to measure the mean absolute error (MAE) in PyTorch?

How to measure the mean absolute error (MAE) in PyTorch?

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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments