Difference between Keras model.save() and model.save_weights()?
Machine LearningTensorflowNeural NetworkKerasMachine Learning Problem Overview
To save a model in Keras, what are the differences between the output files of:
model.save()
model.save_weights()
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?
Machine Learning Solutions
Solution 1 - Machine Learning
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.
Solution 2 - Machine Learning
model.save_weights()
: Will only save the weights so if you need, you are able to apply them on a different architecturemode.save()
: Will save the architecture of the model + the the weights + the training configuration + the state of the optimizer
Solution 3 - Machine Learning
Just to add what ModelCheckPoint's output is, if it's relevant for anyone else: used as a callback during model training, it can either save the whole model or just the weights depending on what state the save_weights_only
argument is set to. TRUE and weights only are saved, akin to calling model.save_weights()
. FALSE (default) and the whole model is saved, as in calling model.save()
.
Solution 4 - Machine Learning
Adding to the answers above, as of tf.keras version '2.7.0', the model can be saved in 2 formats using model.save() i.e., the TensorFlow SavedModel format, and the older Keras H5 format. The recommended format is SavedModel and it is the default when model.save()
is called. To save to .h5(HDF5) format, use model.save('my_model', save_format='h5')
More