I am trying to add percent mean absolute error (pmae) as a custom metric in keras. This is defined as (MAE divided by the mean absolute y-value multiplied by 100). I have tried:
def pmae(y_true,y_pred):
return K.mean(K.abs(y_pred - y_true)) / K.mean(K.abs(y_true)) * 100
...
model.compile(loss='mse', optimizer=Adam(),metrics=[pmae])
which runs, but the value is many orders of magnitude off (when I look at model.history.history.pmae
)
The working numpy version (on a test sample) is:
y_pred = model.predict(X_test)
pmae = abs(y_pred - y_test).mean() / abs(y_true).mean() * 100
I've also tried adding , axis=-1
to the K.mean()
calls, with no improvement (as suggested in other stackoverflow answers). Does anyone know what's wrong?
Resources
- The keras website gives mean value of y as an example:
import keras.backend as K
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
- Others have answered on stackoverflow about other custom metrics (eg Keras custom RMSLE metric and how to implement custom metric in keras?), but the responses there haven't helped me with pmae calculation.