Branden
Branden

Reputation: 326

Early Stopping, Model has gone through how many epochs?

I am using Keras. I am training my Neural Network and using Early Stopping. My patience is 10 and the epoch with the lowest validation loss is 15. My network runs til 25 epochs and stops however my model is the one with 25 epochs not 15 if I understand correctly

Is there an easy way to revert to the 15 epoch model or do I need to re-instantiate the model and run 15 epochs?

Upvotes: 2

Views: 1863

Answers (3)

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

Yes, there is one, the restore_best_weights parameter in the EarlyStopping callback, set this to True and Keras will keep track of the weights producing the best loss:

callback = EarlyStopping(..., restore_best_weights=True)

See all the parameters for this callback here.

Upvotes: 7

JAbr
JAbr

Reputation: 332

Early Stopping doesn't work the way you are thinking, that it should return the lowest loss or highest accuracy model, it works if there is no improvement in model accuracy or loss, for about x epochs (10 in your case, the patience parameter) then it will stop. you should use callback modelcheckpoint functions instead e.g.

keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1)

https://keras.io/callbacks/

This will save or checkpoint best model encountered during the training history.

Upvotes: 0

mujjiga
mujjiga

Reputation: 16866

Yes, you get the model (weights) corresponding to the epoch when it stops. A commonly used strategy is to save the model whenever the validation loss/acc improves.

Upvotes: 0

Related Questions