tf.keras.models.save_model

Saves a model as a TensorFlow SavedModel or HDF5 file.

Used in the notebooks

Used in the guide Used in the tutorials

See the Serialization and Saving guide for details.

Usage:

model = tf.keras.Sequential([
    tf.keras.layers.Dense(5, input_shape=(3,)),
    tf.keras.layers.Softmax()])
model.save('/tmp/model')
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. model.get_layer("dense_1").kernel