Skip to content

Commit 11e4679

Browse files
authored
Merge pull request #223176 from santiagxf/santiagxf/mlflow-2.0-deprecation-exp
Update how-to-track-experiments-mlflow.md
2 parents 1788d15 + aed3880 commit 11e4679

File tree

1 file changed

+97
-83
lines changed

1 file changed

+97
-83
lines changed

articles/machine-learning/how-to-track-experiments-mlflow.md

Lines changed: 97 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.custom: how-to, devx-track-python, ignite-2022
1515

1616
# Query & compare experiments and runs with MLflow
1717

18-
Experiments and runs in Azure Machine Learning can be queried using MLflow. This removes the need of any Azure Machine Learning specific SDKs to manage anything that happens inside of a training job, allowing dependencies removal and creating a more seamless transition between local runs and cloud.
18+
Experiments and runs tracking information in Azure Machine Learning can be queried using MLflow. You don't need to install any specific SDK to manage what happens inside of a training job, creating a more seamless transition between local runs and the cloud by removing cloud-specific dependencies.
1919

2020
> [!NOTE]
2121
> The Azure Machine Learning Python SDK v2 does not provide native logging or tracking capabilities. This applies not just for logging but also for querying the metrics logged. Instead, we recommend to use MLflow to manage experiments and runs. This article explains how to use MLflow to manage experiments and runs in Azure ML.
@@ -40,30 +40,33 @@ Use MLflow to query and manage all the experiments in Azure Machine Learning. Th
4040

4141
You can get all the active experiments in the workspace using MLFlow:
4242

43-
```python
44-
experiments = mlflow.list_experiments()
45-
for exp in experiments:
46-
print(exp.name)
47-
```
43+
```python
44+
experiments = mlflow.search_experiments()
45+
for exp in experiments:
46+
print(exp.name)
47+
```
48+
49+
> [!NOTE]
50+
> __MLflow 2.0 advisory:__ In legacy versions of MLflow (<2.0) use method `list_experiments` instead.
4851
4952
If you want to retrieve archived experiments too, then include the option `ViewType.ALL` in the `view_type` argument. The following sample shows how:
5053

51-
```python
52-
from mlflow.entities import ViewType
54+
```python
55+
from mlflow.entities import ViewType
5356

54-
experiments = mlflow.list_experiments(view_type=ViewType.ALL)
55-
for exp in experiments:
56-
print(exp.name)
57-
```
57+
experiments = mlflow.search_experiments(view_type=ViewType.ALL)
58+
for exp in experiments:
59+
print(exp.name)
60+
```
5861

5962
## Getting a specific experiment
6063

6164
Details about a specific experiment can be retrieved using the `get_experiment_by_name` method:
6265

63-
```python
64-
exp = mlflow.get_experiment_by_name(experiment_name)
65-
print(exp)
66-
```
66+
```python
67+
exp = mlflow.get_experiment_by_name(experiment_name)
68+
print(exp)
69+
```
6770

6871
## Getting runs inside an experiment
6972

@@ -77,14 +80,15 @@ MLflow allows searching runs inside of any experiment, including multiple experi
7780

7881
By experiment name:
7982

80-
```python
81-
mlflow.search_runs(experiment_names=[ "my_experiment" ])
82-
```
83+
```python
84+
mlflow.search_runs(experiment_names=[ "my_experiment" ])
85+
```
86+
8387
By experiment ID:
8488

85-
```python
86-
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ])
87-
```
89+
```python
90+
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ])
91+
```
8892

8993
> [!TIP]
9094
> Notice that `experiment_ids` supports providing an array of experiments, so you can search runs across multiple experiments if required. This may be useful in case you want to compare runs of the same model when it is being logged in different experiments (by different people, different project iterations, etc). You can also use `search_all_experiments=True` if you want to search across all the experiments in the workspace.
@@ -95,33 +99,33 @@ Another important point to notice is that get returning runs, all metrics are pa
9599

96100
By default, experiments are ordered descending by `start_time`, which is the time the experiment was queue in Azure ML. However, you can change this default by using the parameter `order_by`.
97101

98-
```python
99-
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ], order_by=["start_time DESC"])
100-
```
102+
```python
103+
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ], order_by=["start_time DESC"])
104+
```
101105

102106
Use the argument `max_results` from `search_runs` to limit the number of runs returned. For instance, the following example returns the last run of the experiment:
103107

