mrgloom
mrgloom

Reputation: 21622

Keras: EarlyStopping save best model

When I use EarlyStopping callback does Keras save best model in terms of val_loss or it save model on save_epoch = [best epoch in terms of val_loss] + YEARLY_STOPPING_PATIENCE_EPOCHS ?

If it's second option, how to just save best model?

Here is code snippet:

early_stopping = EarlyStopping(monitor='val_loss', patience=YEARLY_STOPPING_PATIENCE_EPOCHS)

history = model.fit_generator(
    train_generator,
    steps_per_epoch=100, # 1 epoch = BATCH_SIZE * steps_per_epoch samples
    epochs=N_EPOCHS,
    validation_data=test_generator,
    validation_steps=20,
    callbacks=[early_stopping])

#Save train log to .csv
pd.DataFrame(history.history).to_csv('vgg16_binary_crossentropy_train_log.csv', index=False)

model.save('vgg16_binary_crossentropy.h5')

Upvotes: 2

Views: 4058

Answers (2)

Justas
Justas

Reputation: 5942

In v2.2.4+ of Keras, EarlyStopping has a restore_best_weights parameter which, when set to True, will set the model to the state of best CV performance. For example:

EarlyStopping(restore_best_weights=True)

Upvotes: 3

user1269942
user1269942

Reputation: 3852

From my experience using the 'earlystopping' callback, the model will not be saved automatically...it will just stop training and when you save it manually, it will be the second option you present. To have your model save each time val_loss decreases, see the following documentation page: https://keras.io/callbacks/ and look at the "Example: model checkpoints" section which will tell you exactly what to do.

note that if you wish to re-use your saved model, I have had better luck using 'save_weights' in combo with saving the architecture in json. YMMV.

Upvotes: 2

Related Questions