Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ModelCheckpoint with custom metrics in Keras?

Tags:

Is it possible to use custom metrics in the ModelCheckpoint callback?

like image 857
Fábio Perez Avatar asked May 04 '17 12:05

Fábio Perez


People also ask

What is ModelCheckpoint in Keras?

ModelCheckpoint callback is used in conjunction with training using model. fit() to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved.

How do I use callback in Keras?

Using Callbacks in KerasCallbacks can be provided to the fit() function via the “callbacks” argument. First, callbacks must be instantiated. Then, one or more callbacks that you intend to use must be added to a Python list. Finally, the list of callbacks is provided to the callback argument when fitting the model.


1 Answers

Yes, it is possible.

Define the 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 to ModelCheckpoint through monitor. If you want the metric calculated in the validation, use the val_ prefix.

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) 

Don't use mode='auto' for custom metrics. Understand why here.


Why am I answering my own question? Check this.

like image 135
Fábio Perez Avatar answered Oct 18 '22 14:10

Fábio Perez