Track ML models with MLflow and Azure Machine Learning
In this article, learn how to enable MLflow's tracking URI and logging API, collectively known as MLflow Tracking, to connect Azure Machine Learning as the backend of your MLflow experiments.
Supported capabilities include:
Track and log experiment metrics and artifacts in your Azure Machine Learning workspace. If you already use MLflow Tracking for your experiments, the workspace provides a centralized, secure, and scalable location to store training metrics and models.
Submit training jobs with MLflow Projects with Azure Machine Learning backend support. You can submit jobs locally with Azure Machine Learning tracking or migrate your runs to the cloud like via an Azure Machine Learning Compute.
Track and manage models in MLflow and Azure Machine Learning model registry.
MLflow is an open-source library for managing the life cycle of your machine learning experiments. MLFlow Tracking is a component of MLflow that logs and tracks your training run metrics and model artifacts, no matter your experiment's environment--locally on your computer, on a remote compute target, a virtual machine, or an Azure Databricks cluster.
See MLflow and Azure Machine Learning for additional MLflow and Azure Machine Learning functionality integrations.
The following diagram illustrates that with MLflow Tracking, you track an experiment's run metrics and store model artifacts in your Azure Machine Learning workspace.
The information in this document is primarily for data scientists and developers who want to monitor the model training process. If you are an administrator interested in monitoring resource usage and events from Azure Machine Learning, such as quotas, completed training runs, or completed model deployments, see Monitoring Azure Machine Learning.
You can use the MLflow Skinny client which is a lightweight MLflow package without SQL storage, server, UI, or data science dependencies. This is recommended for users who primarily need the tracking and logging capabilities without importing the full suite of MLflow features including deployments.
- Install the
- This package automatically brings in
azureml-coreof the The Azure Machine Learning Python SDK, which provides the connectivity for MLflow to access your workspace.
- This package automatically brings in
- Create an Azure Machine Learning Workspace.
Track local runs
MLflow Tracking with Azure Machine Learning lets you store the logged metrics and artifacts from your local runs into your Azure Machine Learning workspace.
Workspace classes to access MLflow's tracking URI and configure your workspace.
In the following code, the
get_mlflow_tracking_uri() method assigns a unique tracking URI address to the workspace,
set_tracking_uri() points the MLflow tracking URI to that address.
import mlflow from azureml.core import Workspace ws = Workspace.from_config() mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())
The tracking URI is valid up to an hour or less. If you restart your script after some idle time, use the get_mlflow_tracking_uri API to get a new URI.
Set the MLflow experiment name with
set_experiment() and start your training run with
start_run(). Then use
log_metric() to activate the MLflow logging API and begin logging your training run metrics.
experiment_name = 'experiment_with_mlflow' mlflow.set_experiment(experiment_name) with mlflow.start_run(): mlflow.log_metric('alpha', 0.03)
Track remote runs
Remote runs let you train your models on more powerful computes, such as GPU enabled virtual machines, or Machine Learning Compute clusters. See Use compute targets for model training to learn about different compute options.
MLflow Tracking with Azure Machine Learning lets you store the logged metrics and artifacts from your remote runs into your Azure Machine Learning workspace. Any run with MLflow Tracking code in it will have metrics logged automatically to the workspace.
The following example conda environment includes
azureml-mlflow as pip packages.
name: sklearn-example dependencies: - python=3.6.2 - scikit-learn - matplotlib - numpy - pip: - azureml-mlflow - mlflow - numpy
import mlflow with mlflow.start_run(): mlflow.log_metric('example', 1.23)
With this compute and training run configuration, use the
Experiment.submit() method to submit a run. This method automatically sets the MLflow tracking URI and directs the logging from MLflow to your Workspace.
run = exp.submit(src)
View metrics and artifacts in your workspace
The metrics and artifacts from MLflow logging are kept in your workspace. To view them anytime, navigate to your workspace and find the experiment by name in your workspace in Azure Machine Learning studio. Or run the below code.
Register and track your models with the Azure Machine Learning model registry which supports the MLflow model registry. Azure Machine Learning models are aligned with the MLflow model schema making it easy to export and import these models across different workflows. The MLflow related metadata such as, run ID is also tagged with the registered model for traceability. Users can submit training runs, register, and deploy models produced from MLflow runs.
If you want to deploy and register your production ready model in one step, see Deploy and register MLflow models.
To register and view a model from a run, use the following steps:
Once the run is complete call the
# the model folder produced from the run is registered. This includes the MLmodel file, model.pkl and the conda.yaml. run.register_model(model_name = 'my-model', model_path = 'model')
View the registered model in your workspace with Azure Machine Learning studio.
In the following example the registered model,
my-modelhas MLflow tracking metadata tagged.
Select the Artifacts tab to see all the model files that align with the MLflow model schema (conda.yaml, MLmodel, model.pkl).
Select MLmodel to see the MLmodel file generated by the run.
Clean up resources
If you don't plan to use the logged metrics and artifacts in your workspace, the ability to delete them individually is currently unavailable. Instead, delete the resource group that contains the storage account and workspace, so you don't incur any charges:
In the Azure portal, select Resource groups on the far left.
From the list, select the resource group you created.
Select Delete resource group.
Enter the resource group name. Then select Delete.
The MLflow with Azure ML notebooks demonstrate and expand upon concepts presented in this article.
A community-driven repository of examples using mlflow can be found at https://github.com/Azure/azureml-examples.