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.
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.
If you saved to an HDF5 file, the call is similar:
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().
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().
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:
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.
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_modelplusload_modelwhen you want the whole model back - use
save_weightsplusload_weightswhen you only want parameter values - use
get_weightsandset_weightsfor 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_modeloutput 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_objectscan makeload_modelfail 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 withload_modelinstead. - 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 istf.keras.models.load_model. - Once loaded, the model already has the trained weights attached.
- Use
get_weights()if you want the raw tensors andset_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.
Related reading
- How to load an image and show the image using keras?
- how to load and use a saved model on tensorflow?
- How to load only specific weights on Keras
- How to load only specific weights on Keras
- How to load a trained TF1 protobuf model into TF2?
- How to load batches of CSV files using tf.data and map
- How to locate multiple objects in the same image?
- How to log Keras loss output to a file
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.