Early stopping and final Loss or weights of models
EarlyStopping

The final weights will be saved, not the weights where your patience parameter is triggered.

Looking at the documentation for EarlyStopping, it seems not to be involved with saving weights at any point - it isn't mentioned.

EDIT

Upon further investigation (reading the source code), it seems you can indeed save the best, using the EarlyStopper callback

The class can be initialised with the aprameters restore_best_weights, as seen here. Then at the end of training, when your waiting period has overshot the patience parameter, the model's weights are returned to be the best weights (weights of the model at the time of loewst validation loss:

if self.restore_best_weights:
    if self.verbose &gt; 0:
        print("Restoring model weights from the end of the best epoch")
    self.model.set_weights(self.best_weights)


It does this by tracking a chosen metric and comparing it to the recorded best value. By default this will be the validation loss. Check out the course code and it's description of the class there.

If these parameters don't work with your code, you will need to upgrade to the latest version (master branch from GitHub). This new parameter was only added 10 days ago!

ModelCheckpoint

If you want to use a callback to prevent overfitting, have a look at the ModelCheckpoint callback class. This has options to save the model weights at given times during training, and will allow you to keep the weights of the model at the end of the epoch specifically where the validation loss was at its minimum. This is selected using the the save_best_only parameter - use it like this:

from keras.callbacks import EarlyStopper, ModelCheckpoint

checkpointer = ModelCheckpoint(filepath, monitor='val_loss', verbose=0,
                               save_best_only=False, save_weights_only=False,
                               mode='auto', period=1)

# your early stopper as before
early_stopper = ...


All callbacks must be placed in a list:

my_callbacks = [checkpointer, early_stopper]

# pass to model along with other parameters as you did already
model.fit(..., callbacks=my_callbacks, ...)


If you don't use save_best_only, the default behaviour is to save the model at the end of every epoch.

You can set the filepath by using certain dynamic variables available during training, so that the filenames have some useful information:

E.g. filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5


  the model checkpoints will be saved with the epoch number and the validation loss in the filename.


Have a look at the linked documentation for how to use the other parameters of ModelCheckpoint.