Search code examples
pythonmachine-learningneural-networkkeras

How to get the best model when using EarlyStopping callback in Keras?


I am training a neural network with Keras using EarlyStopping based on val_acc and patience=0. EarlyStopping stops the training as soon as val_acc decreases.

However the final model that I obtain is not the best model, namely the one with the highest val_acc. But I rather have the model corresponding to the epoch after, namely the one corresponding to a val_acc just a bit lower than the best one and that caused the early stopping!

How do I get the best one?

I tried to use the save the best model using the callback:

ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)

But I get the same results.


Solution

  • If you would like to save the highest accuracy then you should set the checkpoint monitor='val_acc' it will automatically save on highest. Lowest loss might not necessarily correspond to highest accuracy. You can also set verbose=1 to see which model is being saved and why.