
Model checkpointing is a way to save a machine learning model’s progress while training, allowing you to pause training and pick up later without losing any work. Checkpointing also helps save the best model at different points.
In PyCaret, a simple tool that automates many machine learning tasks, checkpointing is easy to set up. It automatically saves the best model during training, tuning, or model comparison.
In this article, we will show you how to set up checkpointing in PyCaret. You’ll learn how to save models, load them later, and keep your work safe during long experiments.
Installing and Setting Up PyCaret
You can install PyCaret using pip:
pip install pycaret
Setting Up the Experiment
PyCaret supports tasks like classification, regression, clustering, and anomaly detection. In this example, we’ll use classification.
First, import the necessary libraries and set up the PyCaret environment:
from pycaret.classification import * from sklearn.datasets import load_iris import pandas as pd # Load the iris dataset data = load_iris() # Create a DataFrame df = pd.DataFrame(data['data'], columns=data['feature_names']) # Add the target column to the DataFrame df['target'] = data['target'] # Set up PyCaret environment exp = setup(df, target='target', session_id=123)
The setup() function initializes the PyCaret environment and prepares the data for modeling. You can specify other parameters depending on your dataset and the task at hand.
Configure Model Checkpointing
In PyCaret, checkpointing is managed through the tune_model() and compare_models() functions. These functions handle hyperparameter optimization and model comparison.
PyCaret uses the model_checkpoint() function to save the best model. PyCaret automatically handles checkpoints during model training. To ensure this, you must:
- Set up the save argument to store the best model at every checkpoint
- Specify a checkpoint directory where models will be stored
Model Checkpointing with compare_models()
Let’s compare and find the best model:
# Compare models and get the best model best_model = compare_models(fold=5, sort='AUC')
In this example:
- The fold=5 argument sets the number of cross-validation folds
- The sort=’AUC’ argument ensures the models are ranked by AUC (Area Under the Curve)
The best model is saved in the pycaret/checkpoints folder by default.
Model Checkpointing with tune_model()
You can also perform model tuning with checkpointing:
tuned_model = tune_model(best_model)
Here, tune_model() performs hyperparameter optimization, and PyCaret will save the best model automatically.
Customizing the Checkpoint Directory
You can customize where the model checkpoints are saved in PyCaret. To do this, pass a checkpoint argument when calling setup(). This lets you choose a custom directory to save the checkpoints.
exp = setup(df, target='target', session_id=123, checkpoint='path/to/checkpoint/directory')
Replace ‘path/to/checkpoint/directory’ with the path to your desired checkpoint folder. Now, any saved models will be stored in this custom directory.
Loading the Saved Model
Once training is done and the model is saved, you can use PyCaret’s load_model() function. Provide the path to the saved model to load it.
# Load the best model
loaded_model = load_model('path/to/checkpoint/directory/best_model')
This will load the saved model from the checkpoint directory. You can then use it for further predictions.
Conclusion
Model checkpointing helps save your work during training. It makes sure you don’t lose progress and keeps the best models. In PyCaret, you can use functions like compare_models() and tune_model() for checkpointing. You can also choose where to save models and load them later.
Keep these best practices for model checkpointing in mind as you work:
- Save Frequently: Save models at important points, like after tuning. This helps prevent losing progress if training stops.
- Track Hyperparameters: Keep track of hyperparameters when saving models. You can do this by saving the model’s settings and tuning details.
- Version Control: Organize saved models using version control. PyCaret saves models with timestamps, but you can also organize them by experiment names or version numbers.
By following what we’ve shared in this article, you can keep your models safe and sound while you train, refine, and deploy them.
