You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/machine-learning/concept-mlflow.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ Azure Machine Learning uses MLflow Tracking for metric logging and artifact stor
54
54
55
55
## Model Registries with MLflow
56
56
57
-
Azure Machine Learning supports MLflow for model management. This represents a convenient way to support the entire model lifecycle for users familiar with the MLFlow client. The following article describes the different capabilities and how it compares with other options.
57
+
Azure Machine Learning supports MLflow for model management. This represents a convenient way to support the entire model lifecycle for users familiar with the MLFlow client.
58
58
59
59
To learn more about how you can manage models using the MLflow API in Azure Machine Learning, view [Manage models registries in Azure Machine Learning with MLflow](how-to-manage-models-mlflow.md).
> [!div class="op_single_selector" title1="Select the version of Azure Machine Learning Python SDK you are using:"]
19
19
> *[v1](./v1/how-to-log-view-metrics.md)
20
-
> *[v2 (preview)](how-to-log-view-metrics.md)
20
+
> *[v2 (current)](how-to-log-view-metrics.md)
21
21
22
-
Log real-time information using [MLflow Tracking](https://www.mlflow.org/docs/latest/tracking.html). You can log models, metrics, and artifacts with MLflow as it supports local mode to cloud portability.
22
+
Azure Machine Learning supports logging and tracking experiments using [MLflow Tracking](https://www.mlflow.org/docs/latest/tracking.html). You can log models, metrics, parameters, and artifacts with MLflow as it supports local mode to cloud portability.
23
23
24
24
> [!IMPORTANT]
25
-
> Unlike the Azure Machine Learning SDK v1, there is no logging functionality in the SDK v2 preview.
25
+
> Unlike the Azure Machine Learning SDK v1, there is no logging functionality in the Azure Machine Learning SDK for Python (v2). If you were using Azure Machine Learning SDK v1 before, we recommend you to start leveraging MLflow for tracking experiments. See [Migrate logging from SDK v1 to MLflow](reference-migrate-sdk-v1-mlflow-tracking.md) for specific guidance.
26
26
27
27
Logs can help you diagnose errors and warnings, or track performance metrics like parameters and model performance. In this article, you learn how to enable logging in the following scenarios:
28
28
@@ -40,43 +40,110 @@ Logs can help you diagnose errors and warnings, or track performance metrics lik
40
40
41
41
* To use Azure Machine Learning, you must have an Azure subscription. If you don't have an Azure subscription, create a free account before you begin. Try the [free or paid version of Azure Machine Learning](https://azure.microsoft.com/free/).
42
42
* You must have an Azure Machine Learning workspace. A workspace is created in [Install, set up, and use the CLI (v2)](how-to-configure-cli.md).
43
-
* You must have the `aureml-core`, `mlflow`, and `azure-mlflow` packages installed. If you don't, use the following command to install them in your development environment:
43
+
* You must have `mlflow`, and `azureml-mlflow` packages installed. If you don't, use the following command to install them in your development environment:
44
44
45
45
```bash
46
-
pip install azureml-core mlflow azureml-mlflow
46
+
pip install mlflow azureml-mlflow
47
47
```
48
48
49
-
## Data types
49
+
> [!IMPORTANT]
50
+
> If you are running outside of any Azure Machine Learning Compute and you want to do remote tracking (running your training routine in other compute but tracking on Azure Machine Learning), you must have MLflow configured to do tracking to your workspace. See [Setup your tracking environment](how-to-use-mlflow-cli-runs.md?#set-up-tracking-environment) for more details.
51
+
52
+
## Logging parameters
53
+
54
+
MLflow supports the logging parameters used by your experiments. Parameters can be of any type, and can be logged using the following syntax:
50
55
51
-
The following table describes how to log specific value types:
56
+
```python
57
+
mlflow.log_param("num_epochs", 20)
58
+
```
59
+
60
+
MLflow also offers a convenient way to log multiple parameters by indicating all of them using a dictionary. Several frameworks can also pass parameters to models using dictionaries and hence this is a convenient way to log them in the experiment.
61
+
62
+
```python
63
+
params = {
64
+
"num_epochs": 20,
65
+
"dropout_rate": .6,
66
+
"objective": "binary_crossentropy"
67
+
}
68
+
69
+
mlflow.log_params(params)
70
+
```
71
+
72
+
> [!NOTE]
73
+
> Azure ML SDK v1 logging can't log parameters. We recommend the use of MLflow for tracking experiments as it offers a superior set of features.
74
+
75
+
## Logging metrics
76
+
77
+
Metrics, as opposite to parameters, are always numeric. The following table describes how to log specific numeric types:
52
78
53
79
|Logged Value|Example code| Notes|
54
80
|----|----|----|
55
-
|Log a numeric value (int or float) |`mlflow.log_metric('my_metric', 1)`||
56
-
|Log a boolean value |`mlflow.log_metric('my_metric', 0)`| 0 = True, 1 = False|
57
-
|Log a string |`mlflow.log_text('foo', 'my_string')`| Logged as an artifact|
58
-
|Log numpy metrics or PIL image objects|`mlflow.log_image(img, 'figure.png')`||
59
-
|Log matlotlib plot or image file|` mlflow.log_figure(fig, "figure.png")`||
81
+
|Log a numeric value (int or float) | `mlflow.log_metric("my_metric", 1)`| |
82
+
|Log a numeric value (int or float) over time | `mlflow.log_metric("my_metric", 1, step=1)`| Use parameter `step` to indicate the step at which you are logging the metric value. It can be any integer number. It defaults to zero. |
83
+
|Log a boolean value | `mlflow.log_metric("my_metric", 0)`| 0 = True, 1 = False|
60
84
61
-
## Log a training job with MLflow
85
+
> [!IMPORTANT]
86
+
> __Performance considerations:__ If you need to log multiple metrics (or multiple values for the same metric) avoid making calls to `mlflow.log_metric` in loops. Better performance can be achieved by logging batch of metrics. Use the method `mlflow.log_metrics` which accepts a dictionary with all the metrics you want to log at once or use `mlflow.log_batch` which accepts multiple type of elements for logging.
62
87
63
-
To set up for logging with MLflow, import `mlflow` and set the tracking URI:
88
+
### Logging curves or list of values
64
89
65
-
> [!TIP]
66
-
> You do not need to set the tracking URI when using a notebook running on an Azure Machine Learning compute instance.
90
+
Curves (or list of numeric values) can be logged with MLflow by logging the same metric multiple times. The following example shows how to do it:
67
91
68
92
```python
69
-
from azureml.core import Workspace
70
-
import mlflow
93
+
list_to_log = [1, 2, 3, 2, 1, 2, 3, 2, 1]
94
+
from mlflow.entities import Metric
95
+
from mlflow.tracking import MlflowClient
96
+
import time
71
97
72
-
ws = Workspace.from_config()
73
-
# Set the tracking URI to the Azure ML backend
74
-
# Not needed if running on Azure ML compute instance
metrics=[Metric(key="sample_list", value=val, timestamp=int(time.time() * 1000), step=0) for val in list_to_log])
77
101
```
78
102
79
-
### Interactive jobs
103
+
## Logging images
104
+
105
+
MLflow supports two ways of logging images:
106
+
107
+
|Logged Value|Example code| Notes|
108
+
|----|----|----|
109
+
|Log numpy metrics or PIL image objects|`mlflow.log_image(img, "figure.png")`| `img` should be an instance of `numpy.ndarray` or `PIL.Image.Image`. `figure.png` is the name of the artifact that will be generated inside of the run. It doesn't have to be an existing file.|
110
+
|Log matlotlib plot or image file|` mlflow.log_figure(fig, "figure.png")`|`figure.png` is the name of the artifact that will be generated inside of the run. It doesn't have to be an existing file. |
111
+
112
+
## Logging other types of data
113
+
114
+
|Logged Value|Example code| Notes|
115
+
|----|----|----|
116
+
|Log text in a text file | `mlflow.log_text("text string", "notes.txt")`| Text is persisted inside of the run in a text file with name `notes.txt`. |
117
+
|Log dictionaries as `JSON` and `YAML` files | `mlflow.log_dict(dictionary, "file.yaml"` | `dictionary` is a dictionary object containing all the structure that you want to persist as `JSON` or `YAML` file. |
118
+
|Log a trivial file already existing | `mlflow.log_artifact("path/to/file.pkl")`| Files are always logged in the root of the run. If `artifact_path` is provided, then the file is logged in a folder as indicated in that parameter. |
119
+
|Log all the artifacts in an existing folder | `mlflow.log_artifacts("path/to/folder")`| Folder structure is copied to the run, but the root folder indicated is not included. |
120
+
121
+
## Logging models
122
+
123
+
MLflow introduces the concept of "models" as a way to package all the artifacts required for a given model to function. Models in MLflow are always a folder with an arbitrary number of files, depending on the framework used to generate the model. Logging models has the advantage of tracking all the elements of the model as a single entity that can be __registered__ and then __deployed__. On top of that, MLflow models enjoy the benefit of [no-code deployment](how-to-deploy-mlflow-models.md) and can be used with the [Responsible AI dashboard](how-to-responsible-ai-dashboard.md) in studio.
124
+
125
+
To save the model from a training run, use the `log_model()` API for the framework you're working with. For example, [mlflow.sklearn.log_model()](https://mlflow.org/docs/latest/python_api/mlflow.sklearn.html#mlflow.sklearn.log_model). For frameworks that MLflow doesn't support, see [Convert custom models to MLflow](how-to-convert-custom-model-to-mlflow.md).
126
+
127
+
## Automatic logging
128
+
129
+
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.
130
+
131
+
To enable [automatic logging](https://mlflow.org/docs/latest/tracking.html#automatic-logging) insert the following code before your training code:
132
+
133
+
```Python
134
+
mlflow.autolog()
135
+
```
136
+
137
+
> [!TIP]
138
+
> You can control what gets automatically logged wit autolog. For instance, if you indicate `mlflow.autolog(log_models=False)`, MLflow will log everything but models for you. Such control is useful in cases where you want to log models manually but still enjoy automatic logging of metrics and parameters. Also notice that some frameworks may disable automatic logging of models if the trained model goes behond specific boundaries. Such behavior depends on the flavor used and we recommend you to view they documentation if this is your case.
139
+
140
+
[Learn more about Automatic logging with MLflow](https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.autolog).
141
+
142
+
## Configuring experiments and runs in Azure Machine Learning
143
+
144
+
MLflow organizes the information in experiments and runs (in Azure Machine Learning, runs are called Jobs). There are some differences in how to configure them depending on how you are running your code:
145
+
146
+
# [Training interactively](#tab/interactive)
80
147
81
148
When training interactively, such as in a Jupyter Notebook, use the following pattern:
82
149
@@ -85,13 +152,11 @@ When training interactively, such as in a Jupyter Notebook, use the following pa
85
152
1. Use logging methods to log metrics and other information.
86
153
1. End the job.
87
154
88
-
For example, the following code snippet demonstrates setting the tracking URI, creating an experiment, and then logging during a job
155
+
For example, the following code snippet demonstrates configuring the experiment, and then logging during a job:
89
156
90
157
```python
91
-
from mlflow.tracking import MlflowClient
92
-
93
-
# Create a new experiment if one doesn't already exist
94
-
mlflow.create_experiment("mlflow-experiment")
158
+
import mlflow
159
+
mlflow.set_experiment("mlflow-experiment")
95
160
96
161
# Start the run, log metrics, end the run
97
162
mlflow_run = mlflow.start_run()
@@ -105,10 +170,8 @@ mlflow.end_run()
105
170
You can also use the context manager paradigm:
106
171
107
172
```python
108
-
from mlflow.tracking import MlflowClient
109
-
110
-
# Create a new experiment if one doesn't already exist
111
-
mlflow.create_experiment("mlflow-experiment")
173
+
import mlflow
174
+
mlflow.set_experiment("mlflow-experiment")
112
175
113
176
# Start the run, log metrics, end the run
114
177
with mlflow.start_run() as run:
@@ -118,19 +181,35 @@ with mlflow.start_run() as run:
118
181
pass
119
182
```
120
183
184
+
When you start a new run with `mlflow.start_run`, it may be useful to indicate the parameter `run_name` which will then translate to the name of the run in Azure Machine Learning user interface and help you identify the run quicker:
185
+
186
+
```python
187
+
with mlflow.start_run(run_name="iris-classifier-random-forest") as run:
188
+
mlflow.log_metric('mymetric', 1)
189
+
mlflow.log_metric('anothermetric',1)
190
+
```
191
+
121
192
For more information on MLflow logging APIs, see the [MLflow reference](https://www.mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_artifact).
122
193
123
-
### Remote runs
194
+
# [Training with jobs](#tab/jobs)
124
195
125
-
For remote training runs, the tracking URI and experiment are set automatically. Otherwise, the options for logging the run are the same as for interactive logging:
196
+
When running training jobsin Azure Machine Learning you don't need to configure the MLflow tracking URI as it is already configured for you. On top of that, you don't need to call `mlflow.start_run` as runs are automatically started. Hence, you can use mlflow tracking capabilities directly in your training scripts:
126
197
127
-
* Call `mlflow.start_run()`, log information, and then call `mlflow.end_run()`.
128
-
* Use the context manager paradigm with `mlflow.start_run()`.
129
-
* Call a logging API such as `mlflow.log_metric()`, which will start a run if one doesn't already exist.
198
+
```python
199
+
import mlflow
130
200
131
-
## Log a model
201
+
mlflow.set_experiment("my-experiment")
132
202
133
-
To save the model from a training run, use the `log_model()` API for the framework you're working with. For example, [mlflow.sklearn.log_model()](https://mlflow.org/docs/latest/python_api/mlflow.sklearn.html#mlflow.sklearn.log_model). For frameworks that MLflow doesn't support, see [Convert custom models to MLflow](how-to-convert-custom-model-to-mlflow.md).
203
+
mlflow.autolog()
204
+
205
+
mlflow.log_metric('mymetric', 1)
206
+
mlflow.log_metric('anothermetric',1)
207
+
```
208
+
209
+
> [!TIP]
210
+
> When submitting jobs using Azure ML CLI v2, you can set the experiment name using the property `experiment_name`in the YAML definition of the job. You don't have to configure it on your training script. See [YAML: display name, experiment name, description, and tags](reference-yaml-job-command.md#yaml-display-name-experiment-name-description-and-tags) for details.
211
+
212
+
---
134
213
135
214
## View job information
136
215
@@ -148,8 +227,8 @@ You can view the metrics, parameters, and tags for the run in the data field of
You can use [Azure Machine Learning studio](https://ml.azure.com) to monitor, organize, and track your jobs for training and experimentation. Your ML job history is an important part of an explainable and repeatable ML development process.
> When submitting jobs using Azure ML CLI v2, you can set the experiment name using the property `experiment_name` in the YAML definition of the job. You don't have to configure it on your training script. See [YAML: display name, experiment name, description, and tags](reference-yaml-job-command.md#yaml-display-name-experiment-name-description-and-tags) for details.
130
+
128
131
You can also set one of the MLflow environment variables [MLFLOW_EXPERIMENT_NAME or MLFLOW_EXPERIMENT_ID](https://mlflow.org/docs/latest/cli.html#cmdoption-mlflow-run-arg-uri) with the experiment name.
129
132
130
133
```bash
@@ -186,17 +189,6 @@ Open your terminal and use the following to submit the job.
186
189
az ml job create -f job.yml --web
187
190
```
188
191
189
-
## Automatic logging
190
-
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.
191
-
192
-
To enable [automatic logging](https://mlflow.org/docs/latest/tracking.html#automatic-logging) insert the following code before your training code:
193
-
194
-
```Python
195
-
mlflow.autolog()
196
-
```
197
-
198
-
[Learn more about Automatic logging with MLflow](https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.autolog).
199
-
200
192
201
193
## View metrics and artifacts in your workspace
202
194
@@ -274,14 +266,7 @@ To register and view a model from a run, use the following steps:
274
266
275
267
## Limitations
276
268
277
-
The following MLflow methods are not fully supported with Azure Machine Learning.
Some methods available in the MLflow API may not be available when connected to Azure Machine Learning. For details about supported and unsupported operations please read [Support matrix for querying runs and experiments](how-to-track-experiments-mlflow.md#support-matrix-for-querying-runs-and-experiments).
0 commit comments