tf.keras
model weights
load model
deep learning
save model

How to load a trained model's weights, which were saved with tf.keras.models.save_model?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If you saved a model with tf.keras.models.save_model, you did not save only the weights. You saved the full model package, including architecture and, depending on format and configuration, additional training state. The easiest way to get the weights back is therefore to load the full model first, then either use it directly or copy its weights into another compatible model.

Load the Full Saved Model First

When the model was saved with save_model, the standard matching API is load_model.

python
1import tensorflow as tf
2
3model = tf.keras.models.load_model("saved_model_dir")
4model.summary()

If you saved to an HDF5 file, the call is similar:

python
model = tf.keras.models.load_model("model.h5")

Once loaded, the model already contains the weights, so in many cases there is nothing else you need to do.

If You Only Want the Weight Tensors

Sometimes you want the weights from the saved model but not the saved model object itself. In that case, load the model and call get_weights().

python
1import tensorflow as tf
2
3saved_model = tf.keras.models.load_model("saved_model_dir")
4weights = saved_model.get_weights()
5
6for tensor in weights:
7    print(tensor.shape)

This returns a list of NumPy arrays representing the trainable and non-trainable weight tensors in layer order.

That is the right answer if the question is literally "how do I access the weights that were saved with save_model."

Copy the Weights Into a New Compatible Model

If you have recreated the same architecture separately and want to transfer the saved weights into it, use set_weights().

python
1import tensorflow as tf
2
3saved_model = tf.keras.models.load_model("saved_model_dir")
4
5new_model = tf.keras.Sequential([
6    tf.keras.layers.Input(shape=(10,)),
7    tf.keras.layers.Dense(32, activation="relu"),
8    tf.keras.layers.Dense(1)
9])
10
11new_model.set_weights(saved_model.get_weights())

This only works if the layer order and weight shapes are compatible. If the new model differs even slightly, set_weights() will fail.

That is an important distinction: save_model stores a full model, but set_weights() is a lower-level operation that assumes architectural compatibility.

When load_weights() Is the Right API Instead

load_weights() is designed for checkpoints or weight-only saves, not for a full SavedModel that was created with save_model.

This is the classic weight-only pattern:

python
model.save_weights("weights.ckpt")
model.load_weights("weights.ckpt")

If you started with save_model, the more natural inverse is load_model. Trying to force a full saved model into a weight-only loading workflow usually creates confusion.

Custom Objects Still Matter

If the saved model used custom layers, custom losses, or custom metrics, load_model needs those definitions available.

python
1model = tf.keras.models.load_model(
2    "saved_model_dir",
3    custom_objects={"MyLayer": MyLayer}
4)

If you only want the weights, you still have to get past model loading first unless you originally saved them in a pure weight format.

A Safe Practical Workflow

If your goal is to reuse training results later, the clean mental model is:

  • use save_model plus load_model when you want the whole model back
  • use save_weights plus load_weights when you only want parameter values
  • use get_weights and set_weights for manual transfer between compatible models

That separation keeps the APIs easy to reason about and avoids mixing full-model serialization with weight-only expectations.

Common Pitfalls

  • Expecting save_model output to behave exactly like a weight checkpoint leads to API confusion. It is a full model package, not just raw weights.
  • Rebuilding a slightly different architecture and then calling set_weights() fails because the saved tensors no longer match by shape and order.
  • Forgetting custom_objects can make load_model fail even though the saved weights themselves are fine.
  • Using load_weights() on a full-model artifact is often the wrong tool for the job. Start with load_model instead.
  • Assuming that loading weights also restores optimizer state is incorrect unless you are loading the full model in a format that preserved it.

Summary

  • If you saved with tf.keras.models.save_model, the normal inverse operation is tf.keras.models.load_model.
  • Once loaded, the model already has the trained weights attached.
  • Use get_weights() if you want the raw tensors and set_weights() to copy them into a compatible model.
  • Use load_weights() only for weight-only saves such as checkpoints.
  • Keep full-model serialization and weight-only workflows conceptually separate.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design