如何使用批次端點操作定型管線
適用於:Azure CLI ml 延伸模組 v2 (目前)Python SDK azure-ai-ml v2 (目前)
在本文中,您將了解如何在批次端點下操作定型管線。 管線會使用多個元件 (或步驟),包括模型定型、資料前處理和模型評估。
您將了解:
- 建立及測試定型管線
- 將管線部署到批次端點
- 修改管線,並在相同的端點中建立新的部署
- 測試新部署,並將它設定為預設部署
關於此範例
此範例會部署定型管線,該管線採用輸入定型資料 (加上標籤) 並產生預測模型,以及在前處理期間套用的評估結果和轉換。 管線將使用來自 UCI 心臟病資料集的表格式資料來定型 XGBoost 模型。 我們會使用資料前處理元件,在資料傳送至定型元件之前預先處理,以符合並評估模型。
管線的視覺效果如下所示:
本文中的範例是以 azureml-examples (英文) 存放庫內含的程式碼範例為基礎。 若要在本機執行命令,而不需要複製/貼上 YAML 和其他檔案,請複製存放庫,然後將目錄變更為該資料夾:
git clone https://github.com/Azure/azureml-examples --depth 1
cd azureml-examples/cli
此範例的檔案位於:
cd endpoints/batch/deploy-pipelines/training-with-components
在 Jupyter Notebook 中跟著做
您可以在複製的存放庫中開啟 sdk-deploy-and-test.ipynb (英文) 筆記本,以便按照此範例的 Python SDK 版本說明操作。
必要條件
遵循本文中的步驟之前,請確定您已滿足下列必要條件:
Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。 試用免費或付費版本的 Azure Machine Learning。
Azure Machine Learning 工作區。 如果您沒有帳戶,請使用管理 Azure 機器學習 工作區一文中的步驟來建立一個工作區。
請確定您在工作區中具有下列權限:
建立或管理批次端點和部署:使用允許
Microsoft.MachineLearningServices/workspaces/batchEndpoints/*
的擁有者、參與者或自定義角色。在工作區資源群組中建立ARM部署:使用可在部署工作區的資源群組中使用擁有
Microsoft.Resources/deployments/write
者、參與者或自定義角色。
您必須安裝下列軟體,才能使用 Azure 機器學習:
Azure CLI 和適用於 Azure Machine Learning 的
ml
擴充功能。az extension add -n ml
注意
批次端點的管線元件部署是在 Azure CLI 的
ml
擴充功能 2.7 版中引進。 使用az extension update --name ml
來取得其最後一個版本。
連線到您的工作區
工作區是 Azure Machine Learning 的最上層資源,其提供一個集中位置來處理您在使用 Azure Machine Learning 時建立的所有成品。 在本節中,我們將連線到您將執行部署工作的工作區。
在下列程式碼中傳入訂用帳戶識別碼、工作區、位置和資源群組的值:
az account set --subscription <subscription>
az configure --defaults workspace=<workspace> group=<resource-group> location=<location>
建立定型管線元件
在本節中,我們將建立定型管線所需的所有資產。 首先,我們會建立一個環境,其中包含定型模型所需的程式庫。 接著,我們將建立一個計算叢集,批次部署將在其上執行,而最後,我們會將輸入資料註冊為資料資產。
建立環境
此範例中的元件將會使用具有 XGBoost
和 scikit-learn
程式庫的環境。 environment/conda.yml
檔案包含環境的設定:
environment/conda.yml
channels:
- conda-forge
dependencies:
- python=3.8.5
- pip
- pip:
- mlflow
- azureml-mlflow
- datasets
- jobtools
- cloudpickle==1.6.0
- dask==2023.2.0
- scikit-learn==1.1.2
- xgboost==1.3.3
- pandas==1.4
name: mlflow-env
建立環境,如下所示:
定義環境:
environment/xgboost-sklearn-py38.yml
$schema: https://azuremlschemas.azureedge.net/latest/environment.schema.json name: xgboost-sklearn-py38 image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest conda_file: conda.yml description: An environment for models built with XGBoost and Scikit-learn.
建立環境:
建立計算叢集
批次端點和部署都會在計算叢集上執行。 它們可在工作區中現有的任何 Azure Machine Learning 計算叢集上執行。 因此,多個批次部署可以共用相同的計算基礎結構。 在此範例中,我們將處理名為 batch-cluster
的 Azure Machine Learning 計算叢集。 我們須確認計算存在於工作區上,否則要另外建立。
az ml compute create -n batch-cluster --type amlcompute --min-instances 0 --max-instances 5
將定型資料註冊為資料資產
我們的定型資料會以 CSV 檔案表示。 為了模擬更接近生產層級的工作負載,我們會在 heart.csv
檔案中將定型資料註冊為工作區中的資料資產。 此資料資產稍後會以端點的輸入表示。
az ml data create --name heart-classifier-train --type uri_folder --path data/train
建立管線
我們想要操作的管線會採用一項輸入、定型資料,並產生三項輸出:已定型的模型、評估結果,以及前處理時套用的資料轉換。 管線包含兩個元件:
preprocess_job
:此步驟會讀取輸入資料,並傳回備妥的資料和套用的轉換。 此步驟會收到三項輸入:data
:包含要轉換和評分之輸入資料的資料夾transformations
:(選擇性) 將要套用之轉換的路徑 (如果可用)。 如果未提供路徑,則會從輸入資料學習轉換。 由於transformations
輸入是選擇性的,因此preprocess_job
元件可以在定型和評分期間使用。categorical_encoding
:類別特徵的編碼策略 (ordinal
或onehot
)。
train_job
:此步驟會根據備妥的資料定型 XGBoost 模型,並傳回評估結果和定型的模型。 此步驟會收到三項輸入:data
:前處理的資料。target_column
:我們想要預測的資料行。eval_size
:表示用於評估的輸入資料比例。
管線組態定義於 deployment-ordinal/pipeline.yml
檔案中:
deployment-ordinal/pipeline.yml
$schema: https://azuremlschemas.azureedge.net/latest/pipelineComponent.schema.json
type: pipeline
name: uci-heart-train-pipeline
display_name: uci-heart-train
description: This pipeline demonstrates how to train a machine learning classifier over the UCI heart dataset.
inputs:
input_data:
type: uri_folder
outputs:
model:
type: mlflow_model
mode: upload
evaluation_results:
type: uri_folder
mode: upload
prepare_transformations:
type: uri_folder
mode: upload
jobs:
preprocess_job:
type: command
component: ../components/prepare/prepare.yml
inputs:
data: ${{parent.inputs.input_data}}
categorical_encoding: ordinal
outputs:
prepared_data:
transformations_output: ${{parent.outputs.prepare_transformations}}
train_job:
type: command
component: ../components/train_xgb/train_xgb.yml
inputs:
data: ${{parent.jobs.preprocess_job.outputs.prepared_data}}
target_column: target
register_best_model: false
eval_size: 0.3
outputs:
model:
mode: upload
type: mlflow_model
path: ${{parent.outputs.model}}
evaluation_results:
mode: upload
type: uri_folder
path: ${{parent.outputs.evaluation_results}}
注意
在 pipeline.yml
檔案中,preprocess_job
中遺漏 transformations
輸入;因此,指令碼會從輸入資料學習轉換參數。
管線的視覺效果如下所示:
測試管線
讓我們使用一些範例資料來測試管線。 請使用管線和先前建立的 batch-cluster
計算叢集來建立作業,以便進行測試。
下列 pipeline-job.yml
檔案包含管線作業的組態:
deployment-ordinal/pipeline-job.yml
$schema: https://azuremlschemas.azureedge.net/latest/pipelineJob.schema.json
type: pipeline
experiment_name: uci-heart-train-pipeline
display_name: uci-heart-train-job
description: This pipeline demonstrates how to train a machine learning classifier over the UCI heart dataset.
compute: batch-cluster
component: pipeline.yml
inputs:
input_data:
type: uri_folder
outputs:
model:
type: mlflow_model
mode: upload
evaluation_results:
type: uri_folder
mode: upload
prepare_transformations:
mode: upload
建立測試作業:
az ml job create -f deployment-ordinal/pipeline-job.yml --set inputs.input_data.path=azureml:heart-classifier-train@latest
建立批次端點
提供端點名稱。 批次端點的名稱在每個區域中都不得重複,因為該名稱會用於建構叫用 URI。 若要確保名稱不重複,請將任何尾端字元附加至下列程式碼中指定的名稱。
設定端點:
建立端點:
查詢端點 URI:
部署管線元件
若要部署管線元件,我們必須建立批次部署。 部署是託管執行實際工作之資產所需的一組資源。
設定部署:
deployment-ordinal/deployment.yml
檔案包含部署的設定。 您可以檢查完整的批次端點 YAML 結構描述,以取得額外的屬性。deployment-ordinal/deployment.yml
$schema: https://azuremlschemas.azureedge.net/latest/pipelineComponentBatchDeployment.schema.json name: uci-classifier-train-xgb description: A sample deployment that trains an XGBoost model for the UCI dataset. endpoint_name: uci-classifier-train type: pipeline component: pipeline.yml settings: continue_on_step_failure: false default_compute: batch-cluster
建立部署:
您的部署已可供使用。
測試部署
建立部署後,即可接收作業。 請遵循下列步驟加以測試:
我們的部署需要我們指出一項資料輸入。
inputs.yml
檔案包含輸入資料資產的定義:inputs.yml
inputs: input_data: type: uri_folder path: azureml:heart-classifier-train@latest
提示
若要深入了解如何指出輸入,請參閱建立批次端點的作業和輸入資料 (機器翻譯)。
您可以叫用預設部署,如下所示:
您可以使用以下命令監視記錄的顯示和串流進度:
值得一提的是,只有管線的輸入會發佈為批次端點中的輸入。 例如,categorical_encoding
是管線步驟的輸入,但不是管線本身的輸入。 使用此事實來控制您想對用戶端公開的輸入,以及您想要隱藏的輸入。
存取作業輸出
作業完成後,我們可以存取其部分輸出。 此管線會為其元件產生下列輸出:
preprocess job
:輸出為transformations_output
train job
:輸出為model
和evaluation_results
您可以使用以下內容下載相關聯的結果:
az ml job download --name $JOB_NAME --output-name transformations
az ml job download --name $JOB_NAME --output-name model
az ml job download --name $JOB_NAME --output-name evaluation_results
在端點中建立新的部署
端點可以同時裝載多個部署,同時只保留一項部署作為預設值。 因此,您可逐一查看不同的模型、將不同的模型部署到您的端點並進行測試,最後將預設部署切換至最適合您的模型部署。
讓我們變更管線中前處理的方式,以查看我們是否取得效能更佳的模型。
變更管線前處理元件中的參數
前處理元件具有稱為 categorical_encoding
的輸入,其值為 ordinal
或 onehot
。 這些值對應至兩種不同的類別特徵編碼方式。
ordinal
:使用來自[1:n]
的數值 (序數) 編碼特徵值,其中n
是特徵中的類別數目。 序數編碼表示特徵類別之間有自然的排名順序。onehot
:不表示自然排序關聯性,但如果類別數目很大,則會產生維度問題。
根據預設,我們先前使用 ordinal
。 現在讓我們變更類別編碼以使用 onehot
,並查看模型的執行方式。
提示
或者,我們可以將 categorial_encoding
輸入公開給用戶端,作為管線作業本身的輸入。 不過,我們選擇在前處理步驟中變更參數值,以便我們隱藏和控制部署內的參數,並利用在相同端點下有多項部署的機會。
修改管線。 如下所示:
管線組態定義於
deployment-onehot/pipeline.yml
檔案中:deployment-onehot/pipeline.yml
$schema: https://azuremlschemas.azureedge.net/latest/pipelineComponent.schema.json type: pipeline name: uci-heart-train-pipeline display_name: uci-heart-train description: This pipeline demonstrates how to train a machine learning classifier over the UCI heart dataset. inputs: input_data: type: uri_folder outputs: model: type: mlflow_model mode: upload evaluation_results: type: uri_folder mode: upload prepare_transformations: type: uri_folder mode: upload jobs: preprocess_job: type: command component: ../components/prepare/prepare.yml inputs: data: ${{parent.inputs.input_data}} categorical_encoding: onehot outputs: prepared_data: transformations_output: ${{parent.outputs.prepare_transformations}} train_job: type: command component: ../components/train_xgb/train_xgb.yml inputs: data: ${{parent.jobs.preprocess_job.outputs.prepared_data}} target_column: target eval_size: 0.3 outputs: model: type: mlflow_model path: ${{parent.outputs.model}} evaluation_results: type: uri_folder path: ${{parent.outputs.evaluation_results}}
設定部署:
deployment-onehot/deployment.yml
檔案包含部署的設定。 您可以檢查完整的批次端點 YAML 結構描述,以取得額外的屬性。deployment-onehot/deployment.yml
$schema: https://azuremlschemas.azureedge.net/latest/pipelineComponentBatchDeployment.schema.json name: uci-classifier-train-onehot description: A sample deployment that trains an XGBoost model for the UCI dataset using onehot encoding for variables. endpoint_name: uci-classifier-train type: pipeline component: pipeline.yml settings: continue_on_step_failure: false default_compute: batch-cluster
建立部署:
您的部署已可供使用。
測試非預設部署
建立部署後,即可接收作業。 我們可以先前的相同方式進行測試,但我們現在會叫用特定的部署:
如下所示叫用部署,並指定部署參數來觸發特定的部署
uci-classifier-train-onehot
:您可以使用以下命令監視記錄的顯示和串流進度:
將新的部署設定為預設部署
一旦我們對新部署的效能感到滿意,我們就可以將這項新部署設定為預設值:
az ml batch-endpoint update --name $ENDPOINT_NAME --set defaults.deployment_name=$DEPLOYMENT_NAME
刪除舊的部署
完成後,如果您不再需要舊部署,即可加以刪除:
az ml batch-deployment delete --name uci-classifier-train-xgb --endpoint-name $ENDPOINT_NAME --yes
清除資源
完成後,請從工作區中刪除相關聯的資源:
執行下列程式碼,以刪除批次端點及其基礎部署。 --yes
用來確認刪除。
az ml batch-endpoint delete -n $ENDPOINT_NAME --yes
(選擇性) 刪除計算,除非您打算在稍後的部署中重複使用計算叢集。