Skip to content

Commit 0574ea6

Browse files
authored
Merge pull request #187580 from AbeOmor/patch-45
Update the MLFLow article to have better details
2 parents c266981 + 17d17da commit 0574ea6

File tree

1 file changed

+153
-27
lines changed

1 file changed

+153
-27
lines changed

articles/machine-learning/how-to-use-mlflow.md

Lines changed: 153 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ The following diagram illustrates that with MLflow Tracking, you track an experi
5050

5151
## Track local runs
5252

53-
MLflow Tracking with Azure Machine Learning lets you store the logged metrics and artifacts from your local runs into your Azure Machine Learning workspace.
53+
MLflow Tracking with Azure Machine Learning lets you store the logged metrics and artifacts runs that were executed on your local machine into your Azure Machine Learning workspace.
54+
55+
### Set up tracking environment
56+
57+
To track a local run, you need to point your local machine to the Azure Machine Learning MLflow Tracking URI.
5458

5559
Import the `mlflow` and [`Workspace`](/python/api/azureml-core/azureml.core.workspace%28class%29) classes to access MLflow's tracking URI and configure your workspace.
5660

@@ -65,14 +69,30 @@ ws = Workspace.from_config()
6569
mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())
6670
```
6771

68-
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+
### Set experiment name
73+
74+
All MLflow runs are logged to the active experiment, which can be set with the MLflow SDK or Azure CLI.
75+
76+
Set the MLflow experiment name with [`set_experiment()`](https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.set_experiment) command.
6977

7078
```Python
7179
experiment_name = 'experiment_with_mlflow'
7280
mlflow.set_experiment(experiment_name)
81+
```
82+
83+
### Start training run
7384

74-
with mlflow.start_run():
75-
mlflow.log_metric('alpha', 0.03)
85+
After you set the MLflow experiment name, you can start your training run with `start_run()`. Then use `log_metric()` to activate the MLflow logging API and begin logging your training run metrics.
86+
87+
```Python
88+
import os
89+
from random import random
90+
91+
with mlflow.start_run() as mlflow_run:
92+
mlflow.log_param("hello_param", "world")
93+
mlflow.log_metric("hello_metric", random())
94+
os.system(f"echo 'hello world' > helloworld.txt")
95+
mlflow.log_artifact("helloworld.txt")
7696
```
7797

7898
## Track remote runs
@@ -81,45 +101,149 @@ Remote runs let you train your models on more powerful computes, such as GPU ena
81101

82102
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.
83103

84-
The following example conda environment includes `mlflow` and `azureml-mlflow` as pip packages.
104+
First, you should create a `src` subdirectory and create a file with your training code in a `train.py` file in the `src` subdirectory. All your training code will go into the `src` subdirectory, including `train.py`.
105+
106+
The training code is taken from this [MLflow example](https://github.com/Azure/azureml-examples/blob/main/cli/jobs/basics/src/hello-mlflow.py) in the Azure Machine Learning example repo.
107+
108+
Copy this code into the file:
109+
110+
```Python
111+
# imports
112+
import os
113+
import mlflow
114+
115+
from random import random
116+
117+
# define functions
118+
def main():
119+
mlflow.log_param("hello_param", "world")
120+
mlflow.log_metric("hello_metric", random())
121+
os.system(f"echo 'hello world' > helloworld.txt")
122+
mlflow.log_artifact("helloworld.txt")
85123

86124

87-
```yaml
88-
name: sklearn-example
89-
dependencies:
90-
- python=3.6.2
91-
- scikit-learn
92-
- matplotlib
93-
- numpy
94-
- pip:
95-
- azureml-mlflow
96-
- mlflow
97-
- numpy
125+
# run functions
126+
if __name__ == "__main__":
127+
# run main function
128+
main()
98129
```
99130

100-
In your script, configure your compute and training run environment with the [`Environment`](/python/api/azureml-core/azureml.core.environment.environment) class. Then, construct [`ScriptRunConfig`](/python/api/azureml-core/azureml.core.script_run_config.scriptrunconfig) with your remote compute as the compute target.
131+
Load training script to submit an experiement.
101132

102133
```Python
103-
import mlflow
134+
script_dir = "src"
135+
training_script = 'train.py'
136+
with open("{}/{}".format(script_dir,training_script), 'r') as f:
137+
print(f.read())
138+
```
139+
140+
In your script, configure your compute and training run environment with the [`Environment`](/python/api/azureml-core/azureml.core.environment.environment) class.
141+
142+
```Python
143+
from azureml.core import Environment
144+
from azureml.core.conda_dependencies import CondaDependencies
145+
146+
env = Environment(name="mlflow-env")
104147

105-
with mlflow.start_run():
106-
mlflow.log_metric('example', 1.23)
148+
# Specify conda dependencies with scikit-learn and temporary pointers to mlflow extensions
149+
cd = CondaDependencies.create(
150+
conda_packages=["scikit-learn", "matplotlib"],
151+
pip_packages=["azureml-mlflow", "pandas", "numpy"]
152+
)
153+
154+
env.python.conda_dependencies = cd
155+
```
156+
157+
Then, construct [`ScriptRunConfig`](/python/api/azureml-core/azureml.core.script_run_config.scriptrunconfig) with your remote compute as the compute target.
158+
159+
```Python
160+
from azureml.core import ScriptRunConfig
161+
162+
src = ScriptRunConfig(source_directory="src",
163+
script=training_script,
164+
compute_target="<COMPUTE_NAME>",
165+
environment=env)
107166
```
108167

