Model saving, loading, and prediction: Worked Example — Introductory MLOps Practices (NVIDIA-Certified Associate: Accelerated Data Science)
Model Saving, Loading, and Prediction: A Worked Example for Accelerated Data Science In the context of MLOps practices for the NVIDIA-Certified...
Model Saving, Loading, and Prediction: A Worked Example for Accelerated Data Science
In the context of MLOps practices for the NVIDIA-Certified Associate: Accelerated Data Science certification, managing models efficiently is crucial. This includes saving trained models, loading them for inference, and performing predictions in production or testing environments. Below is a detailed, step-by-step worked example demonstrating these tasks using a realistic scenario.
Scenario
You have trained a GPU-accelerated machine learning model using scikit-learn on a dataset to predict customer churn. After training, you want to save the model, load it later for inference, and run predictions on new customer data.
Step 1: Train the Model
Assume you have already trained a model. For illustration:
from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=20, random_state=42) model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X, y)
This model is trained on synthetic data representing customer features.
Step 2: Save the Model
Use joblib or pickle to serialize the trained model. Joblib is preferred for large numpy arrays common in models.
import joblib
model_filename = 'customer_churn_model.joblib' joblib.dump(model, model_filename)
This step creates a file customer_churn_model.joblib that stores the model state.
Step 3: Load the Model
When you need to perform predictions later, load the model from disk:
loaded_model = joblib.load(model_filename)
This restores the model to the exact state it was when saved.
Step 4: Prepare New Data for Prediction
Prepare new customer data in the same feature format as training data. For example:
import numpy as np
new_customer = np.array([[0.5, -1.2, 0.3, 0.7, -0.8, 1.1, 0.0, 0.2, -0.5, 1.3, 0.4, -0.6, 0.9, -1.0, 0.1, 0.3, -0.7, 0.8, 0.6, -0.2]])
Step 5: Predict Using the Loaded Model
Run prediction on the new data:
prediction = loaded_model.predict(new_customer) print(f'Predicted churn class: {prediction[0]}')
This outputs the predicted class (e.g., 0 for no churn, 1 for churn).
Step 6: Save and Load Model with GPU-Accelerated Frameworks (Optional)
If using GPU-accelerated libraries such as cuML from RAPIDS, the saving and loading process is similar but may require specific serialization methods. For example, cuML models can be saved using pickle or their own save_model methods.
Summary
- Saving: Serialize the trained model to disk using joblib or pickle.
- Loading: Deserialize the model when needed for inference.
- Prediction: Use the loaded model to predict on new data formatted consistently with training data.
This workflow ensures reproducibility and efficient deployment of GPU-accelerated machine learning models, a foundational MLOps practice for the NVIDIA certification.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →