適用於:
Python SDK azure-ai-ml v2 (目前)
本教學介紹了 Azure 機器學習服務中一些最常用的功能。 你建立、註冊並部署一個模型。 本教學課程協助您熟悉 Azure Machine Learning 的核心概念及其最常見的使用方式。
您將了解如何在可擴充的計算資源上執行訓練作業,接著進行部署,最後測試該部署。
您會建立訓練指令碼來處理資料準備、模型訓練和模型註冊。 訓練好模型後,你將其部署為 端點,然後呼叫端點進行 推論。
您採取的步驟是︰
- 設定 Azure Machine Learning 工作區的控制代碼
- 建立您的定型指令碼
- 建立可擴充的計算資源、計算叢集
- 建立並執行一個指令工作,在計算叢集上執行訓練腳本,並設定適當的工作環境
- 檢視訓練指令碼的輸出
- 將新訓練的模型部署為端點
- 呼叫 Azure Machine Learning 端點以進行推斷
想了解這個快速入門步驟的概述,請觀看這支影片。
先決條件
-
若要使用 Azure Machine Learning,,您需要工作區。 如果您沒有工作區,請完成建立要開始使用所需要的資源以建立工作區,並深入了解其使用方式。
重要事項
如果您的 Azure Machine Learning 工作區是使用受控虛擬網路設定的,您可能需要新增輸出規則,以允許存取公用 Python 套件存放庫。 如需詳細資訊,請參閱案例:存取公用機器學習套件。
-
登入工作室,並選取您的工作區 (如果其尚未開啟的話)。
-
在您的工作區中打開或建立一個筆記本:
設定您的核心程序並在 Visual Studio Code (VS Code) 中開啟
在已開啟的筆記本上方工具列中,如果您尚未有計算執行個體,請建立一個。
如果計算執行個體已停止,請選取 [啟動計算],並等到其執行為止。
等到計算執行個體正在執行中。 然後確定位於右上方的核心程序是
Python 3.10 - SDK v2。 如果沒有,請使用下拉式清單來選取此核心程序。如果未看到此核心程序,請確認您的計算執行個體正在執行中。 如果是,請選取筆記本右上方的 [重新整理] 按鈕。
如果您看到橫幅指出您需要進行驗證,請選取 [驗證]。
您可以在此執行筆記本,或在 VS Code 中予以開啟,以取得包含 Azure Machine Learning 資源強大功能的完全整合式開發環境 (IDE)。 選取 [在 VS Code 中開啟],然後選取 Web 或桌面選項。 以這種方式啟動時,VS Code 會附加至您的計算執行個體、核心程序和工作區檔案系統。
重要事項
本教學課程的其餘部分,包含教學課程筆記本的程式單元。 複製它們並將其貼入新的筆記本中,或者如果您已複製筆記本,請立即切換至該筆記本。
建立工作區的控制代碼
在深入研究程式碼之前,您需要一種方法來參考您的工作區。 工作區是 Azure Machine Learning 的最上層資源,其提供一個集中位置來處理您在使用 Azure Machine Learning 時建立的所有成品。
建立 ml_client 作為工作區的控制代碼。 用 ml_client 來管理資源和工作。
在下一個儲存格中,輸入您的訂用帳戶識別碼、資源群組名稱和工作區名稱。 若要尋找這些值:
- 在右上方的 Azure Machine Learning 工作室工具列中,選取您的工作區名稱。
- 將工作區、資源群組和訂用帳戶識別碼的值複製到程式碼。
- 你需要複製一個值,關閉該區域,然後貼上。 然後再回來取得下一個值。
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
# authenticate
credential = DefaultAzureCredential()
SUBSCRIPTION = "<SUBSCRIPTION_ID>"
RESOURCE_GROUP = "<RESOURCE_GROUP>"
WS_NAME = "<AML_WORKSPACE_NAME>"
# Get a handle to the workspace
ml_client = MLClient(
credential=credential,
subscription_id=SUBSCRIPTION,
resource_group_name=RESOURCE_GROUP,
workspace_name=WS_NAME,
)
附註
建立 MLClient 並不會連接到工作區。 用戶端初始化是惰性的。 它會等到第一次需要呼叫時才執行。 這個動作會在下一個程式碼格發生。
# Verify that the handle works correctly.
# If you ge an error here, modify your SUBSCRIPTION, RESOURCE_GROUP, and WS_NAME in the previous cell.
ws = ml_client.workspaces.get(WS_NAME)
print(ws.location, ":", ws.resource_group)
建立訓練指令碼
建立訓練腳本,也就是 main.py Python 檔案。
首先,建立一個腳本的來源資料夾:
import os
train_src_dir = "./src"
os.makedirs(train_src_dir, exist_ok=True)
這個腳本會預處理資料,並將其拆分成測試資料集和訓練資料集。 它利用這些資料訓練樹狀模型,並回傳輸出模型。
在管線運行期間,使用 MLFlow 記錄參數與指標。
接下來的儲存格會用 IPython 的魔法把訓練腳本寫入你剛建立的目錄。
%%writefile {train_src_dir}/main.py
import os
import argparse
import pandas as pd
import mlflow
import mlflow.sklearn
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
def main():
"""Main function of the script."""
# input and output arguments
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=str, help="path to input data")
parser.add_argument("--test_train_ratio", type=float, required=False, default=0.25)
parser.add_argument("--n_estimators", required=False, default=100, type=int)
parser.add_argument("--learning_rate", required=False, default=0.1, type=float)
parser.add_argument("--registered_model_name", type=str, help="model name")
args = parser.parse_args()
# Start Logging
mlflow.start_run()
# enable autologging
mlflow.sklearn.autolog()
###################
#<prepare the data>
###################
print(" ".join(f"{k}={v}" for k, v in vars(args).items()))
print("input data:", args.data)
credit_df = pd.read_csv(args.data, header=1, index_col=0)
mlflow.log_metric("num_samples", credit_df.shape[0])
mlflow.log_metric("num_features", credit_df.shape[1] - 1)
train_df, test_df = train_test_split(
credit_df,
test_size=args.test_train_ratio,
)
####################
#</prepare the data>
####################
##################
#<train the model>
##################
# Extracting the label column
y_train = train_df.pop("default payment next month")
# convert the dataframe values to array
X_train = train_df.values
# Extracting the label column
y_test = test_df.pop("default payment next month")
# convert the dataframe values to array
X_test = test_df.values
print(f"Training with data of shape {X_train.shape}")
clf = GradientBoostingClassifier(
n_estimators=args.n_estimators, learning_rate=args.learning_rate
)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))
###################
#</train the model>
###################
##########################
#<save and register model>
##########################
# Registering the model to the workspace
print("Registering the model via MLFlow")
# pin numpy
conda_env = {
'name': 'mlflow-env',
'channels': ['conda-forge'],
'dependencies': [
'python=3.10.15',
'pip<=21.3.1',
{
'pip': [
'mlflow==2.17.0',
'cloudpickle==2.2.1',
'pandas==1.5.3',
'psutil==5.8.0',
'scikit-learn==1.5.2',
'numpy==1.26.4',
]
}
],
}
mlflow.sklearn.log_model(
sk_model=clf,
registered_model_name=args.registered_model_name,
artifact_path=args.registered_model_name,
conda_env=conda_env,
)
# Saving the model to a file
mlflow.sklearn.save_model(
sk_model=clf,
path=os.path.join(args.registered_model_name, "trained_model"),
)
###########################
#</save and register model>
###########################
# Stop Logging
mlflow.end_run()
if __name__ == "__main__":
main()
當模型被訓練時,腳本會將模型檔案儲存並註冊到工作區。 你可以用註冊模型來推論端點。
您可能需要選取 [重新整理],才能在 [檔案] 中看到新的資料夾和指令碼。
設定命令
你現在有一個腳本可以執行所需的任務,還有一個計算叢集來執行這個腳本。 使用一個通用 指令 ,能執行命令列操作。 此命令列動作可直接呼叫系統命令或執行指令碼。
建立輸入變數以指定輸入資料、分割比率、學習率及註冊型號名稱。 命令指令碼:
- 使用定義訓練腳本所需軟體與執行時函式庫的 環境 。 Azure Machine Learning 提供許多策劃好或現成的環境,適用於常見的訓練和推斷案例。 您在這裡使用其中一個環境。 在教學課程:在 Azure Machine Learning 中訓練模型,您會了解如何建立自訂環境。
- 設定命令列動作本身 -
python main.py在本案例中。 輸入與輸出可透過${{ ... }}指令符號存取。 - 從網路檔案存取資料。
- 因為你沒指定運算資源,腳本會在一個自動建立的 無伺服器運算叢集 上執行。
from azure.ai.ml import command
from azure.ai.ml import Input
registered_model_name = "credit_defaults_model"
job = command(
inputs=dict(
data=Input(
type="uri_file",
path="https://azuremlexamples.blob.core.windows.net/datasets/credit_card/default_of_credit_card_clients.csv",
),
test_train_ratio=0.2,
learning_rate=0.25,
registered_model_name=registered_model_name,
),
code="./src/", # location of source code
command="python main.py --data ${{inputs.data}} --test_train_ratio ${{inputs.test_train_ratio}} --learning_rate ${{inputs.learning_rate}} --registered_model_name ${{inputs.registered_model_name}}",
environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
display_name="credit_default_prediction",
)
提交工作
提交作業以在 Azure Machine Learning 中執行。 這次,請在 create_or_update 上使用 ml_client。
ml_client.create_or_update(job)
檢視作業輸出並等候作業完成
在 Azure Machine Learning 工作室中查看作業,方法是選取前一個程式單元輸出的連結。
此作業的輸出在 Azure Machine Learning 工作室中看起來會像這樣。 瀏覽分頁,查看各種細節,如指標、產出等。 完成後,這項工作會將模型註冊到你的工作區,這是訓練後的結果。
重要事項
請等到作業狀態完成後,再返回此筆記本繼續作業。 這項工作只需兩到三分鐘完成。 如果運算叢集縮小到零節點且自訂環境仍在建置中,可能需要更久(最多 10 分鐘)。
將模型部署為線上端點
請使用 online endpoint 將您的機器學習模型部署為 Azure 雲中的網路服務。
要部署機器學習服務,請使用你註冊的模型。
建立新的線上端點
現在你註冊了模型,建立你的線上端點。 端點名稱在整個 Azure 區域中必須是唯一的。 在這個教學中,請使用 UUID 來創建一個唯一的名稱。
import uuid
# Creating a unique name for the endpoint
online_endpoint_name = "credit-endpoint-" + str(uuid.uuid4())[:8]
建立端點。
# Expect the endpoint creation to take a few minutes
from azure.ai.ml.entities import (
ManagedOnlineEndpoint,
ManagedOnlineDeployment,
Model,
Environment,
)
# create an online endpoint
endpoint = ManagedOnlineEndpoint(
name=online_endpoint_name,
description="this is an online endpoint",
auth_mode="key",
tags={
"training_dataset": "credit_defaults",
"model_type": "sklearn.GradientBoostingClassifier",
},
)
endpoint = ml_client.online_endpoints.begin_create_or_update(endpoint).result()
print(f"Endpoint {endpoint.name} provisioning state: {endpoint.provisioning_state}")
附註
建立端點預計要幾分鐘的時間。
建立端點後,請依以下程式碼取得:
endpoint = ml_client.online_endpoints.get(name=online_endpoint_name)
print(
f'Endpoint "{endpoint.name}" with provisioning state "{endpoint.provisioning_state}" is retrieved'
)
將模型部署至該端點
建立端點後,使用輸入腳本部署模型。 每個端點可以有多個部署。 你可以指定規則來引導流量到這些部署。 在這個例子中,你建立一個單一部署,處理 100% 的進站流量。 為部署選擇顏色名稱,例如 藍色、 綠色或 紅色。 這個選擇是任意的。
要找到你註冊模型的最新版本,請在 Azure Machine Learning Studio 的 模型 頁面查詢。 或者,使用以下程式碼取得最新的版本號。
# Let's pick the latest version of the model
latest_model_version = max(
[int(m.version) for m in ml_client.models.list(name=registered_model_name)]
)
print(f'Latest model is version "{latest_model_version}" ')
部署模型的最新版本。
# picking the model to deploy. Here we use the latest version of our registered model
model = ml_client.models.get(name=registered_model_name, version=latest_model_version)
# Expect this deployment to take approximately 6 to 8 minutes.
# create an online deployment.
# if you run into an out of quota error, change the instance_type to a comparable VM that is available.
# Learn more on https://azure.microsoft.com/pricing/details/machine-learning/.
blue_deployment = ManagedOnlineDeployment(
name="blue",
endpoint_name=online_endpoint_name,
model=model,
instance_type="Standard_DS3_v2",
instance_count=1,
)
blue_deployment = ml_client.begin_create_or_update(blue_deployment).result()
附註
完成此部署約需要 6 到 8 分鐘的時間。
部署結束後,你就準備好測試它了。
使用範例查詢進行測試
在你將模型部署到端點後,再用模型來執行推論。
建立一個符合分數腳本中 run 方法所預期設計的範例請求檔案。
deploy_dir = "./deploy"
os.makedirs(deploy_dir, exist_ok=True)
%%writefile {deploy_dir}/sample-request.json
{
"input_data": {
"columns": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],
"index": [0, 1],
"data": [
[20000,2,2,1,24,2,2,-1,-1,-2,-2,3913,3102,689,0,0,0,0,689,0,0,0,0],
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8]
]
}
}
# test the blue deployment with some sample data
ml_client.online_endpoints.invoke(
endpoint_name=online_endpoint_name,
request_file="./deploy/sample-request.json",
deployment_name="blue",
)
清除資源
如果你不需要端點,刪除它以停止使用該資源。 在刪除端點之前,請確保沒有其他部署正在使用該端點。
附註
完成刪除約需 20 分鐘的時間。
ml_client.online_endpoints.begin_delete(name=online_endpoint_name)
停止計算執行個體
如果你現在不需要,請停止運算實例:
- 在 Studio 的左窗格中,選取 [計算]。
- 在頂端索引標籤中,選取 [計算執行個體]。
- 選取清單中的計算執行個體。
- 在頂端工具列中,選取 [停止]。
刪除所有資源
重要事項
您所建立的資源可用來作為其他 Azure Machine Learning 教學課程和操作說明文章的先決條件。
如果不打算使用所建立的任何資源,請予以刪除以免產生任何費用:
在 Azure 入口網站的搜尋方塊中,輸入資源群組,然後從結果中選取它。
從清單中,選取您所建立的資源群組。
在 [概觀] 頁面上,選取 [刪除資源群組]。
輸入資源群組名稱。 然後選取 [刪除]。
後續步驟
現在您已對模型的訓練與部署流程有初步概念,接下來可以透過這些教學課程進一步了解整個過程:
| 教學課程 | 描述 |
|---|---|
| 在 Azure Machine Learning 中上傳、存取和探索資料 | 將大型資料儲存在雲端,並可從筆記本與指令碼中擷取 |
| 雲端工作站上的模型開發 | 開始原型設計和開發機器學習模型 |
| 在 Azure Machine Learning 中訓練模型 | 深入探討訓練模型的詳細資料 |
| 將模型部署為線上端點 | 深入了解部署模型的詳細資料 |
| 建立生產機器學習管線 | 將完整的機器學習工作分割成多步驟工作流程。 |