How to Register a Model in Azure Machine Learning
Registering your trained machine learning models in Azure Machine Learning is a crucial step for managing, versioning, and deploying them. This document guides you through the process using both the Azure Machine Learning studio and the Python SDK.
Why Register a Model?
- Versioning: Keep track of different versions of your model.
- Centralized Management: A single place to view and manage all your registered models.
- Deployment: Facilitates the deployment of models to various targets (e.g., Azure Kubernetes Service, Azure Container Instances).
- Reproducibility: Enables easy reproduction of results by referencing specific model versions.
Method 1: Using Azure Machine Learning Studio
The Azure Machine Learning studio provides a user-friendly graphical interface for registering models.
Steps:
Navigate to the Models section: In your Azure Machine Learning workspace, go to the left-hand navigation pane and select "Models".
Initiate Registration: Click on the "+ Register model" button.
Choose Source: Select the source of your model. You can choose from:
- From local files: Upload a model file from your computer.
- From Datastore: Select a model file already stored in your Azure ML datastore.
- From run: Link the model to a specific run of an experiment. This is the recommended approach as it automatically associates the model with its training run, including metrics, parameters, and artifacts.
Provide Model Details:
- Name: A unique name for your model.
- Version: Assign a version number (e.g., 1, 2, or semantic versions like 1.0.0). If you don't specify, Azure ML will auto-increment it.
- Description: (Optional) A brief description of the model.
- Tags: (Optional) Key-value pairs for categorizing your model.
- Framework: Select the ML framework used (e.g., TensorFlow, PyTorch, Scikit-learn, ONNX).
Select Model File(s): Depending on your source selection, you'll be prompted to select the model file(s) or the run and artifact path.
Register: Click the "Register" button to finalize the process.
Method 2: Using the Azure Machine Learning Python SDK
The Python SDK offers programmatic control for registering models, which is ideal for automated MLOps pipelines.
Prerequisites:
- Install the Azure Machine Learning SDK:
pip install azure-ai-ml - An authenticated Azure ML client object.
Code Example:
from azure.ai.ml import MLClient
from azure.ai.ml.entities import Model
from azure.identity import DefaultAzureCredential
# Authenticate and get ML client
ml_client = MLClient(
credential=DefaultAzureCredential(),
subscription_id="YOUR_SUBSCRIPTION_ID",
resource_group_name="YOUR_RESOURCE_GROUP",
workspace_name="YOUR_WORKSPACE_NAME",
)
# --- Option 1: Register from a local file ---
local_model_path = "path/to/your/local/model.pkl"
model_name = "my-local-model"
model_version = "1"
model_local = Model(
path=local_model_path,
name=model_name,
version=model_version,
description="A scikit-learn model registered from local file.",
tags={"data": "iris", "framework": "sklearn"}
)
registered_model_local = ml_client.models.create_or_update(model_local)
print(f"Model registered: {registered_model_local.name} (Version: {registered_model_local.version})")
# --- Option 2: Register from a datastore ---
# Assumes you have a datastore named 'workspaceblobstore' and a model file 'model.pt' inside a folder 'models/pytorch_model'
datastore_model_uri = "azureml://datastores/workspaceblobstore/paths/models/pytorch_model/model.pt"
model_name_ds = "my-datastore-model"
model_version_ds = "1.0"
model_datastore = Model(
path=datastore_model_uri,
name=model_name_ds,
version=model_version_ds,
description="A PyTorch model registered from datastore.",
type="custom", # Specify the model type if known, or use 'mlflow_model' etc.
tags={"framework": "pytorch"}
)
registered_model_datastore = ml_client.models.create_or_update(model_datastore)
print(f"Model registered: {registered_model_datastore.name} (Version: {registered_model_datastore.version})")
# --- Option 3: Register from a run (Recommended) ---
# Assumes you have a previous run ID and the model artifact is logged under 'outputs/model'
run_id = "YOUR_PREVIOUS_RUN_ID"
model_name_run = "my-run-model"
model_version_run = "v2"
model_from_run = Model(
path=f"azureml://runs/{run_id}/outputs/model/", # Path to the model artifact in the run
name=model_name_run,
version=model_version_run,
description="A model registered from a specific experiment run.",
tags={"mlflow_model_type": "sklearn"} # Example tag
)
registered_model_run = ml_client.models.create_or_update(model_from_run)
print(f"Model registered: {registered_model_run.name} (Version: {registered_model_run.version})")
YOUR_SUBSCRIPTION_ID, YOUR_RESOURCE_GROUP, YOUR_WORKSPACE_NAME, and YOUR_PREVIOUS_RUN_ID with your actual Azure resource details. Ensure the paths to your models and datastores are correct.
Model Registration Parameters
| Parameter | Description | Required |
|---|---|---|
path |
The URI to the model file or directory. Can be a local path, a datastore path (azureml://datastores/...), or a run output path (azureml://runs/...). |
Yes |
name |
The name of the model. | Yes |
version |
The version of the model. If not provided, Azure ML will auto-generate one. | No |
description |
A text description of the model. | No |
tags |
Key-value pairs for categorizing and searching models. | No |
type |
The type of model (e.g., mlflow_model, pytorch, tensorflow, onnx, custom). This can help with deployment. |
No |
Next Steps
Once your model is registered, you can proceed to deploy it as a web service or use it in other Azure ML workflows.