|TensorFlow 1 version||View source on GitHub|
Saves a model as a TensorFlow SavedModel or HDF5 file.
Compat aliases for migration
See Migration guide for more details.
tf.keras.models.save_model( model, filepath, overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None, save_traces=True )
Used in the notebooks
|Used in the guide||Used in the tutorials|
See the Serialization and Saving guide for details.
model = tf.keras.Sequential([
loaded_model = tf.keras.models.load_model('/tmp/model')
x = tf.random.uniform((10, 3))
assert np.allclose(model.predict(x), loaded_model.predict(x))
The SavedModel and HDF5 file contains:
- the model's configuration (topology)
- the model's weights
- the model's optimizer's state (if any)
Thus models can be reinstantiated in the exact same state, without any of the code used for model definition or training.
Note that the model weights may have different scoped names after being
loaded. Scoped names include the model/layer names, such as
"dense_1/kernel:0". It is recommended that you use the layer properties to
access specific variables, e.g.
SavedModel serialization format
Keras SavedModel uses
tf.saved_model.save to save the model and all
trackable objects attached to the model (e.g. layers and variables). The model
config, weights, and optimizer are saved in the SavedModel. Additionally, for
every Keras layer attached to the model, the SavedModel stores:
- the config and metadata -- e.g. name, dtype, trainable status
- traced call and loss functions, which are stored as TensorFlow subgraphs.
The traced functions allow the SavedModel format to save and load custom layers without the original class definition.
You can choose to not save the traced functions by disabling the
option. This will decrease the time it takes to save the model and the
amount of disk space occupied by the output SavedModel. If you enable this
option, then you must provide all custom class definitions when loading
the model. See the
custom_objects argument in
||Keras model instance to be saved.|
One of the following:
||Whether we should overwrite any existing model at the target location, or instead ask the user with a manual prompt.|
||If True, save optimizer's state together.|
||Either 'tf' or 'h5', indicating whether to save the model to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X, and 'h5' in TF 1.X.|
Signatures to save with the SavedModel. Applicable to the 'tf'
format only. Please see the
(only applies to SavedModel format) |