Skip to content

Files

202 lines (140 loc) · 9.33 KB

how-to-train-mlflow-projects.md

File metadata and controls

202 lines (140 loc) · 9.33 KB
title titleSuffix description services author ms.author ms.service ms.subservice ms.date ms.topic ms.custom
Train with MLflow Projects
Azure Machine Learning
Set up MLflow with Azure Machine Learning to log metrics and artifacts from ML models
machine-learning
blackmist
larryfr
machine-learning
core
06/16/2021
conceptual
how-to, devx-track-python, sdkv1, event-tier1-build-2022

Train ML models with MLflow Projects and Azure Machine Learning (preview)

[!INCLUDE sdk v1]

[!INCLUDE preview disclaimer]

In this article, learn how to enable MLflow's tracking URI and logging API, collectively known as MLflow Tracking, to submit training jobs with MLflow Projects and 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.

MLflow Projects allow for you to organize and describe your code to let other data scientists (or automated tools) run it. MLflow Projects with Azure Machine Learning enable you to track and manage your training runs in your workspace.

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.

Learn more about the MLflow and Azure Machine Learning integration..

Tip

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.

Prerequisites

Train MLflow Projects on local compute

This example shows how to submit MLflow projects locally with Azure Machine Learning tracking.

Install the azureml-mlflow package to use MLflow Tracking with Azure Machine Learning on your experiments locally. Your experiments can run via a Jupyter Notebook or code editor.

pip install azureml-mlflow

Import the mlflow and Workspace classes to access MLflow's tracking URI and configure your workspace.

import mlflow
from azureml.core import Workspace

ws = Workspace.from_config()

mlflow.set_tracking_uri(ws.get_mlflow_tracking_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-projects'
mlflow.set_experiment(experiment_name)

Create the backend configuration object to store necessary information for the integration such as, the compute target and which type of managed environment to use.

backend_config = {"USE_CONDA": False}

Add the azureml-mlflow package as a pip dependency to your environment configuration file in order to track metrics and key artifacts in your workspace.

name: mlflow-example
channels:
  - defaults
  - anaconda
  - conda-forge
dependencies:
  - python=3.6
  - scikit-learn=0.19.1
  - pip
  - pip:
    - mlflow
    - azureml-mlflow

Submit the local run and ensure you set the parameter backend = "azureml" . With this setting, you can submit runs locally and get the added support of automatic output tracking, log files, snapshots, and printed errors in your workspace.

View your runs and metrics in the Azure Machine Learning studio.

local_env_run = mlflow.projects.run(uri=".", 
                                    parameters={"alpha":0.3},
                                    backend = "azureml",
                                    use_conda=False,
                                    backend_config = backend_config, 
                                    )

Train MLflow projects with remote compute

This example shows how to submit MLflow projects on a remote compute with Azure Machine Learning tracking.

Install the azureml-mlflow package to use MLflow Tracking with Azure Machine Learning on your experiments locally. Your experiments can run via a Jupyter Notebook or code editor.

pip install azureml-mlflow

Import the mlflow and Workspace classes to access MLflow's tracking URI and configure your workspace.

import mlflow
from azureml.core import Workspace

ws = Workspace.from_config()

mlflow.set_tracking_uri(ws.get_mlflow_tracking_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 = 'train-mlflow-project-amlcompute'
mlflow.set_experiment(experiment_name)

Create the backend configuration object to store necessary information for the integration such as, the compute target and which type of managed environment to use.

The integration accepts "COMPUTE" and "USE_CONDA" as parameters where "COMPUTE" is set to the name of your remote compute cluster and "USE_CONDA" which creates a new environment for the project from the environment configuration file. If "COMPUTE" is present in the object, the project will be automatically submitted to the remote compute and ignore "USE_CONDA". MLflow accepts a dictionary object or a JSON file.

# dictionary
backend_config = {"COMPUTE": "cpu-cluster", "USE_CONDA": False}

Add the azureml-mlflow package as a pip dependency to your environment configuration file in order to track metrics and key artifacts in your workspace.

name: mlflow-example
channels:
  - defaults
  - anaconda
  - conda-forge
dependencies:
  - python=3.6
  - scikit-learn=0.19.1
  - pip
  - pip:
    - mlflow
    - azureml-mlflow

Submit the mlflow project run and ensure you set the parameter backend = "azureml" . With this setting, you can submit your run to your remote compute and get the added support of automatic output tracking, log files, snapshots, and printed errors in your workspace.

View your runs and metrics in the Azure Machine Learning studio.

remote_mlflow_run = mlflow.projects.run(uri=".", 
                                    parameters={"alpha":0.3},
                                    backend = "azureml",
                                    backend_config = backend_config, 
                                    )

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:

  1. In the Azure portal, select Resource groups on the far left.

    Delete in the Azure portal

  2. From the list, select the resource group you created.

  3. Select Delete resource group.

  4. Enter the resource group name. Then select Delete.

Example notebooks

The MLflow with Azure ML notebooks demonstrate and expand upon concepts presented in this article.

Note

A community-driven repository of examples using mlflow can be found at https://github.com/Azure/azureml-examples.

Next steps