How to save and restore Keras LSTM 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
Keras provides several ways to save and restore LSTM models: model.save() saves the complete model (architecture, weights, optimizer state) to a single file, model.save_weights() saves only the weights, and model.to_json() exports only the architecture. The recommended modern format is the Keras native format (.keras), which replaced the legacy HDF5 (.h5) format. For TensorFlow deployment, use tf.saved_model.save() to export a SavedModel directory.
Saving the Complete Model
model.save() stores everything needed to restore the model: layer architecture, trained weights, optimizer state, and compilation config. The .keras format is the default in Keras 3+.
Restoring the Complete Model
load_model() reconstructs the full model including the optimizer, so you can resume training immediately without recompiling.
Saving and Loading Weights Only
Weight-only saving is useful when the model architecture is defined in code and you only need to store the learned parameters. The architecture must match exactly when loading.
Saving Architecture as JSON
JSON export captures the layer configuration but not weights or optimizer state. This is useful for sharing model designs or version-controlling architecture changes.
SavedModel Format for TensorFlow Serving
SavedModel is the standard format for TensorFlow Serving, TensorFlow Lite conversion, and TensorFlow.js export.
Checkpointing During Training
ModelCheckpoint automatically saves the model at specified intervals. save_best_only=True keeps only the version with the lowest validation loss.
Custom LSTM with Custom Objects
Custom layers must implement get_config() to be serializable. When loading, pass custom_objects so Keras can reconstruct the custom layer.
Common Pitfalls
- Architecture mismatch when loading weights:
load_weights()requires the model architecture to be identical to the one used during saving. Adding, removing, or renaming layers causes shape mismatch errors. - Missing custom_objects on load: Models with custom layers, losses, or metrics fail to load without
custom_objects. Register them via@keras.utils.register_keras_serializable()or pass them toload_model(). - HDF5 format deprecated in Keras 3: The
.h5format is legacy. Use.kerasfor new projects. HDF5 does not support some Keras 3 features like custom saving logic. - Optimizer state not saved with save_weights:
save_weights()only saves layer weights. Optimizer state (momentum, learning rate schedule) is lost. Usemodel.save()if you need to resume training exactly where you left off. - Large LSTM models and memory: Loading a large LSTM model allocates memory for all layers at once. On memory-constrained systems, use
tf.lite.TFLiteConverterto convert to a smaller TensorFlow Lite model before deployment.
Summary
- Use
model.save('model.keras')to save the complete model (architecture + weights + optimizer) - Use
load_model('model.keras')to restore and resume training - Use
save_weights()/load_weights()when architecture is defined in code - Use
ModelCheckpointcallback to save the best model during training - Use SavedModel format (
tf.saved_model.save) for TensorFlow Serving deployment - Custom layers require
get_config()implementation andcustom_objectson load
Related reading
- How to save and restore partitioned variable in Tensorflow
- How to save final model using keras?
- How To Save Keras Regressor Model?
- How to save TextVectorization to disk in tensorflow?
- How to save estimator in Tensorflow for later use?
- How to save Keras model as frozen graph?
- How to save the model for text-classification in tensorflow?
- How to save training history on every epoch in Keras?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.