在 Azure 机器学习中使用模型

适用范围:Azure CLI ml 扩展 v2(最新版)Python SDK azure-ai-ml v2(最新版)

Azure 机器学习可用于处理不同类型的模型。 本文介绍如何使用 Azure 机器学习处理不同的模型类型,例如自定义、MLflow 和 Triton。 另外,介绍如何从不同的位置注册模型,以及如何使用 Azure 机器学习 SDK、用户界面 (UI) 和 Azure 机器学习 CLI 来管理模型。

提示

如果已创建使用 SDK/CLI v1 的模型资产,仍然可以通过 SDK/CLI v2 使用这些资产。 提供完全后向兼容性。 使用 V1 SDK 注册的所有模型都分配有 custom 类型。

先决条件

另外需要:

支持的路径

当提供要注册的模型时,需要指定指向数据或作业位置的 path 参数。 下表显示了 Azure 机器学习中支持的不同数据位置以及 path 参数示例:

位置 示例
本地计算机上的路径 mlflow-model/model.pkl
Azure 机器学习数据存储上的路径 azureml://datastores/<datastore-name>/paths/<path_on_datastore>
Azure 机器学习作业中的路径 azureml://jobs/<job-name>/outputs/<output-name>/paths/<path-to-model-relative-to-the-named-output-location>
MLflow 作业的路径 runs:/<run-id>/<path-to-model-relative-to-the-root-of-the-artifact-location>
Azure 机器学习工作区的模型资产中的路径 azureml:<model-name>:<version>
Azure 机器学习注册表的模型资产中的路径 azureml://registries/<registry-name>/models/<model-name>/versions/<version>

支持的模式

当你使用模型输入/输出运行作业时,可以指定模式 - 例如,希望模型以只读方式装载还是下载到计算目标。 下表显示了不同类型/模式/输入/输出组合的可能模式:

类型 输入/输出 upload download ro_mount rw_mount direct
custom 文件 输入
custom 文件夹 输入
mlflow 输入
custom 文件 输出
custom 文件夹 输出
mlflow 输出

在 Jupyter Notebook 中继续操作

可以在 Jupyter Notebook 中按照此示例进行操作。 在 azureml-examples 存储库中,打开笔记本:model.ipynb

在模型注册表中创建模型

通过模型注册,可在 Azure 云的工作区中存储模型并控制模型版本。 模型注册表可帮助你组织和跟踪已训练的模型。

本部分中的代码片段演示如何执行以下操作:

  • 使用 CLI 将模型注册为机器学习中的资产。
  • 使用 SDK 将模型注册为机器学习中的资产。
  • 使用 UI 将模型注册为机器学习中的资产。

这些代码片段使用 custommlflow

  • custom 是一种类型,它指的是使用 Azure 机器学习当前不支持的自定义标准训练的模型文件或文件夹。
  • mlflow 是引用使用 mlflow 训练的模型的类型。 MLflow 训练的模型位于包含 MLmodel 文件、模型文件、conda 依赖项文件和 requirements.txt 文件的文件夹中。

连接到工作区

首先,让我们连接到要在其中工作的 Azure 机器学习工作区。

az account set --subscription <subscription>
az configure --defaults workspace=<workspace> group=<resource-group> location=<location>

使用 CLI 将模型注册为机器学习中的资产

使用以下选项卡选择模型所在的位置。

$schema: https://azuremlschemas.azureedge.net/latest/model.schema.json
name: local-file-example
path: mlflow-model/model.pkl
description: Model created from local file.
az ml model create -f <file-name>.yml

有关完整示例,请参阅模型 YAML

使用 SDK 将模型注册为机器学习中的资产

使用以下选项卡选择模型所在的位置。

from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes

file_model = Model(
    path="mlflow-model/model.pkl",
    type=AssetTypes.CUSTOM_MODEL,
    name="local-file-example",
    description="Model created from local file.",
)
ml_client.models.create_or_update(file_model)

使用 UI 将模型注册为机器学习中的资产

