Keras: How to stop training with the lowest observ

2019-09-22 00:39发布

问题:

With Keras, I would like to stop the training at the epoch which returns the best (in most cases: lowest) observed metric (such as val_loss for example). I would not like to use the state of the network after the patience "ran out".

How can I do that?

回答1:

Well.... you can't really "stop" at the best accuracy, because you need to know the future values to decide if there will be better values!

But you can use another callback, the ModelCheckpoint, to save your model after each epoch.

You can pass the argument save_best_only so the model will only be saved when the monitored value (in your case 'val_loss') is better than the last saved model.

After training, you can load the saved model: keras.models.load_model(filepath)

If you have problems loading a saved model like that, you can try to use save_weights_only=True in the callback. And then you'd load the weights with model.load_weights(filepath).