7
$\begingroup$

I am new to DL and Keras. I am trying to solve a regression problem with multivariate outputs (y shape (?, 2)) using Keras (tensorflow backend). I am having my confusion about how the loss is calculated. I use mean absolute error as the loss function. However, since my target data has 2 dimensions, is the loss value calculated as the reduced mean on all dimensions (a scalar as the result)? I checked the Keras source code, it uses K.mean(..., axis=-1) for MAE calculation. If K.mean is the same to numpy.mean, "axis=-1" should do the column mean (for my case, it should return a tensor with shape (?,2) but not a scalar). If this is the case, how could the loss value be a single number (as outputed in the training process log)?

If the MAE return is indeed a scalar (reduced mean), this gives me another problem. The data from each dimension of my target is not in a same range. A reduced mean would be biased towards the high value dimension. Shall I change my model to a multi-task learning model then?

Thanks a lot for your help on this.

L.

$\endgroup$

2 Answers 2

1
$\begingroup$

To answer:

1) When calculating the loss function for multivariate outputs, keras calculates it as a mean across all your outputs: https://github.com/keras-team/keras/blob/2.0.4/keras/losses.py#L12

Hence, if one output is doing really badly and others not, it could influence your loss result.

2) In the source code there are no mentioning about scaling the outputs for the calculation of loss function and, thus, I would conclude that the loss function will depend highly on the boundaries of each of your Y features.

To avoid this, what you could do is to write a custom loss function that scales your Y outputs before outputting loss value:

scaler = MinMaxScaler()
data_trans = scaler.fit_transform(y_true)

def custom_mean_absolute_error(y_true, y_pred):
    y_true = (y_true - K.constant(scaler.min_)) / K.constant(scaler.scale_)
    y_pred = (y_pred - K.constant(scaler.min_)) / K.constant(scaler.scale_)
    return K.mean(K.abs(y_pred - y_true), axis=-1)
$\endgroup$
0
$\begingroup$

I fall on same issue with RMSE which by the way may be a good complementary choice of MAE. Thus in order to measure error prediction on multidimentional output the way i implemented was as follow. Measure the index score per dimension then do the average on all dimensions which gives a single scalar value. If your data isn t on same scale, apply a standardization on it. And in order to have same order of magnitude on all your output dimensions you also can divide each column by their corresponding test set standard deviation before applying the mean reduce.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.