|
| 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: 06/10/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 URI and logging API, collectively also known as MLflow Tracking, with Azure Machine Learning service to track and log your experiment metrics and artifacts in your [Azure Machine Learning service workspace](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture#workspace). If you already use MLflow Tracking for your experiments, the workspace provides a centralized, secure, and scalable location to store your training metrics and models. |
| 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. |
| 21 | + |
| 22 | + |
| 23 | +## Compare MLflow and Azure Machine Learning service clients |
| 24 | + |
| 25 | + The below table summarizes the different clients that can use Azure Machine Learning service, and their respective function capabilities. |
| 26 | + |
| 27 | + MLflow Tracking offers metric logging and artifact storage functionalities that are only otherwise available via the [Azure Machine Learning Python SDK](https://docs.microsoft.com/python/api/overview/azure/ml/intro?view=azure-ml-py). |
| 28 | + |
| 29 | +| | MLflow Tracking | Azure Machine Learning <br> Python SDK | Azure Machine Learning <br> CLI | Azure portal| |
| 30 | +|-|-|-|-|-| |
| 31 | +| Manage workspace | | ✓ | ✓ | ✓ | |
| 32 | +| Use data stores | | ✓ | ✓ | | |
| 33 | +| Log metrics | ✓ | ✓ | | | |
| 34 | +| Upload artifacts | ✓ | ✓ | | | |
| 35 | +| View metrics | ✓ | ✓ | ✓ | ✓ | |
| 36 | +| Manage compute | | ✓ | ✓ | ✓ | |
| 37 | +| Deploy models | | ✓ | ✓ | ✓ | |
| 38 | + |
| 39 | +## Prerequisites |
| 40 | + |
| 41 | +* [Install MLflow.](https://mlflow.org/docs/latest/quickstart.html) |
| 42 | +* [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. |
| 43 | + |
| 44 | +## Track local runs |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +```shell |
| 49 | +pip install azureml-contrib-run |
| 50 | +``` |
| 51 | + |
| 52 | +>[!NOTE] |
| 53 | +>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. |
| 54 | +
|
| 55 | +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. |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | +```Python |
| 60 | +import mlflow |
| 61 | +from azureml.core import Workspace |
| 62 | + |
| 63 | +ws = Workspace.from_config() |
| 64 | + |
| 65 | +mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri()) |
| 66 | +``` |
| 67 | + |
| 68 | +>[!NOTE] |
| 69 | +>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. |
| 70 | +
|
| 71 | +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. |
| 72 | + |
| 73 | +```Python |
| 74 | +experiment_name = "experiment_with_mlflow" |
| 75 | +mlflow.set_experiment(experiment_name) |
| 76 | + |
| 77 | +with mlflow.start_run(): |
| 78 | + mlflow.log_metric('alpha', 0.03) |
| 79 | +``` |
| 80 | + |
| 81 | +## Track remote runs |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +```Python |
| 88 | + |
| 89 | +from azureml.core import Environment |
| 90 | +from azureml.core.conda_dependencies import CondaDependencies |
| 91 | +from azureml.core import ScriptRunConfig |
| 92 | + |
| 93 | +exp = Experiment(workspace = "my_workspace", |
| 94 | + name = "my_experiment") |
| 95 | + |
| 96 | +mlflow_env = Environment(name="mlflow-env") |
| 97 | + |
| 98 | +cd = CondaDependencies.create(pip_packages = ["mlflow", "azureml-contrib-run"]) |
| 99 | + |
| 100 | +mlflow_env.python.conda_dependencies=cd |
| 101 | + |
| 102 | +src = ScriptRunConfig(source_directory="./my_script_location", script="my_training_script.py") |
| 103 | + |
| 104 | +src.run_config.target = "my-remote-compute-compute" |
| 105 | +src.run_config.environment = mlflow_env |
| 106 | +``` |
| 107 | + |
| 108 | +In your training script, import `mlflow` to use the MLflow logging APIs, and start logging your run metrics. |
| 109 | + |
| 110 | +```Python |
| 111 | +import mlflow |
| 112 | + |
| 113 | +with mlflow.start_run(): |
| 114 | + mlflow.log_metric("example", 1.23) |
| 115 | +``` |
| 116 | + |
| 117 | +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. |
| 118 | + |
| 119 | +```Python |
| 120 | +run = exp.submit(src) |
| 121 | +``` |
| 122 | + |
| 123 | +## View metrics and artifacts in your workspace |
| 124 | + |
| 125 | +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. |
| 126 | + |
| 127 | +## Clean up resources |
| 128 | + |
| 129 | +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: |
| 130 | + |
| 131 | +1. In the Azure portal, select **Resource groups** on the far left. |
| 132 | + |
| 133 | +  |
| 134 | + |
| 135 | +1. From the list, select the resource group you created. |
| 136 | + |
| 137 | +1. Select **Delete resource group**. |
| 138 | + |
| 139 | +1. Enter the resource group name. Then select **Delete**. |
| 140 | + |
| 141 | +## Example notebooks |
| 142 | + |
| 143 | +The [MLflow with Azure ML notebooks](https://github.com/Azure/MachineLearningNotebooks/blob/master/contrib/mlflow) demonstrates concepts in this article. |
| 144 | + |
| 145 | +## Next steps |
| 146 | + |
| 147 | +* [How to deploy a model](how-to-deploy-and-where.md). |
0 commit comments