How does Keras choose the final model if validation data is given?

Viewed 121

The final training step may not have the lowest loss if the loss fluctuates.

I want to know, if validation is given to training a model in Keras, how does Keras pick the final model?

Does

  1. Keras pick by choosing a model with the lowest loss on validation data from the whole training process?

Or

  1. Keras pick the final model from the final training step, regardless if the final model gives the minimum loss on validation data?

Thank you.

1 Answers

Yes, by default Keras return the final model from the final training step, regardless if the final model gives the minimum loss on validation data.

If you want to use a model with the lowest loss on validation data, you need to use the ModelCheckpoint callback with parameter save_best_only. After training is done, you need to load saved model. It will be the best model in terms of loss on validation data.

save_best_model = ModelCheckpoint(best_model_path, monitor='val_loss', 
                                  save_best_only=True, save_weights_only=True)

# fit model
model.fit(X_train, y_train, 
              batch_size=64,
              epochs=35,
              validation_data=(X_val, y_val), 
              callbacks=[save_best_model])

# select the best model
model.load_weights(best_model_path)
Related