tf.keras.models.load_model
Stay organized with collections
Save and categorize content based on your preferences.
Loads a model saved via save_model
.
tf.keras.models.load_model(
filepath, custom_objects=None, compile=True
)
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))
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`.
Arguments |
filepath
|
One of the following:
- String or
pathlib.Path object, path to the saved model
h5py.File object from which to load the model
|
custom_objects
|
Optional dictionary mapping names
(strings) to custom classes or functions to be
considered during deserialization.
|
compile
|
Boolean, whether to compile the model
after loading.
|
Returns |
A Keras model instance. If the original model was compiled, and saved with
the optimizer, then the returned model will be compiled. Otherwise, the
model will be left uncompiled. In the case that an uncompiled model is
returned, a warning is displayed if the compile argument is set to
True .
|
Raises |
ImportError
|
if loading from an hdf5 file and h5py is not available.
|
IOError
|
In case of an invalid savefile.
|
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.load_model\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/models/load_model) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/keras/saving/save.py#L141-L194) |\n\nLoads a model saved via `save_model`.\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.load_model`](/api_docs/python/tf/keras/models/load_model)\n\n\u003cbr /\u003e\n\n tf.keras.models.load_model(\n filepath, custom_objects=None, compile=True\n )\n\n#### Usage:\n\n model = tf.keras.Sequential([\n tf.keras.layers.Dense(5, input_shape=(3,)),\n tf.keras.layers.Softmax()])\n model.save('/tmp/model')\n loaded_model = tf.keras.models.load_model('/tmp/model')\n x = tf.random.uniform((10, 3))\n assert np.allclose(model.predict(x), loaded_model.predict(x))\n\nNote that the model weights may have different scoped names after being\nloaded. Scoped names include the model/layer names, such as\n\"dense_1/kernel:0\"`. It is recommended that you use the layer properties to\naccess specific variables, e.g.`model.get_layer(\"dense_1\").kernel\\`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| `filepath` | One of the following: \u003cbr /\u003e - String or `pathlib.Path` object, path to the saved model - `h5py.File` object from which to load the model |\n| `custom_objects` | Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. |\n| `compile` | Boolean, whether to compile the model after loading. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Keras model instance. If the original model was compiled, and saved with the optimizer, then the returned model will be compiled. Otherwise, the model will be left uncompiled. In the case that an uncompiled model is returned, a warning is displayed if the `compile` argument is set to `True`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|---------------|---------------------------------------------------------|\n| `ImportError` | if loading from an hdf5 file and h5py is not available. |\n| `IOError` | In case of an invalid savefile. |\n\n\u003cbr /\u003e"]]