Skip to content

Commit 6af6ec8

Browse files
committed
intro, add images
1 parent 939b308 commit 6af6ec8

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: How to use MLflow with Azure Machine Learning service
3+
titleSuffix: Azure Machine Learning service
4+
description: Learn how to log metrics and artifacts using MLflow library to Azure Machine Learning service
5+
services: machine-learning
6+
author: rastala
7+
ms.author: roastala
8+
ms.service: machine-learning
9+
ms.subservice: core
10+
ms.reviewer: nibaccam
11+
ms.topic: conceptual
12+
ms.date: 05/20/2019
13+
ms.custom: seodec18
14+
---
15+
16+
# How to use MLflow with Azure Machine Learning service (Preview)
17+
18+
This article demonstrates how to use MLflow's tracking API, also known as MLflow Tracking, with Azure Machine Learning to track and log your experiment metrics and artifacts in your workspace.
19+
20+
[MLflow](https://www.mlflow.org) is an open-source library for managing the life cycle of your machine learning experiments. [MLFlow Tracking](https://mlflow.org/docs/latest/quickstart.html#using-the-tracking-api) is a component of MLflow that logs and tracks your training run metrics and model artifacts, whether your experiments are run locally, on a virtual machine, or on a remote compute cluster. If you already use MLflow Tracking with your experiments, the [Azure Machine Learning Workspace](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture#workspace) provides a centralized, secure, and scalable location to store your training metrics and models.
21+
22+
![mlflow with azure machine learning diagram](media/how-to-use-mlflow/mlflow-diagram.png)
23+
24+
## Compare MLflow and Azure Machine Learning service clients
25+
26+
The below table summarizes the different clients that can use Azure Machine Learning service, as well as their respective function capabilities.
27+
28+
| | MLflow | Azure Machine Learning <br> Python SDK | Azure Machine Learning <br> CLI | Azure portal|
29+
|-|-|-|-|-|
30+
| Manage workspace | ||||
31+
| Use data stores | ||| |
32+
| Log metrics ||| | |
33+
| Upload artifacts ||| | |
34+
| View metrics |||||
35+
| Manage compute | ||||
36+
| Deploy models | ||||
37+
38+
## Prerequisites
39+
40+
* [Install MLflow.](https://mlflow.org/docs/latest/quickstart.html)
41+
* [Install the Azure Machine Learning Python SDK on your local computer and create an Azure Machine Learning Workspace](setup-create-workspace.md#sdk). The SDK provides the connectivity for MLflow to access your workspace.
42+
43+
## Use MLflow Tracking on local runs
44+
45+
Install the `azureml-contrib-run` package to use MLflow Tracking with Azure Machine Learning on your experiments locally run in a Jupyter Notebook or code editor.
46+
47+
```shell
48+
pip install azureml-contrib-run
49+
```
50+
51+
>[!NOTE]
52+
>The azureml.contrib namespace changes frequently, as we work to improve the service. As such, anything in this namespace should be considered as a preview, and not fully supported by Microsoft.
53+
54+
Import the `mlflow` and [`Workspace`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.workspace(class)?view=azure-ml-py) classes to access MLflow's tracking URI and configure your workspace.
55+
56+
In the following code, the `get_mlflow_tracking_uri()` method assigns a unique tracking URI address to the workspace, `ws`, and `set_tracking_uri()` points the MLflow tracking URI to that address.
57+
58+
```Python
59+
import mlflow
60+
from azureml.core import Workspace
61+
62+
ws = Workspace.from_config()
63+
64+
mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())
65+
```
66+
67+
>[!NOTE]
68+
>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.
69+
70+
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.
71+
72+
```Python
73+
experiment_name = "experiment_with_mlflow"
74+
mlflow.set_experiment(experiment_name)
75+
76+
with mlflow.start_run():
77+
mlflow.log_metric('alpha', 0.03)
78+
```
79+
80+
## Use MLflow Tracking with remote runs
81+
82+
Remote runs let you train your models on more powerful computes, such as GPU enabled virtual machines, or Machine Learning Compute clusters. See [Set up compute targets for model training](how-to-set-up-training-targets.md) to learn about different compute options.
83+
84+
Configure your compute and training run environment with the [`Environment`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py) class. Include `mlflow` and `azure-contrib-run` pip packages in environment's [`CondaDependencies`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.conda_dependencies.condadependencies?view=azure-ml-py) section. Then construct [`ScriptRunConfig`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.script_run_config.scriptrunconfig?view=azure-ml-py) with your remote compute as the compute target.
85+
86+
```Python
87+
88+
from azureml.core import Environment
89+
from azureml.core.conda_dependencies import CondaDependencies
90+
from azureml.core import ScriptRunConfig
91+
92+
exp = Experiment(workspace = "my_workspace",
93+
name = "my_experiment")
94+
95+
mlflow_env = Environment(name="mlflow-env")
96+
97+
cd = CondaDependencies.create(pip_packages = ["mlflow", "azureml-contrib-run"])
98+
99+
mlflow_env.python.conda_dependencies=cd
100+
101+
src = ScriptRunConfig(source_directory="./my_script_location", script="my_training_script.py")
102+
103+
src.run_config.target = "my-remote-compute-compute"
104+
src.run_config.environment = mlflow_env
105+
```
106+
107+
In your training script, import `mlflow` to use the MLflow logging APIs, and start logging your run metrics.
108+
109+
```Python
110+
import mlflow
111+
112+
with mlflow.start_run():
113+
mlflow.log_metric("example", 1.23)
114+
```
115+
116+
With this compute and training run configuration, use the `Experiment.submit("train.py")` method to submit a run. This automatically sets the MLflow tracking URI and directs the logging from MLflow to your Workspace.
117+
118+
```Python
119+
run = exp.submit(src)
120+
```
121+
122+
## View metrics and artifacts in your workspace
123+
124+
The metrics and artifacts from MLflow logging are kept in your workspace on the [Azure portal](https://portal.azure.com). To view them any time, navigate to your workspace and find the experiment by name.
125+
126+
## Clean up resources
127+
128+
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:
129+
130+
1. In the Azure portal, select **Resource groups** on the far left.
131+
132+
![Delete in the Azure portal](media/how-to-use-mlflow/delete-resources.png)
133+
134+
1. From the list, select the resource group you created.
135+
136+
1. Select **Delete resource group**.
137+
138+
1. Enter the resource group name. Then select **Delete**.
139+
140+
## Example notebooks
141+
142+
The [MLflow with Azure ML notebooks](https://github.com/Azure/MachineLearningNotebooks/blob/master/contrib/mlflow) demonstrates concepts in this article.
143+
144+
## Next steps
145+
146+
* [How to deploy a model](how-to-deploy-and-where.md).
124 KB
Loading

0 commit comments

Comments
 (0)