104-
```python
105-
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ], max_results=1, order_by=["start_time DESC"])
106-
```
108+
```python
109+
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ], max_results=1, order_by=["start_time DESC"])
110+
```
107111

108112
> [!WARNING]
109113
> Using `order_by` with expressions containing `metrics.*` in the parameter `order_by` is not supported by the moment. Please use `order_values` method from Pandas as shown in the next example.
110114
111115
You can also order by metrics to know which run generated the best results:
112116

113-
```python
114-
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ]).sort_values("metrics.accuracy", ascending=False)
115-
```
117+
```python
118+
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ]).sort_values("metrics.accuracy", ascending=False)
119+
```
116120

117121
### Filtering runs
118122

119123
You can also look for a run with a specific combination in the hyperparameters using the parameter `filter_string`. Use `params` to access run's parameters and `metrics` to access metrics logged in the run. MLflow supports expressions joined by the AND keyword (the syntax does not support OR):
120124

121-
```python
122-
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ],
123-
filter_string="params.num_boost_round='100'")
124-
```
125+
```python
126+
mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ],
127+
filter_string="params.num_boost_round='100'")
128+
```
125129

126130
### Filter runs by status
127131

@@ -140,95 +144,105 @@ You can also filter experiment by status. It becomes useful to find runs that ar
140144
> [!WARNING]
141145
> Expressions containing `attributes.status` in the parameter `filter_string` are not support at the moment. Please use Pandas filtering expressions as shown in the next example.
142146
143-
The following example shows all the runs that have been completed:
147+
The following example shows all the completed runs:
144148

145-
```python
146-
runs = mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ])
147-
runs[runs.status == "FINISHED"]
148-
```
149+
```python
150+
runs = mlflow.search_runs(experiment_ids=[ "1234-5678-90AB-CDEFG" ])
151+
runs[runs.status == "FINISHED"]
152+
```
149153

150154
## Getting metrics, parameters, artifacts and models
151155

152-
By default, MLflow returns runs as a Pandas `Dataframe` containing a limited amount of information. You can get Python objects if needed, which may be useful to get details about them. Use the `output_format` parameter to control how output is returned:
156+
The method `search_runs` returns a Pandas `Dataframe` containing a limited amount of information by default. You can get Python objects if needed, which may be useful to get details about them. Use the `output_format` parameter to control how output is returned:
157+
158+
```python
159+
runs = mlflow.search_runs(
160+
experiment_ids=[ "1234-5678-90AB-CDEFG" ],
161+
filter_string="params.num_boost_round='100'",
162+
output_format="list",
163+
)
164+
```
153165

154-
```python
155-
runs = mlflow.search_runs(
156-
experiment_ids=[ "1234-5678-90AB-CDEFG" ],
157-
filter_string="params.num_boost_round='100'",
158-
output_format="list",
159-
)
160-
```
161166
Details can then be accessed from the `info` member. The following sample shows how to get the `run_id`:
162167

163-
```python
164-
last_run = runs[-1]
165-
print("Last run ID:", last_run.info.run_id)
166-
```
168+
```python
169+
last_run = runs[-1]
170+
print("Last run ID:", last_run.info.run_id)
171+
```
167172

168173
### Getting params and metrics from a run
169174

170175
When runs are returned using `output_format="list"`, you can easily access parameters using the key `data`:
171176

172-
```python
173-
last_run.data.params
174-
```
177+
```python
178+
last_run.data.params
179+
```
175180

176181
In the same way, you can query metrics:
177182

178-
```python
179-
last_run.data.metrics
180-
```
183+
```python
184+
last_run.data.metrics
185+
```
186+
181187
For metrics that contain multiple values (for instance, a loss curve, or a PR curve), only the last logged value of the metric is returned. If you want to retrieve all the values of a given metric, uses `mlflow.get_metric_history` method. This method requires you to use the `MlflowClient`:
182188

183-
```python
184-
client = mlflow.tracking.MlflowClient()
185-
client.get_metric_history("1234-5678-90AB-CDEFG", "log_loss")
186-
```
189+
```python
190+
client = mlflow.tracking.MlflowClient()
191+
client.get_metric_history("1234-5678-90AB-CDEFG", "log_loss")
192+
```
187193

188194
### Getting artifacts from a run
189195

190196
Any artifact logged by a run can be queried by MLflow. Artifacts can't be access using the run object itself and the MLflow client should be used instead:
191197

192-
```python
193-
client = mlflow.tracking.MlflowClient()
194-
client.list_artifacts("1234-5678-90AB-CDEFG")
195-
```
198+
```python
199+
client = mlflow.tracking.MlflowClient()
200+
client.list_artifacts("1234-5678-90AB-CDEFG")
201+
```
196202

