Azure ML MLflow Integration

1.62.0.post2 · active · verified Sat Apr 11

The `azureml-mlflow` package provides integration code for connecting MLflow with Azure Machine Learning. It enables users to leverage MLflow's open-source capabilities for tracking machine learning experiments, logging metrics, parameters, and artifacts, and managing models directly within an Azure Machine Learning workspace. This facilitates centralized, secure, and scalable storage for MLflow-tracked assets, whether experiments run locally or on Azure compute. The library is actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure MLflow to track experiments in an Azure Machine Learning workspace from a Python script. It sets the MLflow tracking URI to your Azure ML workspace and then logs a simple parameter and metric. Ensure you have `azure-ai-ml` and `azure-identity` installed and are authenticated to Azure (e.g., via `az login`).

import os
import mlflow
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

# Replace with your Azure subscription, resource group, and workspace name
# For local execution, ensure you are logged into Azure CLI or have appropriate env vars set
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
resource_group = os.environ.get('AZURE_RESOURCE_GROUP', 'YOUR_RESOURCE_GROUP')
workspace_name = os.environ.get('AZURE_ML_WORKSPACE_NAME', 'YOUR_WORKSPACE_NAME')

try:
    # Connect to Azure ML Workspace
    ml_client = MLClient(
        credential=DefaultAzureCredential(),
        subscription_id=subscription_id,
        resource_group_name=resource_group,
        workspace_name=workspace_name
    )
    azureml_tracking_uri = ml_client.workspaces.get(ml_client.workspace_name).mlflow_tracking_uri
    mlflow.set_tracking_uri(azureml_tracking_uri)
    print(f"MLflow tracking URI set to: {mlflow.get_tracking_uri()}")

    # Start an MLflow run and log a metric
    with mlflow.start_run(run_name="quickstart_run") as run:
        mlflow.log_param("alpha", 0.5)
        mlflow.log_metric("accuracy", 0.95)
        print(f"Logged metric 'accuracy' in run: {run.info.run_id}")

    print("MLflow run completed. Check Azure ML Studio -> Jobs for details.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure Azure credentials are configured (e.g., via Azure CLI 'az login')")
    print("and that the subscription, resource group, and workspace names are correct.")

view raw JSON →