model saving
model restoration
machine learning
model deployment
data science

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.

Practice ML system design

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:

  1. HDF5 Format: This format saves the entire model architecture, weights, and training configuration in a single file.
python
   # Assume `model` is a Keras model instance
   model.save('path_to_my_model.h5')
  1. TensorFlow SavedModel Format: This is TensorFlow's standard format which saves the model architecture, weights, and any included custom layers or objects.
python
   model.save('path_to_my_model', save_format='tf')

Restoring Models

To restore models in TensorFlow/Keras:

  1. From HDF5:
python
   from keras.models import load_model
   model = load_model('path_to_my_model.h5')
  1. From SavedModel:
python
   model = tf.keras.models.load_model('path_to_my_model')

It's crucial to ensure that any custom objects or layers are available when restoring the model. For example:

python
custom_objects = {'CustomLayer': CustomLayer}
model = load_model('path', custom_objects=custom_objects)

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.

python
1# Save model and optimizer state
2torch.save({'model_state_dict': model.state_dict(),
3            'optimizer_state_dict': optimizer.state_dict()}, 
4           'path_to_my_model.pth')

Restoring Models

To restore a PyTorch model, initialize the model and optimizer first, then load the saved state dictionaries.

python
1checkpoint = torch.load('path_to_my_model.pth')
2model = TheModelClass(*args, **kwargs)
3optimizer = TheOptimizerClass(*args, **kwargs)
4
5model.load_state_dict(checkpoint['model_state_dict'])
6optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
7
8model.eval()  # Set to evaluation mode, if needed

Key Considerations

  • Version Compatibility: Ensure your TensorFlow, Keras, or PyTorch versions are compatible with the serialized model formats.
  • Custom Layers/Objects: Use custom_objects in 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

FrameworkSave MethodRestore MethodKey Note
TensorFlow/KerasHDF5load_model()Single file saving architecture and weights.
TensorFlow/KerasSavedModelload_model()Standard format with broader functionality.
PyTorchState DictLoad state_dictRecomended method for saving model parameters.
PyTorchEntire ModelLoad Model, discouraged usageNot 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice ML system design

All Rights Reserved.