load_weights() reads numeric weight values from a file created by save_weights() and assigns them into an already-existing model's layers — unlike load_model(), it does not reconstruct any architecture, so the model must already be built, and typically compiled, with layers whose shapes exactly match what was saved, before calling this. It's the standard way to restore a training checkpoint into a freshly re-created model instance, or to load pretrained weights into a custom architecture for transfer learning.
1Understanding model.load_weights()
load_weights() reads numeric weight values from a file created by save_weights() and assigns them into an already-existing model's layers — unlike load_model(), it does not reconstruct any architecture, so the model must already be built, and typically compiled, with layers whose shapes exactly match what was saved, before calling this. It's the standard way to restore a training checkpoint into a freshly re-created model instance, or to load pretrained weights into a custom architecture for transfer learning.
The model you call load_weights() on must have the exact same architecture, same layers in the same order with matching shapes, as the model that originally called save_weights() — a shape mismatch raises an error rather than silently loading partial or incorrect weights.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(2, input_shape=(3,))])
model.save_weights('checkpoint.weights.h5')
new_model = Sequential([layers.Dense(2, input_shape=(3,))])
new_model.load_weights('checkpoint.weights.h5')
print((model.get_weights()[0] == new_model.get_weights()[0]).all())2Practical Example
Here is a real-world application of model.load_weights() showing how it is used in production TensorFlow code.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(2, input_shape=(3,))])
model.save_weights('checkpoint.weights.h5')
mismatched_model = Sequential([layers.Dense(5, input_shape=(3,))])
try:
mismatched_model.load_weights('checkpoint.weights.h5')
print('loaded')
except Exception:
print('shape mismatch error')3Best Practices
Follow these guidelines when working with model.load_weights():
1. Build the exact same architecture in code before calling load_weights(), since it has no ability to reconstruct or verify architecture the way load_model() does
2. Use load_weights() to restore a training checkpoint into a freshly re-instantiated model of the same architecture, resuming training from where it left off
3. Consider by_name=True when loading weights into a model with a different but overlapping architecture, matching layers by name rather than requiring an exact one-to-one structural match
Tip: The model you call load_weights() on must have the exact same architecture, same layers in the same order with matching shapes, as the model that originally called save_weights() — a shape mismatch raises an error rather than silently loading partial or incorrect weights.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(2, input_shape=(3,))])
model.save_weights('checkpoint.weights.h5')
new_model = Sequential([layers.Dense(2, input_shape=(3,))])
new_model.load_weights('checkpoint.weights.h5')
print((model.get_weights()[0] == new_model.get_weights()[0]).all())