Keras 3 API documentation / Models API / Saving & serialization / Whole model saving & loading

Whole model saving & loading

[source]

save method

Model.save(filepath, overwrite=True, **kwargs)

Saves a model as a .keras file.

Arguments

  • filepath: str or pathlib.Path object. Path where to save the model. Must end in .keras.
  • overwrite: Whether we should overwrite any existing model at the target location, or instead ask the user via an interactive prompt.
  • save_format: The save_format argument is deprecated in Keras 3. Format to use, as a string. Only the "keras" format is supported at this time.

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 = keras.random.uniform((10, 3))
assert np.allclose(model.predict(x), loaded_model.predict(x))

Note that model.save() is an alias for keras.saving.save_model().

The saved .keras file contains:

  • The model's configuration (architecture)
  • The model's weights
  • The model's optimizer's state (if any)

Thus models can be reinstantiated in the exact same state.


[source]

save_model function

keras.saving.save_model(model, filepath, overwrite=True, **kwargs)

Saves a model as a .keras file.

Arguments

  • model: Keras model instance to be saved.
  • filepath: str or pathlib.Path object. Path where to save the model.
  • overwrite: Whether we should overwrite any existing model at the target location, or instead ask the user via an interactive prompt.

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 = keras.random.uniform((10, 3))
assert np.allclose(model.predict(x), loaded_model.predict(x))

Note that model.save() is an alias for keras.saving.save_model().

The saved .keras file contains:

  • The model's configuration (architecture)
  • The model's weights
  • The model's optimizer's state (if any)

Thus models can be reinstantiated in the exact same state.


[source]

load_model function

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

Loads a model saved via model.save().

Arguments

  • 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.