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

mikal94305 picture mikal94305 · Mar 6, 2017 · Viewed 19.1k times · Source

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?

Answer

Dr. Snoopy picture Dr. Snoopy · Mar 6, 2017

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.