Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Keras model.save() and model.save_weights()?

To save a model in Keras, what are the differences between the output files of:

  1. model.save()
  2. model.save_weights()
  3. ModelCheckpoint() in the callback

The saved file from model.save() is larger than the model from model.save_weights(), but significantly larger than a JSON or Yaml model architecture file. Why is this?

Restating this: Why is size(model.save()) + size(something) = size(model.save_weights()) + size(model.to_json()), what is that "something"?

Would it be more efficient to just model.save_weights() and model.to_json(), and load from these than to just do model.save() and load_model()?

What are the differences?

like image 271
mikal94305 Avatar asked Mar 06 '17 09:03

mikal94305


People also ask

How do you save models in Keras?

There are two formats you can use to save an entire model to disk: the TensorFlow SavedModel format, and the older Keras H5 format. The recommended format is SavedModel. It is the default when you use model.save() .

What is the difference between h5 and HDF5?

Just different file extensions, but otherwise the files will be identical. *. h5 and *. hdf5 are synonymous file extensions.

How do I save and load weights in Keras?

Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.

How do I save the Keras model in TensorFlow?

Using save_weights() method Now you can simply save the weights of all the layers using the save_weights() method. It saves the weights of the layers contained in the model. It is advised to use the save() method to save h5 models instead of save_weights() method for saving a model using tensorflow.


2 Answers

save() saves the weights and the model structure to a single HDF5 file. I believe it also includes things like the optimizer state. Then you can use that HDF5 file with load() to reconstruct the whole model, including weights.

save_weights() only saves the weights to HDF5 and nothing else. You need extra code to reconstruct the model from a JSON file.

like image 55
Dr. Snoopy Avatar answered Nov 02 '22 05:11

Dr. Snoopy


  • model.save_weights(): Will only save the weights so if you need, you are able to apply them on a different architecture
  • mode.save(): Will save the architecture of the model + the the weights + the training configuration + the state of the optimizer
like image 29
Aymen Avatar answered Nov 02 '22 04:11

Aymen