Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EarlyStopping in Keras save the best model?

When using something like:

callbacks = [
    EarlyStopping(patience=15, monitor='val_loss', min_delta=0, mode='min'),
    ModelCheckpoint('best-weights.h5', monitor='val_loss', save_best_only=True, save_weights_only=True)
]

model.fit(..., callbacks=callbacks)

y_pred = model.predict(x_test)

am I doing the prediction with the best weights calculated during training or model is using the last weights (which may not be the best ones)?

So, is the above a safe approach or should I load best-weights.h5 into the model even if the predictions are done right after training?

like image 230
crash Avatar asked Aug 15 '18 11:08

crash


People also ask

How do you save the best model in Keras?

We can use the Keras callback keras. callbacks. ModelCheckpoint() to save the model at its best performing epoch.

Does early stopping save best model?

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.

What is EarlyStopping in Keras?

EarlyStopping classStop training when a monitored metric has stopped improving. Assuming the goal of a training is to minimize the loss. With this, the metric to be monitored would be 'loss' , and mode would be 'min' .

How do you save the best model in TensorFlow?

if save_best_only=True , it only saves when the model is considered the "best" and the latest best model according to the quantity monitored will not be overwritten. If filepath doesn't contain formatting options like {epoch} then filepath will be overwritten by each new better model.


2 Answers

After the training stops by EarlyStopping callback, the current model may not be the best model with the highest/lowest monitored quantity. As a result a new argument, restore_best_weights, has been introduced in Keras 2.2.3 release for EarlyStopping callback if you would like to restore the best weights:

restore_best_weights: whether to restore model weights from the epoch with the best value of the monitored quantity. If False, the model weights obtained at the last step of training are used.

like image 102
today Avatar answered Oct 15 '22 10:10

today


EarlyStopping callback doesn't save anything on its own (you can double check it looking at its source code https://github.com/keras-team/keras/blob/master/keras/callbacks.py#L458). Thus your code saves the last model that achieved the best result on dev set before the training was stopped by the early stopping callback. I would say that, if you are saving only the best model according to dev, it is not useful to have also an early stopping callback (unless you don't want to save time and your are sure enough you are not going to find any better model if you continue the training)

like image 39
Tommaso Pasini Avatar answered Oct 15 '22 10:10

Tommaso Pasini