Custom loss function Tensorflow / Keras penalizing relative distance

457 views Asked by At

I built a tensorflow neural-network with three output classes. My loss function is currently val_mean_absolute_percentage_error as not the absolute distance but the relative distance between target and predicted variable matters. However, the mean absolute percentage error calculated as

1/n sum(|(y_test - y_pred) / y_test|)

is not fully appropriate for my problem as it penalizes stronger if y_pred > y_test

Example 1: y_test = 5, y_pred = 2 --> Mape = 0.6

Example 2: y_pred = 5, y_test = 2 --> Mape = 1.5

However, the above examples should penalize equally.

Does anyone know which (custom) loss function may more suitable for my problem?

1

There are 1 answers

3
Timbus Calin On

For bigger differences between the y_true and y_pred you could consider to use the mse loss instead of mae loss; in case of the RMSE/MSE, as the errors are squared prior to being averaged, the RMSE gives a higher weight for larger errors.

The problem is that you divide to y_test, hence the difference in results. You could first try to remove the denominator to obtain the same MAPE and if you still get bigger values in magnitude between y_test and y_pred, consider using RMSE.