0
$\begingroup$

I am trying to understand what happens here when I use the Keras ModelCheckpoint callback without setting either save_best_only & save_weights_only to True.

i.e.

from keras.callbacks import ModelCheckpoint
mc = ModelCheckpoint(filepath = './checkpoint/to/save.h5')

According to Tensorflow, both save weights and save best are both set to False by default.

tf.keras.callbacks.ModelCheckpoint(
    filepath, monitor='val_loss', verbose=0, save_best_only=False,
    save_weights_only=False, mode='auto', save_freq='epoch',
    options=None, **kwargs
)

So when none are initialized to True, what gets saved?

$\endgroup$
0
$\begingroup$

EDIT: I don't believe I really answered your question. Setting save_best_only to False is supposed to let the model save after every specified epoch - this does not currently work. Save_weights_only means it only saves the weights and not the full model. You would have to first define the model then load the weights if you do this. If False, you could load the model with having to redefine it.

Yes, I believe this is a bug with Tensorflow. There was an open issue about this on the GitHub repo for Tensorflow, but I don't remember the link to the page. Effectively, is you do as you said, Tensorflow will still only save the model if there is an improvement in neural networks performance. Furthermore, passing "epochs" selects how many times the neural network's performance needs to improve before saving the weights.

One theoretical work around is to create your own Callback to save after every n epoch. It is not difficult to do. Here is the documentation.

Some really rough psuedo code:

class SaveAtEpoch(keras.callbacks.Callback):
      def __init__(self, save_frequency,filepath):
      super(SaveAtEpoch,self).__init__
      self.save_frequency = save_frequency
      self.filepath = filepath
     
     def on_epoch_end(self, epoch, logs=None):
     if epoch%self.frequency == 0:
        self.model.save(self.filepath)



      
$\endgroup$
0
$\begingroup$

Keras should be accessed as tf.keras now with tf2 ,so your import should be written as

tf.keras.callbacks.ModelCheckpoint

Keras own documentation as well as tf api documentation can be easily accessed for this purpose. Keras ModelCheckpoint class mentions the following arguments in official docs:

save_best_only: if save_best_only=True, it only saves when the model is considered the "best" and the latest best model according to the quantity monitored will not be overwritten. If filepath doesn't contain formatting options like {epoch} then filepath will be overwritten by each new better model.

save_weights_only: if True, then only the model's weights will be saved (model.save_weights(filepath)), else the full model is saved (model.save(filepath)).

So both 'save_best_only and save_weights_only' have default value as False and will save all weights and full model if not True.

$\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.