How to save/restore a model after training?
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
When training machine learning models, particularly deep learning models, the process can be computationally expensive and time-consuming. Consequently, it's essential to save your models once training is complete, so you don’t have to retrain them from scratch in the future. Moreover, being able to restore a trained model allows for quick deployment and evaluation on new data.
This guide will walk you through the process of saving and restoring models using popular frameworks like TensorFlow/Keras and PyTorch, while discussing key considerations and configurations.
Saving and Restoring Models in TensorFlow/Keras
Saving Models
In TensorFlow/Keras, models can be saved in various ways, such as:
- HDF5 Format: This format saves the entire model architecture, weights, and training configuration in a single file.
- TensorFlow SavedModel Format: This is TensorFlow's standard format which saves the model architecture, weights, and any included custom layers or objects.
Restoring Models
To restore models in TensorFlow/Keras:
- From HDF5:
- From SavedModel:
It's crucial to ensure that any custom objects or layers are available when restoring the model. For example:
Saving and Restoring Models in PyTorch
Saving Models
In PyTorch, the model state and optimizer state can be saved using the torch.save() function. PyTorch recommends saving the model's state dictionary rather than the entire model object.
Restoring Models
To restore a PyTorch model, initialize the model and optimizer first, then load the saved state dictionaries.
Key Considerations
- Version Compatibility: Ensure your TensorFlow, Keras, or PyTorch versions are compatible with the serialized model formats.
- Custom Layers/Objects: Use
custom_objectsin Keras or ensure all required classes are defined/imported in PyTorch tooling. - Environment Consistency: When deploying a model, replicate the training environment, libraries, and model structures as closely as possible.
Summary Table
| Framework | Save Method | Restore Method | Key Note |
| TensorFlow/Keras | HDF5 | load_model() | Single file saving architecture and weights. |
| TensorFlow/Keras | SavedModel | load_model() | Standard format with broader functionality. |
| PyTorch | State Dict | Load state_dict | Recomended method for saving model parameters. |
| PyTorch | Entire Model | Load Model, discouraged usage | Not recommended, hard to maintain versioning. |
Additional Considerations
Checkpoints: Implement model checkpointing during training to save intermediate states of the model as a fallback strategy against data loss or adverse events like training interruptions.
Model Versioning: Consider using tools designed for model versioning and tracking, such as MLflow, DVC (Data Version Control), or TensorBoard. These can integrate with CI/CD pipelines to automate model deployment and monitoring.
Security: Handle model files securely, especially in sensitive applications. Consider encryption if the model's deployment or storage location is not secure.
By adhering to these strategies, you ensure a smooth transition from model training to deployment, with reliable processes for persisting and reloading your model as needed.
Related reading
- How to save/restore a model after training?
- How to seek for bigram similarity in gensim word2vec model
- How to select batch size automatically to fit GPU?
- How to select half precision BFLOAT16 vs FLOAT16 for your trained model?
- How to select all columns except one in pandas?
- How to select distinct across multiple data frame columns in pandas?
- How to scale k8s pods according to rabbitmq queue message rate?
- how to scale kubernetes daemonset to 0?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the 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.