21

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?

2
  • You tell us -- did it have an impact?
    – erip
    Aug 20, 2018 at 1:13
  • @erip, I just discovered first method always save the best weights. No guarantee for the second method. Aug 20, 2018 at 3:45

2 Answers 2

26

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.

3
  • 3
    Thanks for your answer. Upvoted. I think there is a key difference. Using ModelCheckpoint with save_best_only=True, one is guaranteed to save the best weights. No guarantee for the other method. So, save_best_only=True is always better. I cannot think of any other reason not to use it all the time. Aug 20, 2018 at 3:45
  • 1
    @FlashTek, can you also tell us the difference between hdf5 and h5? Both are saving the model with weight, correct? Thanks Jun 18, 2019 at 8:36
  • 6
    @FloridaMan It's the same. Just different file extensions, but otherwise the files will be identical. *.h5 and *.hdf5 are synonymous file extensions.
    – zimmerrol
    Jun 18, 2019 at 8:52
6

HDF5 (.h5, .hdf5)

As fundamentally explained here reference.wolfram.com/language/ref/format/HDF5.html

HDF is an acronym for Hierarchical Data Format. HDF5 is HDF Version 5.

Import["file.h5"] 

imports an HDF5 file, returning the names of the datasets stored in the file.

In Keras documents; as you can see here keras.io/api/models/model_saving_apis/

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'

In Tensorflow documents; as you can see here tensorflow.org/tutorials/keras/save_and_load

# Save the entire model to a HDF5 file.
# The '.h5' extension indicates that the model should be saved to HDF5.
model.save('my_model.h5')

At fundamental level they are same thing. They cant create any difference.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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