Azure 机器学习将 MLflow 跟踪用于试验的指标日志记录和项目存储,无论试验是通过 Azure 机器学习 Python SDK、Azure 机器学习 CLI 还是 Azure 机器学习工作室创建的。 建议使用 MLflow 来跟踪试验。
如果要从 SDK v1 迁移到 SDK v2,请使用本部分中的信息来了解 SDK v1 记录 API 的 MLflow 等效项。
为什么使用 MLflow?
MLflow 每月下载量超过 1300 万次,已成为端到端 MLOps 的标准平台,各种规模的团队通过它都能够跟踪、共享、打包和部署任何模型以进行批量或实时推理。 Azure 机器学习与 MLflow 集成,可支持训练代码实现真正的可移植性,并与其他平台无缝集成,因为它不包含任何特定于 Azure 机器学习的指令。
准备迁移到 MLflow
要使用 MLflow 跟踪,需要安装 Mlflow SDK 包 mlflow
和适用于 MLflow azureml-mlflow
的 Azure 机器学习插件。 所有 Azure 机器学习环境已提供这些包,但如果创建自己的环境,则需包含这些包。
pip install mlflow azureml-mlflow
连接到工作区
用户使用 Azure 机器学习可在工作区上运行或远程运行的训练作业中执行跟踪(跟踪在 Azure 机器学习外部运行的试验)。 如果执行远程跟踪,则需指示要将 MLflow 连接到的工作区。
在 Azure 机器学习计算中运行时,已连接到工作区。
实验和运行
SDK v1
from azureml.core import Experiment
# create an Azure Machine Learning experiment and start a run
experiment = Experiment(ws, "create-experiment-sdk-v1")
azureml_run = experiment.start_logging()
使用 MLflow 的 SDK v2
# Set the MLflow experiment and start a run
mlflow.set_experiment("logging-with-mlflow")
mlflow_run = mlflow.start_run()
记录 API 比较
记录整数或浮点数指标
SDK v1
azureml_run.log("sample_int_metric", 1)
使用 MLflow 的 SDK v2
mlflow.log_metric("sample_int_metric", 1)
记录布尔指标
SDK v1
azureml_run.log("sample_boolean_metric", True)
使用 MLflow 的 SDK v2
mlflow.log_metric("sample_boolean_metric", 1)
记录字符串指标
SDK v1
azureml_run.log("sample_string_metric", "a_metric")
使用 MLflow 的 SDK v2
mlflow.log_text("sample_string_text", "string.txt")
- 字符串将记录为项目,而不是记录为指标。 在 Azure 机器学习工作室中,该值会显示在“输出 + 日志”选项卡中。
将图像记录到 PNG 或 JPEG 文件
SDK v1
azureml_run.log_image("sample_image", path="Azure.png")
使用 MLflow 的 SDK v2
mlflow.log_artifact("Azure.png")
图像会记录为项目,并将显示在 Azure 机器学习工作室中的“图像”选项卡中。
记录 matplotlib.pyplot
SDK v1
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
azureml_run.log_image("sample_pyplot", plot=plt)
使用 MLflow 的 SDK v2
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
fig, ax = plt.subplots()
ax.plot([0, 1], [2, 3])
mlflow.log_figure(fig, "sample_pyplot.png")
- 图像会记录为项目,并将显示在 Azure 机器学习工作室中的“图像”选项卡中。
记录指标列表
SDK v1
list_to_log = [1, 2, 3, 2, 1, 2, 3, 2, 1]
azureml_run.log_list('sample_list', list_to_log)
使用 MLflow 的 SDK v2
list_to_log = [1, 2, 3, 2, 1, 2, 3, 2, 1]
from mlflow.entities import Metric
from mlflow.tracking import MlflowClient
import time
metrics = [Metric(key="sample_list", value=val, timestamp=int(time.time() * 1000), step=0) for val in list_to_log]
MlflowClient().log_batch(mlflow_run.info.run_id, metrics=metrics)
- 指标显示在 Azure 机器学习工作室中的“指标”选项卡中。
- 不支持文本值。
记录一行指标
SDK v1
azureml_run.log_row("sample_table", col1=5, col2=10)
使用 MLflow 的 SDK v2
metrics = {"sample_table.col1": 5, "sample_table.col2": 10}
mlflow.log_metrics(metrics)
- 在 Azure 机器学习工作室中,指标不会呈现为表。
- 不支持文本值。
- 记录为项目,而不是指标。
记录表
SDK v1
table = {
"col1" : [1, 2, 3],
"col2" : [4, 5, 6]
}
azureml_run.log_table("table", table)
使用 MLflow 的 SDK v2
# Add a metric for each column prefixed by metric name. Similar to log_row
row1 = {"table.col1": 5, "table.col2": 10}
# To be done for each row in the table
mlflow.log_metrics(row1)
# Using mlflow.log_artifact
import json
with open("table.json", 'w') as f:
json.dump(table, f)
mlflow.log_artifact("table.json")
- 记录每个列的指标。
- 在 Azure 机器学习工作室中,指标不会呈现为表。
- 不支持文本值。
- 记录为项目,而不是指标。
记录准确度表
SDK v1
ACCURACY_TABLE = '{"schema_type": "accuracy_table", "schema_version": "v1", "data": {"probability_tables": ' +\
'[[[114311, 385689, 0, 0], [0, 0, 385689, 114311]], [[67998, 432002, 0, 0], [0, 0, ' + \
'432002, 67998]]], "percentile_tables": [[[114311, 385689, 0, 0], [1, 0, 385689, ' + \
'114310]], [[67998, 432002, 0, 0], [1, 0, 432002, 67997]]], "class_labels": ["0", "1"], ' + \
'"probability_thresholds": [0.52], "percentile_thresholds": [0.09]}}'
azureml_run.log_accuracy_table('v1_accuracy_table', ACCURACY_TABLE)
使用 MLflow 的 SDK v2
ACCURACY_TABLE = '{"schema_type": "accuracy_table", "schema_version": "v1", "data": {"probability_tables": ' +\
'[[[114311, 385689, 0, 0], [0, 0, 385689, 114311]], [[67998, 432002, 0, 0], [0, 0, ' + \
'432002, 67998]]], "percentile_tables": [[[114311, 385689, 0, 0], [1, 0, 385689, ' + \
'114310]], [[67998, 432002, 0, 0], [1, 0, 432002, 67997]]], "class_labels": ["0", "1"], ' + \
'"probability_thresholds": [0.52], "percentile_thresholds": [0.09]}}'
mlflow.log_dict(ACCURACY_TABLE, 'mlflow_accuracy_table.json')
- 在 Azure 机器学习工作室中,指标不会呈现为准确度表。
- 记录为项目,而不是指标。
mlflow.log_dict
方法是实验性方法。
记录混淆矩阵
SDK v1
CONF_MATRIX = '{"schema_type": "confusion_matrix", "schema_version": "v1", "data": {"class_labels": ' + \
'["0", "1", "2", "3"], "matrix": [[3, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]}}'
azureml_run.log_confusion_matrix('v1_confusion_matrix', json.loads(CONF_MATRIX))
使用 MLflow 的 SDK v2
CONF_MATRIX = '{"schema_type": "confusion_matrix", "schema_version": "v1", "data": {"class_labels": ' + \
'["0", "1", "2", "3"], "matrix": [[3, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]}}'
mlflow.log_dict(CONF_MATRIX, 'mlflow_confusion_matrix.json')
- 在 Azure 机器学习工作室中,指标不会呈现为混淆矩阵。
- 记录为项目,而不是指标。
mlflow.log_dict
方法是实验性方法。
记录预测
SDK v1
PREDICTIONS = '{"schema_type": "predictions", "schema_version": "v1", "data": {"bin_averages": [0.25,' + \
' 0.75], "bin_errors": [0.013, 0.042], "bin_counts": [56, 34], "bin_edges": [0.0, 0.5, 1.0]}}'
azureml_run.log_predictions('test_predictions', json.loads(PREDICTIONS))
使用 MLflow 的 SDK v2
PREDICTIONS = '{"schema_type": "predictions", "schema_version": "v1", "data": {"bin_averages": [0.25,' + \
' 0.75], "bin_errors": [0.013, 0.042], "bin_counts": [56, 34], "bin_edges": [0.0, 0.5, 1.0]}}'
mlflow.log_dict(PREDICTIONS, 'mlflow_predictions.json')
- 在 Azure 机器学习工作室中,指标不会呈现为混淆矩阵。
- 记录为项目,而不是指标。
mlflow.log_dict
方法是实验性方法。
记录残差
SDK v1
RESIDUALS = '{"schema_type": "residuals", "schema_version": "v1", "data": {"bin_edges": [100, 200, 300], ' + \
'"bin_counts": [0.88, 20, 30, 50.99]}}'
azureml_run.log_residuals('test_residuals', json.loads(RESIDUALS))
使用 MLflow 的 SDK v2
RESIDUALS = '{"schema_type": "residuals", "schema_version": "v1", "data": {"bin_edges": [100, 200, 300], ' + \
'"bin_counts": [0.88, 20, 30, 50.99]}}'
mlflow.log_dict(RESIDUALS, 'mlflow_residuals.json')
- 在 Azure 机器学习工作室中,指标不会呈现为混淆矩阵。
- 记录为项目,而不是指标。
mlflow.log_dict
方法是实验性方法。
查看运行信息和数据
可以使用 mLflow 运行 (mlflow.entities.Run) 对象的属性 data
和 info
来访问运行信息。
提示
可以使用 MLflow 查询 Azure 机器学习中的试验和运行跟踪信息,MLflow 可提供全面的搜索 API 来轻松查询和搜索试验和运行,并快速比较结果。 有关此维度的 MLflow 中所有功能的详细信息,请参阅使用 MLflow 查询并比较试验和运行
以下示例展示了如何检索已完成的运行:
from mlflow.tracking import MlflowClient
# Use MlFlow to retrieve the run that was just completed
client = MlflowClient()
finished_mlflow_run = MlflowClient().get_run("<RUN_ID>")
以下示例展示了如何查看 metrics
、tags
和 params
:
metrics = finished_mlflow_run.data.metrics
tags = finished_mlflow_run.data.tags
params = finished_mlflow_run.data.params
注意
metrics
将仅具有给定指标的最近记录值。 例如,如果按顺序依次将 1
、2
、3
和 4
的值记录到名为 sample_metric
的指标,则 metrics
字典中将仅存在 4
。 若要获取为特定命名指标记录的所有指标,请使用 MlFlowClient.get_metric_history:
with mlflow.start_run() as multiple_metrics_run:
mlflow.log_metric("sample_metric", 1)
mlflow.log_metric("sample_metric", 2)
mlflow.log_metric("sample_metric", 3)
mlflow.log_metric("sample_metric", 4)
print(client.get_run(multiple_metrics_run.info.run_id).data.metrics)
print(client.get_metric_history(multiple_metrics_run.info.run_id, "sample_metric"))
有关详细信息,请参阅 MlFlowClient 参考。
info
字段提供有关运行的常规信息,例如开始时间、运行 ID、实验 ID 等:
run_start_time = finished_mlflow_run.info.start_time
run_experiment_id = finished_mlflow_run.info.experiment_id
run_id = finished_mlflow_run.info.run_id
查看运行项目
若要查看运行的项目,请使用 MlFlowClient.list_artifacts:
client.list_artifacts(finished_mlflow_run.info.run_id)
要下载项目,请使用 mlflow.artifacts.download_artifacts:
mlflow.artifacts.download_artifacts(run_id=finished_mlflow_run.info.run_id, artifact_path="Azure.png")