Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 40 ยท Data Pipelines and Saving Models

Saving and Loading Models

Save and restore Keras models: the .keras format, weights-only files, checkpoints, custom objects, SavedModel export and what is stored, with a NumPy weights round trip.

Training can take hours, so you want to keep the result. Keras can save everything about a model in one file, or only its weights, and can write checkpoints during training so a crash costs minutes, not hours. This lesson covers the formats, when to use each, and the pitfalls around custom layers.

TensorFlow is not available in the browser runner, so the Keras calls are read-only. The runnable blocks reproduce the idea of saving and restoring weights with NumPy.

What a model consists of

A trained model is more than numbers:

  1. Architecture: layers and how they connect.
  2. Weights: the learned values.
  3. Training configuration: optimizer, loss and metrics from compile().
  4. Optimizer state: for example Adam's running averages, so training can resume exactly.

Different save formats keep different subsets of these.

The .keras file

The recommended whole-model format is a single .keras file (a zip archive containing a JSON config, the weights and the optimizer state).

Python
import keras

model.save("classifier.keras")

restored = keras.models.load_model("classifier.keras")
restored.summary()
restored.evaluate(x_test, y_test)     # same results as before saving

Because the optimizer state is saved, restored.fit(...) continues where training stopped.

Weights only

If your code can rebuild the architecture, you can store only the weights. The file name must end in .weights.h5.

Python
model.save_weights("run1.weights.h5")

new_model = build_model()               # same architecture, same layer order
new_model.load_weights("run1.weights.h5")

This is lighter and is the right tool for checkpoints and for loading weights into a slightly modified architecture (for example a new head, with skip_mismatch=True when shapes differ).

The idea behind it: a NumPy round trip

Under the hood, saving means writing each layer's arrays to disk and loading means putting them back into a freshly built structure. Here is the same thing with np.savez, using an in-memory buffer instead of a file.

Python Save and restore weights

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The restored model matches only because the new structure has the same shapes in the same order. That is exactly why load_weights needs an identical architecture, and why layer order changes break old weight files.

Checkpoints during training

The ModelCheckpoint callback writes a file whenever the monitored metric improves.

Python
checkpoint = keras.callbacks.ModelCheckpoint(
    "best.keras",
    monitor="val_loss",
    save_best_only=True,
)
early = keras.callbacks.EarlyStopping(monitor="val_loss", patience=5,
                                      restore_best_weights=True)
model.fit(train_ds, validation_data=val_ds, epochs=100,
          callbacks=[checkpoint, early])

Use save_weights_only=True with a .weights.h5 name to save only weights. File names can include the epoch: "ckpt-{epoch:02d}.keras".

Python Best-so-far checkpointing logic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Custom layers and loading

A model containing your own classes can only be restored if Keras can find them. Register the class with a decorator, or pass it through custom_objects.

Python
@keras.saving.register_keras_serializable(package="my")
class Scale(keras.layers.Layer):
    def __init__(self, factor=2.0, **kwargs):
        super().__init__(**kwargs)
        self.factor = factor
    def call(self, x):
        return x * self.factor
    def get_config(self):
        config = super().get_config()
        config["factor"] = self.factor
        return config

model.save("with_custom.keras")
keras.models.load_model("with_custom.keras")          # works, class is registered
# without the decorator:
keras.models.load_model("with_custom.keras", custom_objects={"Scale": Scale})

The key is get_config(): it must return every constructor argument needed to rebuild the layer. Writing layers is covered in Custom Layers and Models.

Exporting for serving

For deployment, model.export("exported_model") writes a TensorFlow SavedModel directory containing a serving signature: a compiled graph that TensorFlow Serving, TFLite conversion and other tools consume without any of your Python code. A .keras file is for continuing work in Keras; an exported SavedModel is for production. See Deploying Models.

Python
model.export("exported_model")                     # SavedModel for serving
loaded = tf.saved_model.load("exported_model")
infer = loaded.signatures["serve"]

Security and versions

  • load_model runs with safe_mode=True by default, which refuses to execute arbitrary code stored in a file (such as a Lambda layer's lambda). Only disable it for files you trust.
  • Pin the Keras and TensorFlow versions with the model. Old .h5 and SavedModel files from TensorFlow 2.x may need conversion; legacy .h5 full-model files are still readable but not recommended.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which format lets you resume training with the same optimizer state?

    .keras stores architecture, weights, compile configuration and optimizer state.
  2. 2. What must be true to call load_weights on a new model?

    Weights are matched to layers by structure, so shapes and order must agree.
  3. 3. A custom layer must implement which method to survive save/load?

    get_config lets Keras rebuild the layer from the saved JSON configuration.
  4. 4. What does save_best_only=True do in ModelCheckpoint?

    The file is overwritten only when the monitored value gets better.

Summary

  • model.save("x.keras") stores architecture, weights, compile config and optimizer state; keras.models.load_model restores it.
  • save_weights("x.weights.h5") stores weights only and needs the same architecture to load.
  • ModelCheckpoint and EarlyStopping(restore_best_weights=True) protect long training runs.
  • Custom classes need get_config() and registration or custom_objects.
  • Use model.export() (SavedModel) when handing the model to serving infrastructure.

Next: Deploying Models