How to Use Model Registry for Deployment in Machine Learning
Use a
model registry to store, version, and manage machine learning models centrally. For deployment, retrieve the desired model version from the registry and load it into your serving environment to ensure consistent and trackable model usage.Syntax
The typical syntax for using a model registry involves registering a model, retrieving it by name and version, and then loading it for deployment.
register_model(model_uri, name): Save the model with a unique name.get_model(name, version): Retrieve the model from the registry.load_model(model_uri): Load the model into memory for inference or deployment.
python
from mlflow import register_model from mlflow.pyfunc import load_model # Register a model register_model(model_uri='runs:/1234567890abcdef/model', name='my_model') # Retrieve model URI model_uri = f'models:/my_model/1' # Load model for deployment model = load_model(model_uri=model_uri)
Example
This example shows how to register a simple scikit-learn model, then load it from the model registry for deployment.
python
import mlflow import mlflow.sklearn from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split # Load data iris = load_iris() X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42) # Train model model = RandomForestClassifier() model.fit(X_train, y_train) # Start MLflow run with mlflow.start_run() as run: # Log model mlflow.sklearn.log_model(model, artifact_path="model") run_id = run.info.run_id # Register the model model_uri = f"runs:/{run_id}/model" model_name = "IrisRFModel" mlflow.register_model(model_uri, model_name) # Load the registered model for deployment from mlflow.pyfunc import load_model loaded_model = load_model(f"models:/{model_name}/1") # Predict with loaded model predictions = loaded_model.predict(X_test) print(predictions[:5])
Output
[1 0 2 1 1]
Common Pitfalls
Common mistakes when using a model registry for deployment include:
- Not specifying the correct model version, leading to loading the wrong model.
- Failing to register the model after training, so the deployment environment cannot find it.
- Using inconsistent model URIs or names, causing errors when loading.
- Not handling model stage transitions (e.g., from
StagingtoProduction), which can cause deploying untested models.
python
## Wrong way: Loading without version # model = load_model(f"models:/IrisRFModel") # Missing version causes error ## Right way: Specify version explicitly # model = load_model(f"models:/IrisRFModel/1")
Quick Reference
| Action | Command/Method | Description |
|---|---|---|
| Register Model | mlflow.register_model(model_uri, name) | Save model with a name and version in registry |
| Load Model | mlflow.pyfunc.load_model('models:/name/version') | Load model for inference or deployment |
| List Models | mlflow.client().list_registered_models() | View all registered models |
| Transition Stage | mlflow.client().transition_model_version_stage() | Change model stage (e.g., Staging to Production) |
Key Takeaways
Always register your trained model with a unique name and version in the model registry.
Load models for deployment using the exact model name and version to avoid errors.
Manage model stages (Staging, Production) to control deployment readiness.
Use the model registry to track and reproduce deployed models easily.
Avoid loading models without specifying versions to prevent unexpected behavior.