mindspore.nn.MAE

class mindspore.nn.MAE[source]

Calculates the mean absolute error(MAE).

Creates a criterion that measures the MAE between each element in the input: \(x\) and the target: \(y\).

\[\text{MAE} = \frac{\sum_{i=1}^n \|y_i - x_i\|}{n}\]

where \(n\) is batch size.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]), mindspore.float32)
>>> y = Tensor(np.array([0.1, 0.25, 0.7, 0.9]), mindspore.float32)
>>> error = nn.MAE()
>>> error.clear()
>>> error.update(x, y)
>>> result = error.eval()
>>> print(result)
0.037499990314245224
clear()[source]

Clears the internal evaluation result.

eval()[source]

Computes the mean absolute error(MAE).

Returns

numpy.float64. The computed result.

Raises

RuntimeError – If the total number of samples is 0.

update(*inputs)[source]

Updates the internal evaluation result \(y_{pred}\) and \(y\).

Parameters

inputs – Input y_pred and y for calculating MAE where the shape of y_pred and y are both N-D and the shape should be the same.

Raises

ValueError – If the number of the input is not 2.