3
$\begingroup$

I m training a sequence model in Keras using the tensorflow backend. I've also included some callbacks to save checkpoints and revert to best weights if the model starts to overfit (which it will).

My question - when fitting using this set of callbacks, does the final checkpoint contain the version of the model with the best weight? I know that the weights in classif_model will revert but I'm not sure if that also applies to the final saved state.

from keras import callbacks as kc

classif_model = my_model(input_shape)

# Set up callbacks
checkpointer = kc.ModelCheckpoint(filepath='results/'+name+'.h5', verbose=0)
earlystopping = kc.EarlyStopping(monitor='val_loss', patience=patience, restore_best_weights = True)
callbacks = [checkpointer, earlystopping]

# train the model
hist = classif_model.fit(x = X_tr, y = Y_tr, epochs = epochs, batch_size = batch_size, 
                         callbacks = callbacks, validation_data = (X_val, Y_val), 
                         verbose = 0)
$\endgroup$
2
$\begingroup$

It will if you set the save_best_only flag in your checkpoint callback definition:

ModelCheckpoint(filepath, monitor='val_loss', save_best_only=True)

From the docs:

save_best_only: if save_best_only=True, the latest best model according to the quantity monitored will not be overwritten.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.