I want to get a model which works best, what should I go for while training the model, ModelCheckpoint, EarlyStopping, or both?

  • Early stopping: stop the training when a condition is met
  • Checkpoint : frequently save the model

The purpose of Early Stopping is to avoid overfitting by stopping the model before it happens using a defined condition. If you use it, and then you save the model when the training is stopped*, you will get a model that is assumed to be good enough and not overfitted.

The purpose of the class ModelCheckpoint is to save models several times while training. This can be useful to find at which epoch the model gets the best performance. So, if you use it, you will get several models that are saved at different epochs (or, more generally, "checkpoints").

Even after using both methods, you will get some models, but they have a different purpose. None of them is better than the other. I almost always use both methods at the same time. You can use early stopping to stop the training and save a lot of models while training using ModelCheckpoint. In most of my cases, the best model is around the epoch during early stopping.

*note: the model saving process is not done by EarlyStopping

  • $\begingroup$ What do you mean by "Even both methods "save the model,". As far as I remember, Early stopping does not save any model automatically. The EarlyStopping class has a parameter restore_best_weights, but this is just about restoring the weights of your final neural network (if I remember correctly). $\endgroup$
    – nbro
    Sep 13 at 16:24
  • $\begingroup$ ah that's correct! I have edited my answer $\endgroup$
    – malioboro
    Sep 13 at 16:34
  • $\begingroup$ I try to highlight "save the model" part because in this part both methods may look similar (and I think that's the reason OP is confuse) $\endgroup$
    – malioboro
    Sep 13 at 16:37
  • 1
    $\begingroup$ Maybe it's obvious but other (very important) checkpointing's purpose is to safeguard the training, especially if the training takes very long time and is being done on a (campus/company/shared) server that have risks of stopping the training (hardware issues, electrical issues, etc) we can still continue our training from the last checkpoint. $\endgroup$
    – Sanyou
    Sep 13 at 17:00
  • 1
    $\begingroup$ @Sanyou Yes, but you need to load the weights manually from the checkpoint, at least, as far as I remember, that's how keras works, i.e. it doesn't automatically restart training from the last saved checkpoint, unless you manually tell keras to do that. Feel free to edit this answer to add that detail or feel free to add another formal answer, because I think this "detail" is important, you also think. $\endgroup$
    – nbro
    Sep 13 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.