197203
The method above will list all the artifacts logged in the run, but they will remain stored in the artifacts store (Azure ML storage). To download any of them, use the method `download_artifact`:
198204

199-
```python
200-
file_path = client.download_artifacts("1234-5678-90AB-CDEFG", path="feature_importance_weight.png")
201-
```
205+
```python
206+
file_path = mlflow.artifacts.download_artifacts(
207+
run_id="1234-5678-90AB-CDEFG", artifact_path="feature_importance_weight.png"
208+
)
209+
```
210+
211+
> [!NOTE]
212+
> __MLflow 2.0 advisory:__ In legacy versions of MLflow (<2.0), use the method `MlflowClient.download_artifacts()` instead.
202213
203214
### Getting models from a run
204215

205216
Models can also be logged in the run and then retrieved directly from it. To retrieve it, you need to know the artifact's path where it is stored. The method `list_artifacats` can be used to find artifacts that are representing a model since MLflow models are always folders. You can download a model by indicating the path where the model is stored using the `download_artifact` method:
206217

207-
```python
208-
artifact_path="classifier"
209-
model_local_path = client.download_artifacts("1234-5678-90AB-CDEFG", path=artifact_path)
210-
```
218+
```python
219+
artifact_path="classifier"
220+
model_local_path = mlflow.artifacts.download_artifacts(
221+
run_id="1234-5678-90AB-CDEFG", artifact_path=artifact_path
222+
)
223+
```
211224

212225
You can then load the model back from the downloaded artifacts using the typical function `load_model`:
213226

214-
```python
215-
model = mlflow.xgboost.load_model(model_local_path)
216-
```
227+
```python
228+
model = mlflow.xgboost.load_model(model_local_path)
229+
```
230+
217231
> [!NOTE]
218-
> In the example above, we are assuming the model was created using `xgboost`. Change it to the flavor applies to your case.
232+
> The previous example assumes the model was created using `xgboost`. Change it to the flavor applies to your case.
219233
220-
MLflow also allows you to both operations at once and download and load the model in a single instruction. MLflow will download the model to a temporary folder and load it from there. This can be done using the `load_model` method which uses an URI format to indicate from where the model has to be retrieved. In the case of loading a model from a run, the URI structure is as follows:
234+
MLflow also allows you to both operations at once and download and load the model in a single instruction. MLflow will download the model to a temporary folder and load it from there. The method `load_model` uses an URI format to indicate from where the model has to be retrieved. In the case of loading a model from a run, the URI structure is as follows:
221235

222-
```python
223-
model = mlflow.xgboost.load_model(f"runs:/{last_run.info.run_id}/{artifact_path}")
224-
```
236+
```python
237+
model = mlflow.xgboost.load_model(f"runs:/{last_run.info.run_id}/{artifact_path}")
238+
```
225239

226240
> [!TIP]
227241
> You can also load models from the registry using MLflow. View [loading MLflow models with MLflow](how-to-manage-models-mlflow.md#loading-models-from-registry) for details.
228242
229243
## Getting child (nested) runs
230244

231-
MLflow supports the concept of child (nested) runs. They are useful when you need to spin off training routines requiring being tracked independently from the main training process. This is the typical case of hyper-parameter tuning for instance. You can query all the child runs of a specific run using the property tag `mlflow.parentRunId`, which contains the run ID of the parent run.
245+
MLflow supports the concept of child (nested) runs. They are useful when you need to spin off training routines requiring being tracked independently from the main training process. Hyper-parameter tuning optimization processes or Azure Machine Learning pipelines are typical examples of jobs that generate multiple child runs. You can query all the child runs of a specific run using the property tag `mlflow.parentRunId`, which contains the run ID of the parent run.
232246

233247
```python
234248
hyperopt_run = mlflow.last_active_run()
@@ -237,7 +251,7 @@ child_runs = mlflow.search_runs(
237251
)
238252
```
239253

240-
## Compare jobs and models in AzureML Studio (preview)
254+
## Compare jobs and models in AzureML studio (preview)
241255

242256
To compare and evaluate the quality of your jobs and models in AzureML Studio, use the [preview panel](./how-to-enable-preview-features.md) to enable the feature. Once enabled, you can compare the parameters, metrics, and tags between the jobs and/or models you selected.
243257

0 commit comments

Comments
 (0)