The loss function that calculates the mean absolute error (mean absolute error - MAE), which is the average sum of the absolute differences between network responses and real labels.
It is applied in regression tasks and is resistant to outliers.
The error function formula is:
N - number of objects in the sample;
y_i - real value for the i-th object;
y_i^p - value predicted by the model for the i-th object.
import numpy as np from PuzzleLib.Backend import gpuarray from PuzzleLib.Cost import Abs
gpuarray is required to properly place the tensor in the GPU.
Synthetic target and prediction tensors:
targets = gpuarray.to_gpu(np.random.randn(10, 10).astype(np.float32)) predictions = gpuarray.to_gpu(np.random.randn(10, 10).astype(np.float32))
Please remember that the first dimension of target and prediction tensors is the size of the batch.
Initializing the error function:
mae = Abs()
Calculating the error and the gradient on the batch:
error, grad = mae(predictions, targets)