Skip to content

declearn.metrics.MeanAbsoluteError

Bases: MeanMetric

Mean Absolute Error (MAE) metric.

This metric applies to a regression model, and computes the (opt. weighted) mean sample-wise absolute error. Note that for inputs with multiple channels, the sum of absolute channel-wise errors is computed for each sample, and averaged across samples.

Computed metric is the following:

  • mae: float Mean absolute error, averaged across samples (possibly summed over channels for (>=2)-dimensional inputs).
Source code in declearn/metrics/_mean.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class MeanAbsoluteError(MeanMetric):
    """Mean Absolute Error (MAE) metric.

    This metric applies to a regression model, and computes the (opt.
    weighted) mean sample-wise absolute error. Note that for inputs
    with multiple channels, the sum of absolute channel-wise errors
    is computed for each sample, and averaged across samples.

    Computed metric is the following:

    * mae: float
        Mean absolute error, averaged across samples (possibly
        summed over channels for (>=2)-dimensional inputs).
    """

    name: ClassVar[str] = "mae"

    def metric_func(
        self,
        y_true: np.ndarray,
        y_pred: np.ndarray,
    ) -> np.ndarray:
        # Sample-wise (sum of) absolute error function.
        errors = np.abs(y_true - y_pred)
        while errors.ndim > 1:
            errors = errors.sum(axis=-1)
        return errors