在 Azure Machine Learning 中使用模型

適用於:Azure CLI ml 延伸模組 v2 (目前)Python SDK azure-ai-ml v2 (目前)

Azure Machine Learning 可讓您使用不同類型的模型。 在本文中,您將瞭解如何使用 Azure Machine Learning 來處理不同的模型類型,例如自訂、MLflow 和 Triton。 您也會瞭解如何從不同的位置登錄模型,以及如何使用 Azure Machine Learning SDK、使用者介面 (UI) ,以及 Azure Machine Learning CLI 來管理您的模型。

提示

如果您擁有使用 SDK/CLI v1 建立的模型資產,則仍然可以搭配使用這些資產與 SDK/CLI v2。 提供完整的回溯相容性。 所有向 V1 SDK 登錄的模型都會獲指派類型 custom

必要條件

此外,您必須:

支援的路徑

在提供您要註冊的模型時,您必須指定指向資料或作業位置的 path 參數。 下列資料表顯示 Azure Machine Learning 中所支援的不同資料位置,以及path參數之範例:

Location 範例
本機電腦上的路徑 mlflow-model/model.pkl
Azure Machine Learning 資料存放區上的路徑 azureml://datastores/<datastore-name>/paths/<path_on_datastore>
Azure Machine Learning 作業的路徑 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 Machine Learning 工作區中模型資產的路徑 azureml:<model-name>:<version>
Azure Machine Learning 登錄中模型資產的路徑 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 將模型登錄為 Machine Learning 中的資產。
  • 使用 SDK 將模型登錄為 Machine Learning 中的資產。
  • 使用 UI 將模型登錄為 Machine Learning 中的資產。

這些程式碼片段會使用 custommlflow

  • custom 是一種類型,參考以 Azure Machine Learning 目前不支援的自訂標準定型的模型檔案或資料夾。
  • mlflow 是參照模型的類型,而此模型是使用 mlflow 所定型。 MLflow 定型模型位於資料夾中,其中包含 MLmodel 檔案、模型檔案、conda 相依性檔案,以及 requirements.txt 檔案。

連線到您的工作區

首先,讓我們連線到我們要在其中工作的 Azure Machine Learning 工作區。

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

使用 CLI 將模型登錄為 Machine Learning 中的資產

使用下列索引標籤來選取模型所在的位置。

$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 將模型登錄為 Machine Learning 中的資產

使用下列索引標籤來選取模型所在的位置。

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 將模型登錄為 Machine Learning 中的資產

若要在 Machine Learning 中建立模型,請從 UI 開啟 [模型] 頁面。 選取 [登錄模型],然後選取模型所在的位置。 填妥必要的欄位,然後選取 [登錄]

Screenshot of the UI to register a model.


管理模型

SDK 和 CLI (v2) 也可讓您管理 Azure Machine Learning 模型資產的生命週期。

清單​​

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

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_modeltriton_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://registries/azureml/environments/sklearn-1.0/labels/latest

接下來,在 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://registries/azureml/environments/sklearn-1.0/labels/latest

接下來,使用 CLI 建立作業:

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

如需完整範例,請參閱模型 GitHub 存放庫

下一步