I want to get a model which works best, what should I go for while training the model, ModelCheckpoint, EarlyStopping, or both?
1 Answer
- 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 parameterrestore_best_weights
, but this is just about restoring the weights of your final neural network (if I remember correctly). $\endgroup$– nbroSep 13, 2021 at 16:24 -
$\begingroup$ ah that's correct! I have edited my answer $\endgroup$ Sep 13, 2021 at 16:34
-
1$\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$ Sep 13, 2021 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$– SanyouSep 13, 2021 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$– nbroSep 13, 2021 at 18:48