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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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 = "mae"

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