I saw two ways of saving the weights of a keras model.
First way;
checkpointer = ModelCheckpoint(filepath="weights.hdf5", verbose=1, save_best_only=True)
model.fit(x_train, y_train,
nb_epoch=number_of_epoch,
batch_size=128,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[reduce_lr, checkpointer],
shuffle=True)
Second way;
model.save_weights("model_weights.h5")
What is the difference between the two ways? Any difference in prediction performance between loading weights.hdf5
and model_weights.h5
?
You only need to save weights and biases to capture the learnable parameters of the model. Keras provides a couple of options to save the weights and biases either during the training of the model or before/after the model training.
keras save model is the process of saving the complete keras model that we have created along with all its components. In this article, we will have a look at the keras save model by studying keras save model overviews, How to use save model keras, saving and loading the model, keras save model explains, method and conclusion about the same.
This also applies to any Keras model: just add a tf.distribute distribution strategy scope enclosing the model building and compiling code, and the training will be distributed according to the tf.distribute distribution strategy.
Note: it is not recommended to use pickle or cPickle to save a Keras model. Whole-model saving means creating a file that will contain: the architecture of the model, allowing you to re-create the model the state of the optimizer, allowing you to resume training exactly where you left off.
No, there is no difference performance-wise. These are just two different ways of how and especially when the model shall be saved. Using model.save_weights
requires to especially call this function whenever you want to save the model, e.g. after the training or parts of the training are done. Using ModelCheckpoint
is much more convenient if you are still developing a model. Using this way, keras
can save a checkpoint of your model after each training epoch, so that you can restore the different models; or you can set save_best_only=True
so that keras
will overwrite the latest checkpoint only if the performance has improved, so that you end with the best performing model.
To summarize it: these are just two different ways of doing two different things. It depends on your use case and needs, what's the best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With