共用方式為


快速入門:開始使用 Azure Machine Learning

適用於Python SDK azure-ai-ml v2 (目前)

本教學介紹了 Azure 機器學習服務中一些最常用的功能。 你建立、註冊並部署一個模型。 本教學課程協助您熟悉 Azure Machine Learning 的核心概念及其最常見的使用方式。

您會了解如何在可調整的計算資源上執行定型作業,然後加以部署,最後測試部署。

您建立定型指令碼來處理資料準備、定型和註冊模型。 訓練好模型後,你將其部署為 端點,然後呼叫端點進行 推論

您採取的步驟如下︰

  • 設定 Azure Machine Learning 工作區的控制代碼
  • 建立您的定型指令碼
  • 建立可調整的計算資源、計算叢集
  • 建立並執行一個指令工作,在計算叢集上執行訓練腳本,並設定適當的工作環境
  • 檢視訓練指令碼的輸出
  • 將新定型的模型部署為端點
  • 呼叫 Azure Machine Learning 端點以進行推斷

想了解這個快速入門步驟的概述,請觀看這支影片。

必要條件

  1. 若要使用 Azure 機器學習,您需要工作區。 如果您沒有工作區,請完成建立要開始使用所需要的資源以建立工作區,並深入了解其使用方式。

    重要

    如果您的 Azure 機器學習 工作區是使用受控虛擬網路設定的,您可能需要新增輸出規則,以允許存取公用 Python 套件存放庫。 如需詳細資訊,請參閱 案例:存取公用機器學習套件

  2. 登入工作室,並選取您的工作區 (如果其尚未開啟的話)。

  3. 在工作區開啟或建立筆記本:

    • 如果您想要將程式代碼複製並貼到儲存格中,請建立 新的筆記本
    • 或者,從工作室的 [範例] 區段開啟 tutorials/get-started-notebooks/quickstart.ipynb。 然後選取 [複製] 以將筆記本新增至 [檔案]。 若要尋找範例筆記本,請參閱 從範例筆記本學習。

設定您的核心並在 Visual Studio Code (VS Code) 中開啟

  1. 在開啟的筆記本上方的頂端列上,如果您還沒有計算執行個體,請建立計算執行個體。

    螢幕擷取畫面顯示如何建立計算執行個體。

  2. 如果計算執行個體已停止,請選取 [啟動計算],並等到其執行為止。

    顯示如何啟動已停止計算執行個體的螢幕擷取畫面。

  3. 等候計算實例正在執行。 然後確定位於右上方的核心是 Python 3.10 - SDK v2。 如果沒有,請使用下拉式清單來選取此核心。

    螢幕擷取畫面顯示如何設定核心。

    如果您沒有看到此核心,請確認您的計算實例正在執行。 如果是,請選取 筆記本右上方的 [重新 整理] 按鈕。

  4. 如果您看到橫幅指出您需要進行驗證,請選取 [驗證]

  5. 您可以在此執行筆記本,或在 VS Code 中予以開啟,以取得包含 Azure Machine Learning 資源強大功能的完全整合式開發環境 (IDE)。 選取 [在 VS Code 中開啟],然後選取 Web 或桌面選項。 以這種方式啟動時,VS Code 會附加至您的計算執行個體、核心和工作區檔案系統。

    顯示如何在 VS Code 中開啟筆記本的螢幕擷取畫面。

重要

本教學課程的其餘部分包含教學課程筆記本的儲存格。 複製並貼到新的筆記本中,或者如果您複製筆記本,請立即切換至筆記本。

建立工作區的控制代碼

在深入研究程式碼之前,您需要一種方法來參考您的工作區。 工作區是 Azure Machine Learning 的最上層資源,其提供一個集中位置來處理您在使用 Azure Machine Learning 時建立的所有成品。

建立 ml_client 為工作區的 handle 。 用 ml_client 來管理資源和工作。

在下一個儲存格中,輸入您的訂用帳戶識別碼、資源群組名稱和工作區名稱。 若要尋找這些值:

  1. 在右上方的 Azure Machine Learning 工作室工具列中,選取您的工作區名稱。
  2. 將工作區、資源群組和訂用帳戶識別碼的值複製到程式碼。
  3. 你需要複製一個值,關閉該區域,然後貼上。 然後再回來拿下一個數值。

螢幕擷取畫面:在工具列右上角尋找程式碼的認證。

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)

停止計算執行個體

如果你現在不需要,請停止運算實例:

  1. 在 Studio 的左窗格中,選取 [計算]。
  2. 在頂端索引標籤中,選取 [計算執行個體]
  3. 選取清單中的計算執行個體。
  4. 在頂端工具列中,選取 [停止]

刪除所有資源

重要

您所建立的資源可用來作為其他 Azure Machine Learning 教學課程和操作說明文章的先決條件。

如果不打算使用您建立的任何資源,請刪除以免產生任何費用:

  1. 在 [Azure 入口網站] 的搜尋方塊中,輸入 [資源群組],然後從結果中選取它。

  2. 從清單中,選取您所建立的資源群組。

  3. 在 [概觀] 頁面上,選取 [刪除資源群組]

    在 Azure 入口網站中刪除資源群組選項的螢幕擷取畫面。

  4. 輸入資源群組名稱。 接著選取刪除

下一步

現在您對於定型和部署模型所涉及的項目已經有概念,接下來請深入了解這些教學課程中的程序:

教學課程 描述
在 Azure Machine Learning 中上傳、存取及探索資料 將大型資料儲存在雲端中,可從筆記本和指令碼擷取
雲端工作站上的模型開發 開始原型設計和開發機器學習模型
在 Azure Machine Learning 中為模型定型 深入探討為模型定型的詳細資料
將模型部署為線上端點 深入了解部署模型的詳細資料
建立生產機器學習管線 將完整的機器學習工作分割成多步驟工作流程。