Ashwin
Ashwin

Reputation: 333

Saving best Keras model based on mutiple parameters

Is it possible to use the ModelCheckpoint module in Keras with the monitor parameter having more than one option?. I want to save whenever validation accuracy increases. In case two models have the same validation accuracy, I want the one with the best training accuracy to be saved instead. Is this possible? If not, is there a way I can combine two different metrics using a harmonic average?

Upvotes: 0

Views: 63

Answers (1)

nuric
nuric

Reputation: 11225

You can do it with a custom callback. Have a look at what ModelCheckpoint does and something along the lines of:

class MyCheckpoint(Callback):
  # __init__ etc...
  def on_epoch_end(self, epoch, logs=None):
    logs = logs or dict()
    acc = logs.get('acc')
    val_acc = logs.get('val_acc')
    # Your conditions
    if val_acc > self.best_val_acc:
      self.model.save_weights(filename)
      self.best_val_acc = val_acc
    elif val_acc == self.best_val_acc and acc > self.best_acc:
      self.model.save_weights(filename)
      self.best_acc = acc

Upvotes: 1

Related Questions