Model restoration
Restore a previously saved model for inference or further training.
TensorFlow · Function
Loads a Keras model saved with model.save() and returns a Keras model instance. It can restore compilation and deserialize custom objects, while safe_mode controls unsafe lambda loading.
The function reconstructs a Keras model from a path produced by model.save(). It can restore compilation state, resolve custom classes or functions through custom_objects, and optionally prevent unsafe lambda deserialization.
tf.keras.models.load_model(filepath, custom_objects=None, compile=True, safe_mode=True)| Name | Type | Description |
|---|---|---|
filepath | str or pathlib.Path | Path to the saved model file; accepts a string or pathlib.Path object. |
custom_objects | dict or None | Optional mapping from serialized names to custom classes or functions used during deserialization. |
compile | bool | Whether to compile the model after loading. |
safe_mode | bool | Whether to reject unsafe lambda deserialization; defaults to True and applies to the Keras v3 format. |
A Keras model instance. If the original model was compiled and compile=True, the returned model is compiled; otherwise it is uncompiled.
model.save().custom_objects when deserialization needs custom classes or functions.compile flag controls whether the reconstructed model is compiled.safe_mode determines whether unsafe lambda deserialization is rejected.compile=True.safe_mode for untrusted model files because deserialization can execute arbitrary code.safe_mode option affects Keras v3 files, not other model formats.import numpy as np
import tensorflow as tf
from tensorflow import keras
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, verbose=0), loaded_model.predict(x, verbose=0))Creates, saves, reloads, and compares predictions from a Keras model stored in the recommended .keras format.
Restore a previously saved model for inference or further training.
Reconstruct models containing application-specific layers or functions.
Load a model while choosing whether its training configuration is restored.
load_model return and what are its main controls?It accepts a saved-model path, optional custom-object mappings, a compilation flag, and a safe-deserialization flag. The result is a Keras model instance.
Follow-up: What happens when compile is false? The returned model is left uncompiled, even if the saved model had compilation information.
custom_objects maps serialized names to the custom classes or functions needed during deserialization. Without those definitions, a saved .keras file cannot restore custom objects.
Follow-up: How can custom objects be supplied without passing them directly? Register them or use a Keras custom-object scope around the loading call.
compile argument affect the loaded model?With compile=True, a previously compiled model is compiled again after loading. With compile=False, loading returns the model without compilation.
Follow-up: Which option should be treated cautiously for untrusted files? safe_mode=False, because unsafe lambda deserialization can trigger arbitrary code execution.
tf.keras.Model.savetf.keras.models.save_modeltf.keras.saving.load_modeltf.saved_model.loadkeras.saving.custom_object_scope