save_weights() is a lighter-weight alternative to the full model.save(), storing just the numeric values of every trainable weight and bias, with no information about the architecture connecting them or the optimizer's configuration. It's most useful for checkpointing during training, saving progress periodically without the overhead of re-serializing the full model each time, or for transferring learned weights into a separately, independently defined model with a matching architecture, such as during transfer learning.
1Understanding model.save_weights()
save_weights() is a lighter-weight alternative to the full model.save(), storing just the numeric values of every trainable weight and bias, with no information about the architecture connecting them or the optimizer's configuration. It's most useful for checkpointing during training, saving progress periodically without the overhead of re-serializing the full model each time, or for transferring learned weights into a separately, independently defined model with a matching architecture, such as during transfer learning.
save_weights() alone can't be used to reconstruct a model from nothing — you need the exact same architecture already built in code before you can load these saved weights back into it with load_weights().
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import os
model = Sequential([layers.Dense(2, input_shape=(3,))])
model.save_weights('my_weights.weights.h5')
print(os.path.exists('my_weights.weights.h5'))2Practical Example
Here is a real-world application of model.save_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,))])
original_weights = model.get_weights()[0].copy()
model.save_weights('my_weights.weights.h5')
model.set_weights([w * 0 for w in model.get_weights()])
model.load_weights('my_weights.weights.h5')
restored_weights = model.get_weights()[0]
print((original_weights == restored_weights).all())3Best Practices
Follow these guidelines when working with model.save_weights():
1. Use save_weights() for periodic checkpointing during long training runs, where the lighter-weight save avoids the overhead of re-serializing the full architecture each time
2. Keep a copy of the model-building code alongside any saved weights file, since load_weights() requires reconstructing the exact matching architecture yourself before loading
3. Use save_weights() when transferring learned weights into a separately defined, matching-architecture model, such as for transfer learning or fine-tuning
Tip: save_weights() alone can't be used to reconstruct a model from nothing — you need the exact same architecture already built in code before you can load these saved weights back into it with load_weights().
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import os
model = Sequential([layers.Dense(2, input_shape=(3,))])
model.save_weights('my_weights.weights.h5')
print(os.path.exists('my_weights.weights.h5'))