Does EarlyStopping in Keras save the best model?

后端 未结 3 347
悲哀的现实
悲哀的现实 2021-01-18 05:48

When using something like:

callbacks = [
    EarlyStopping(patience=15, monitor=\'val_loss\', min_delta=0, mode=\'min\'),
    ModelCheckpoint(\'best-weights.         


        
相关标签:
3条回答
  • 2021-01-18 06:32

    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.

    0 讨论(0)
  • 2021-01-18 06:42

    I would say model uses the latest weights, but I could not find any evidence in the docs. Fortunately you can check the behavior of model by yourself.

    First you run:

    y_pred = model.predict(x_test)
    

    After that, you can load best-weights.h5 and run the prediction on the same test set again.

    If model contains the latest weights, you should get an improved result when loading best-weights.h5. If the results are the same, you can be sure that model automatically uses the best achieved results.

    0 讨论(0)
  • 2021-01-18 06:50

    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)

    0 讨论(0)
提交回复
热议问题