tf.keras.models.load_model
Stay organized with collections
Save and categorize content based on your preferences.
Loads a model saved via model.save()
.
tf.keras.models.load_model(
filepath, custom_objects=None, compile=True, safe_mode=True
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
Args |
filepath
|
str or pathlib.Path object, path to the saved model file.
|
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.
|
safe_mode
|
Boolean, whether to disallow unsafe lambda deserialization.
When safe_mode=False , loading an object has the potential to
trigger arbitrary code execution. This argument is only
applicable to the Keras v3 model format. Defaults to True .
|
Returns |
A Keras model instance. If the original model was compiled,
and the argument compile=True is set, then the returned model
will be compiled. Otherwise, the model will be left uncompiled.
|
Example:
model = keras.Sequential([
keras.layers.Dense(5, input_shape=(3,)),
keras.layers.Softmax()])
model.save("model.keras")
loaded_model = keras.saving.load_model("model.keras")
x = np.random.random((10, 3))
assert np.allclose(model.predict(x), loaded_model.predict(x))
Note that the model variables may have different name values
(var.name
property, e.g. "dense_1/kernel:0"
) after being reloaded.
It is recommended that you use layer attributes to
access specific variables, e.g. model.get_layer("dense_1").kernel
.
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. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.models.load_model\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/saving/saving_api.py#L116-L205) |\n\nLoads a model saved via `model.save()`. \n\n tf.keras.models.load_model(\n filepath, custom_objects=None, compile=True, safe_mode=True\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Introduction to modules, layers, and models](https://www.tensorflow.org/guide/intro_to_modules) - [Migrate \\`tf.feature_column\\`s to Keras preprocessing layers](https://www.tensorflow.org/guide/migrate/migrating_feature_columns) - [Migrate the SavedModel workflow](https://www.tensorflow.org/guide/migrate/saved_model) - [Migrating your TFLite code to TF2](https://www.tensorflow.org/guide/migrate/tflite) - [Multi-GPU and distributed training](https://www.tensorflow.org/guide/keras/distributed_training) | - [Save and load a model using a distribution strategy](https://www.tensorflow.org/tutorials/distribute/save_and_load) - [Save and load models](https://www.tensorflow.org/tutorials/keras/save_and_load) - [Distributed training with Keras](https://www.tensorflow.org/tutorials/distribute/keras) - [Multi-worker training with Keras](https://www.tensorflow.org/tutorials/distribute/multi_worker_with_keras) - [Transfer learning with TensorFlow Hub](https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `filepath` | `str` or `pathlib.Path` object, path to the saved model file. |\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| `safe_mode` | Boolean, whether to disallow unsafe `lambda` deserialization. When `safe_mode=False`, loading an object has the potential to trigger arbitrary code execution. This argument is only applicable to the Keras v3 model format. Defaults to `True`. |\n\n\u003cbr /\u003e\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 the argument `compile=True` is set, then the returned model will be compiled. Otherwise, the model will be left uncompiled. ||\n\n\u003cbr /\u003e\n\n#### Example:\n\n model = keras.Sequential([\n keras.layers.Dense(5, input_shape=(3,)),\n keras.layers.Softmax()])\n model.save(\"model.keras\")\n loaded_model = keras.saving.load_model(\"model.keras\")\n x = np.random.random((10, 3))\n assert np.allclose(model.predict(x), loaded_model.predict(x))\n\nNote that the model variables may have different name values\n(`var.name` property, e.g. `\"dense_1/kernel:0\"`) after being reloaded.\nIt is recommended that you use layer attributes to\naccess specific variables, e.g. `model.get_layer(\"dense_1\").kernel`."]]