若要在机器学习中创建模型,请在 UI 中打开“模型”页。 选择“注册模型”,然后选择模型所在的位置。 填写必填字段,然后选择“注册“。

Screenshot of the UI to register a model.


管理模型

通过 SDK 和 CLI (v2) 还可管理 Azure 机器学习模型资产的生命周期。

列表

列出工作区中的所有模型:

az ml model list

列出给定名称下的所有模型版本:

az ml model list --name run-model-example

显示

获取特定模型的详细信息:

az ml model show --name run-model-example --version 1

更新

更新特定模型的可变属性:

az ml model update --name  run-model-example --version 1 --set description="This is an updated description." --set tags.stage="Prod"

重要

对于模型,只能更新 descriptiontags。 所有其他属性都是不可变的;如果需要更改其中的任何属性,应创建一个新版本的模型。

存档

默认情况下,将模型存档会使其从列表查询 (az ml model list) 中隐藏。 你仍然可以继续在工作流中引用和使用已存档的模型。 你可以存档模型的所有版本,也可只存档特定版本。

如果未指定版本,则会存档该给定名称下模型的所有版本。 如果在已存档模型容器下创建新的模型版本,该新版本也将自动设置为已存档。

存档所有模型版本:

az ml model archive --name run-model-example

存档特定模型版本:

az ml model archive --name run-model-example --version 1

使用模型进行训练

通过 SDK 和 CLI (v2),还可以在训练作业中将模型用作输入或输出。

在作业中使用模型作为输入

创建作业规范 YAML 文件 (<file-name>.yml)。 在作业的 inputs 部分指定:

  1. type;无论模型是 mlflow_modelcustom_model 还是 triton_model
  2. 数据所在位置的 path;可以是支持的路径部分中概述的任何路径。
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json

# Possible Paths for models:
# AzureML Datastore: azureml://datastores/<datastore-name>/paths/<path_on_datastore>
# MLflow run: runs:/<run-id>/<path-to-model-relative-to-the-root-of-the-artifact-location>
# Job: azureml://jobs/<job-name>/outputs/<output-name>/paths/<path-to-model-relative-to-the-named-output-location>
# Model Asset: azureml:<my_model>:<version>

command: |
  ls ${{inputs.my_model}}
inputs:
  my_model:
    type: mlflow_model # List of all model types here: https://learn.microsoft.com/azure/machine-learning/reference-yaml-model#yaml-syntax
    path: ../../assets/model/mlflow-model
environment: azureml:AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest
compute: azureml:cpu-cluster

接下来,在 CLI 中运行

az ml job create -f <file-name>.yml

有关完整示例,请参阅模型 GitHub 存储库

在作业中使用模型作为输出

在作业中,可使用输出将模型写入基于云的存储。

创建作业规范 YAML 文件 (<file-name>.yml),并在 outputs 部分使用要将数据写入到的类型和路径进行填充:

$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json

# Possible Paths for Model:
# Local path: mlflow-model/model.pkl
# AzureML Datastore: azureml://datastores/<datastore-name>/paths/<path_on_datastore>
# MLflow run: runs:/<run-id>/<path-to-model-relative-to-the-root-of-the-artifact-location>
# Job: azureml://jobs/<job-name>/outputs/<output-name>/paths/<path-to-model-relative-to-the-named-output-location>
# Model Asset: azureml:<my_model>:<version>

code: src
command: >-
  python hello-model-as-output.py 
  --input_model ${{inputs.input_model}} 
  --custom_model_output ${{outputs.output_folder}}
inputs:
  input_model: 
    type: mlflow_model # mlflow_model,custom_model, triton_model
    path: ../../assets/model/mlflow-model
outputs:
  output_folder: 
    type: custom_model # mlflow_model,custom_model, triton_model
environment: azureml:AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest
compute: azureml:cpu-cluster

接下来,使用 CLI 创建作业:

az ml job create --file <file-name>.yml

有关完整示例,请参阅模型 GitHub 存储库

后续步骤