tf.keras.models.save_model
Stay organized with collections
Save and categorize content based on your preferences.
Saves a model as a TensorFlow SavedModel or HDF5 file.
tf.keras.models.save_model(
model, filepath, overwrite=True, include_optimizer=True, save_format=None,
signatures=None, options=None
)
The saved model contains:
- the model's configuration (topology)
- the model's weights
- the model's optimizer's state (if any)
Thus the saved model can be reinstantiated in
the exact same state, without any of the code
used for model definition or training.
SavedModel serialization (not yet added)
The SavedModel serialization path uses tf.saved_model.save
to save the model
and all trackable objects attached to the model (e.g. layers and variables).
@tf.function
-decorated methods are also saved. Additional trackable objects
and functions are added to the SavedModel to allow the model to be
loaded back as a Keras Model object.
Arguments |
model
|
Keras model instance to be saved.
|
filepath
|
One of the following:
- String, path where to save the model
h5py.File object where to save the model
|
overwrite
|
Whether we should overwrite any existing model at the target
location, or instead ask the user with a manual prompt.
|
include_optimizer
|
If True, save optimizer's state together.
|
save_format
|
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
|
Signatures to save with the SavedModel. Applicable to the 'tf'
format only. Please see the signatures argument in
tf.saved_model.save for details.
|
options
|
Optional tf.saved_model.SaveOptions object that specifies
options for saving to SavedModel.
|
Raises |
ImportError
|
If save format is hdf5, and h5py is not available.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.models.save_model\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/models/save_model) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/saving/save.py#L46-L115) |\n\nSaves a model as a TensorFlow SavedModel or HDF5 file.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.models.save_model`](/api_docs/python/tf/keras/models/save_model)\n\n\u003cbr /\u003e\n\n tf.keras.models.save_model(\n model, filepath, overwrite=True, include_optimizer=True, save_format=None,\n signatures=None, options=None\n )\n\nThe saved model contains: \n\n - the model's configuration (topology)\n - the model's weights\n - the model's optimizer's state (if any)\n\nThus the saved model can be reinstantiated in\nthe exact same state, without any of the code\nused for model definition or training.\n\n*SavedModel serialization* (not yet added)\n\nThe SavedModel serialization path uses [`tf.saved_model.save`](../../../tf/saved_model/save) to save the model\nand all trackable objects attached to the model (e.g. layers and variables).\n`@tf.function`-decorated methods are also saved. Additional trackable objects\nand functions are added to the SavedModel to allow the model to be\nloaded back as a Keras Model object.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `model` | Keras model instance to be saved. |\n| `filepath` | One of the following: \u003cbr /\u003e - String, path where to save the model - `h5py.File` object where to save the model |\n| `overwrite` | Whether we should overwrite any existing model at the target location, or instead ask the user with a manual prompt. |\n| `include_optimizer` | If True, save optimizer's state together. |\n| `save_format` | 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. |\n| `signatures` | Signatures to save with the SavedModel. Applicable to the 'tf' format only. Please see the `signatures` argument in [`tf.saved_model.save`](../../../tf/saved_model/save) for details. |\n| `options` | Optional [`tf.saved_model.SaveOptions`](../../../tf/saved_model/SaveOptions) object that specifies options for saving to SavedModel. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|---------------|----------------------------------------------------|\n| `ImportError` | If save format is hdf5, and h5py is not available. |\n\n\u003cbr /\u003e"]]