While saving only the weights using model.save_weights()
is useful for transferring learned parameters between identical architectures or as lightweight checkpoints, often you need to preserve the entire state of your model. This includes not just the weights, but also the model's architecture (how the layers are connected) and its training configuration (optimizer, losses, metrics defined during model.compile()
). Saving the complete model ensures that you, or someone else, can recreate the exact model state later for inference, further training, or analysis without needing the original code that defined the model structure.
TensorFlow, through Keras, provides a straightforward way to save the entire model using the model.save()
method. This method bundles everything needed to reconstitute the model.
model.save()
The model.save()
function serializes the model into a specified path. By default, it uses TensorFlow's standard SavedModel format, which is the recommended approach for most use cases, especially deployment. Alternatively, you can explicitly save to the older Keras HDF5 format.
When you call model.save()
and provide a directory path, TensorFlow saves the model in the SavedModel format.
# Assume 'model' is a compiled Keras model that has been trained
# Save the entire model to a directory named 'my_full_model'
model.save('my_full_model')
This command creates a directory named my_full_model
containing the following components:
saved_model.pb
: This Protocol Buffer file stores the TensorFlow graph defining the model's architecture and computation logic. It includes the forward pass (inference) and potentially the training graph if applicable.variables/
directory: This contains the learned weights (parameters) of the model, stored in a format suitable for efficient loading.assets/
directory: An optional directory for any external files needed by the model, such as vocabulary files for text processing layers.keras_metadata.pb
: Stores Keras-specific metadata, including the optimizer state, loss configuration, and metrics defined during model.compile()
. This is essential for resuming training.The SavedModel format is language-neutral and designed for serving environments like TensorFlow Serving, TensorFlow Lite (for mobile/edge devices), and TensorFlow.js (for web browsers). It captures not only the Keras model structure but also the underlying TensorFlow computation graph, making it robust for deployment.
.h5
or .keras
)You can also save the entire model to a single HDF5 file. This format bundles the architecture, weights, and training configuration (optimizer state, losses, metrics) into one binary file. While convenient as a single file, the SavedModel format is generally preferred for its broader compatibility and deployment features.
To save in HDF5 format, provide a filename ending in .h5
or the newer .keras
extension (preferred for Keras V3 compatibility).
# Save the entire model to a single HDF5 file (newer format)
model.save('my_full_model.keras')
# Save the entire model to a single HDF5 file (legacy format)
# model.save('my_full_model.h5')
This creates a single file (my_full_model.keras
or my_full_model.h5
) containing:
When using model.save()
for the entire model (in either format), you preserve:
model.compile()
.This completeness is significant because it allows you to reload the model and resume training exactly where you left off, maintaining the optimizer's momentum and learning rate adjustments. It also ensures that evaluating the loaded model uses the same metrics defined during the original setup.
Loading a model saved with model.save()
is done using tf.keras.models.load_model()
. TensorFlow automatically detects the format (SavedModel directory or HDF5 file) based on the path provided.
import tensorflow as tf
# Load a model from the SavedModel format
loaded_model_sm = tf.keras.models.load_model('my_full_model')
# Load a model from the HDF5 format
loaded_model_h5 = tf.keras.models.load_model('my_full_model.keras') # or .h5
# Verify the loaded model structure
loaded_model_sm.summary()
The load_model
function reconstitutes the model architecture, loads the weights, and restores the training configuration (optimizer, losses, metrics). The loaded model is already compiled with the saved configuration and is ready for use.
A key advantage of loading a fully saved model is the ability to seamlessly continue training or use it directly for inference.
# Assume you have new data (new_data, new_labels) or want to continue training
# The loaded model remembers its optimizer state and compilation settings
# Example: Continue training for a few more epochs
history = loaded_model_sm.fit(training_data, training_labels, epochs=10, validation_data=(val_data, val_labels))
# Example: Make predictions on new, unseen data
predictions = loaded_model_sm.predict(new_unseen_data)
Because the optimizer state was saved and loaded, training resumes effectively, building upon the previous learning process.
Saving the entire model is generally the best approach when:
Compared to saving only weights, saving the entire model provides a more complete and reproducible snapshot, crucial for reliable deployment and collaborative workflows. Remember that the SavedModel format is the standard and most versatile option provided by TensorFlow for sharing and deploying trained models.
© 2025 ApX Machine Learning