Saves a model as a TensorFlow SavedModel or HDF5 file. 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
(model,
filepath,
overwrite=True,
include_optimizer=True,
save_format=None,
signatures=None)
| 45 | |
| 46 | @keras_export('keras.models.save_model') |
| 47 | def save_model(model, |
| 48 | filepath, |
| 49 | overwrite=True, |
| 50 | include_optimizer=True, |
| 51 | save_format=None, |
| 52 | signatures=None): |
| 53 | """Saves a model as a TensorFlow SavedModel or HDF5 file. |
| 54 | |
| 55 | The saved model contains: |
| 56 | - the model's configuration (topology) |
| 57 | - the model's weights |
| 58 | - the model's optimizer's state (if any) |
| 59 | |
| 60 | Thus the saved model can be reinstantiated in |
| 61 | the exact same state, without any of the code |
| 62 | used for model definition or training. |
| 63 | |
| 64 | _SavedModel serialization_ (not yet added) |
| 65 | |
| 66 | The SavedModel serialization path uses `tf.saved_model.save` to save the model |
| 67 | and all trackable objects attached to the model (e.g. layers and variables). |
| 68 | `@tf.function`-decorated methods are also saved. Additional trackable objects |
| 69 | and functions are added to the SavedModel to allow the model to be |
| 70 | loaded back as a Keras Model object. |
| 71 | |
| 72 | Arguments: |
| 73 | model: Keras model instance to be saved. |
| 74 | filepath: One of the following: |
| 75 | - String, path where to save the model |
| 76 | - `h5py.File` object where to save the model |
| 77 | overwrite: Whether we should overwrite any existing model at the target |
| 78 | location, or instead ask the user with a manual prompt. |
| 79 | include_optimizer: If True, save optimizer's state together. |
| 80 | save_format: Either 'tf' or 'h5', indicating whether to save the model |
| 81 | to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X, and 'h5' |
| 82 | in TF 1.X. |
| 83 | signatures: Signatures to save with the SavedModel. Applicable to the 'tf' |
| 84 | format only. Please see the `signatures` argument in |
| 85 | `tf.saved_model.save` for details. |
| 86 | |
| 87 | Raises: |
| 88 | ImportError: If save format is hdf5, and h5py is not available. |
| 89 | """ |
| 90 | from tensorflow.python.keras.engine import sequential # pylint: disable=g-import-not-at-top |
| 91 | |
| 92 | default_format = 'tf' if tf2.enabled() else 'h5' |
| 93 | save_format = save_format or default_format |
| 94 | |
| 95 | if (save_format == 'h5' or |
| 96 | (h5py is not None and isinstance(filepath, h5py.File)) or |
| 97 | os.path.splitext(filepath)[1] in _HDF5_EXTENSIONS): |
| 98 | # TODO(b/130258301): add utility method for detecting model type. |
| 99 | if (not model._is_graph_network and # pylint:disable=protected-access |
| 100 | not isinstance(model, sequential.Sequential)): |
| 101 | raise NotImplementedError( |
| 102 | 'Saving the model to HDF5 format requires the model to be a ' |
| 103 | 'Functional model or a Sequential model. It does not work for ' |
| 104 | 'subclassed models, because such models are defined via the body of ' |