How do I use ModelCheckpoint with custom metrics in Keras?
1 answer
Yes it is possible.
Define custom metrics as described in the documentation:
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])
To check all available metrics:
print(model.metrics_names)
> ['loss', 'acc', 'mean_pred']
Pass the metric name ModelCheckpoint
through monitor
. If you want the metric to be calculated in validation, use a prefix val_
.
ModelCheckpoint(weights.{epoch:02d}-{val_mean_pred:.2f}.hdf5,
monitor='val_mean_pred',
save_best_only=True,
save_weights_only=True,
mode='max',
period=1)
Do not use mode='auto'
for custom metrics. Understand why here .
Why am I answering my own question? Check this one out .
+6
source to share