Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

keras.models.load_model

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.

How it works

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.

Syntax

tf.keras.models.load_model(filepath, custom_objects=None, compile=True, safe_mode=True)

Parameters

NameTypeDescription
filepathstr or pathlib.PathPath to the saved model file; accepts a string or pathlib.Path object.
custom_objectsdict or NoneOptional mapping from serialized names to custom classes or functions used during deserialization.
compileboolWhether to compile the model after loading.
safe_modeboolWhether to reject unsafe lambda deserialization; defaults to True and applies to the Keras v3 format.

Returns

A Keras model instance. If the original model was compiled and compile=True, the returned model is compiled; otherwise it is uncompiled.

Key points

  • Use this function to reconstruct a model from a file or path created by model.save().
  • Provide custom_objects when deserialization needs custom classes or functions.
  • The compile flag controls whether the reconstructed model is compiled.
  • safe_mode determines whether unsafe lambda deserialization is rejected.
  • The return value is a Keras model object rather than a generic loaded artifact.
  • A saved compiled model remains compiled when loading uses compile=True.

Common mistakes

  • Do not disable safe_mode for untrusted model files because deserialization can execute arbitrary code.
  • The safe_mode option affects Keras v3 files, not other model formats.
  • Do not depend on variable names remaining identical after loading; use layer attributes to access variables.

Example: Save and reload a Keras model

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.

When to use it

Model restoration

Restore a previously saved model for inference or further training.

Custom-object deserialization

Reconstruct models containing application-specific layers or functions.

Controlled compilation

Load a model while choosing whether its training configuration is restored.

Interview questions

QWhat does 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.

QHow are custom layers and functions restored?

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.

QHow does the 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.save
  • tf.keras.models.save_model
  • tf.keras.saving.load_model
  • tf.saved_model.load
  • keras.saving.custom_object_scope

Source: tf.keras.models.load_model | TensorFlow v2.16.1. Checked 2026-10-15.