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?
We can use the Keras callback keras. callbacks. ModelCheckpoint() to save the model at its best performing epoch.
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.
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' .
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With