109168
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.
110169

111170
```Python
171+
from azureml.core import Experiment
172+
from azureml.core import Workspace
173+
ws = Workspace.from_config()
174+
175+
experiment_name = "experiment_with_mlflow"
176+
exp = Experiment(workspace=ws, name=experiment_name)
177+
112178
run = exp.submit(src)
113179
```
114180

115181
## View metrics and artifacts in your workspace
116182

117-
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](https://ml.azure.com). Or run the below code.
183+
The metrics and artifacts from MLflow logging are tracked 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](https://ml.azure.com). Or run the below code.
184+
185+
Retrieve run metric using MLflow [get_run()](https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.get_run).
186+
187+
```Python
188+
from mlflow.entities import ViewType
189+
from mlflow.tracking import MlflowClient
190+
191+
# Retrieve run ID for the last run experiement
192+
current_experiment=mlflow.get_experiment_by_name(experiment_name)
193+
runs = mlflow.search_runs(experiment_ids=current_experiment.experiment_id, run_view_type=ViewType.ALL)
194+
run_id = runs.tail(1)["run_id"].tolist()[0]
195+
196+
# Use MlFlow to retrieve the run that was just completed
197+
client = MlflowClient()
198+
finished_mlflow_run = MlflowClient().get_run(run_id)
118199

119-
```python
120-
run.get_metrics()
200+
metrics = finished_mlflow_run.data.metrics
201+
tags = finished_mlflow_run.data.tags
202+
params = finished_mlflow_run.data.params
203+
204+
print(metrics,tags,params)
121205
```
122206

207+
### Retrieve artifacts with MLFLow
208+
209+
To view the artifacts of a run, you can use [MlFlowClient.list_artifacts()](https://mlflow.org/docs/latest/python_api/mlflow.tracking.html#mlflow.tracking.MlflowClient.list_artifacts)
210+
211+
```Python
212+
client.list_artifacts(run_id)
213+
```
214+
215+
To download an artifact to the current directory, you can use [MLFlowClient.download_artifacts()](https://www.mlflow.org/docs/latest/python_api/mlflow.tracking.html#mlflow.tracking.MlflowClient.download_artifacts)
216+
217+
```Python
218+
client.download_artifacts(run_id, "helloworld.txt", ".")
219+
```
220+
221+
### Compare and query
222+
223+
Compare and query all MLflow runs in your Azure Machine Learning workspace with the following code.
224+
[Learn more about how to query runs with MLflow](https://mlflow.org/docs/latest/search-syntax.html#programmatically-searching-runs).
225+
226+
```Python
227+
from mlflow.entities import ViewType
228+
229+
all_experiments = [exp.experiment_id for exp in MlflowClient().list_experiments()]
230+
query = "metrics.hello_metric > 0"
231+
runs = mlflow.search_runs(experiment_ids=all_experiments, filter_string=query, run_view_type=ViewType.ALL)
232+
233+
runs.head(10)
234+
```
235+
236+
## Automatic logging
237+
With Azure Machine Learning and MLFlow, users can log metrics, model parameters and model artifacts automatically when training a model. A [variety of popular machine learning libraries](https://mlflow.org/docs/latest/tracking.html#automatic-logging) are supported.
238+
239+
To enable [automatic logging](https://mlflow.org/docs/latest/tracking.html#automatic-logging) insert the following code before your training code:
240+
241+
```Python
242+
mlflow.autolog()
243+
```
244+
245+
[Learn more about Automatic logging with MLflow](https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.autolog).
246+
123247
## Manage models
124248

125249
Register and track your models with the [Azure Machine Learning model registry](concept-model-management-and-deployment.md#register-package-and-deploy-models-from-anywhere), 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.
@@ -128,11 +252,13 @@ If you want to deploy and register your production ready model in one step, see
128252

129253
To register and view a model from a run, use the following steps:
130254

131-
1. Once the run is complete call the `register_model()` method.
255+
1. Once a run is complete, call the [`register_model()`](https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.register_model) method.
132256

133-
```python
134-
# the model folder produced from the run is registered. This includes the MLmodel file, model.pkl and the conda.yaml.
135-
run.register_model(model_name = 'my-model', model_path = 'model')
257+
```Python
258+
# the model folder produced from a run is registered. This includes the MLmodel file, model.pkl and the conda.yaml.
259+
model_path = "model"
260+
model_uri = 'runs:/{}/{}'.format(run_id, model_path)
261+
mlflow.register_model(model_uri,"registered_model_name")
136262
```
137263

138264
1. View the registered model in your workspace with [Azure Machine Learning studio](overview-what-is-machine-learning-studio.md).

0 commit comments

Comments
 (0)