使用 Azure Machine Learning CLI、SDK 和 REST API 將模型定型

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

Azure Machine Learning 提供多種方式來提交 ML 定型作業。 在本文中,您將了解如何使用下列方法來提交作業:

  • 適用於機器學習的 Azure CLI 延伸模組:ml 延伸模組也稱為 CLI v2。
  • 適用於 Azure Machine Learning 的 Python SDK v2
  • REST API:CLI 和 SDK 所依據的 API。

必要條件

如若要使用 SDK 資訊,請安裝適用於 Python SDK v2 的 Azure Machine Learning。

複製範例存放庫

本文中的程式碼片段是以 Azure Machine Learning 範例 GitHub 存放庫中的範例為基礎。 若要將存放庫複製到您的開發環境,請使用下列命令:

git clone --depth 1 https://github.com/Azure/azureml-examples

提示

使用 --depth 1 僅複製存放庫的最新認可,如此可縮短完成作業的時間。

範例作業

本文中的範例會使用鳶尾花資料集來定型 MLFlow 模型。

在雲端中定型

在雲端中定型時,您必須連線至 Azure Machine Learning 工作區,然後選取將用來執行定型作業的計算資源。

1.連線到工作區

提示

使用以下索引標籤來選取您想要用來將模型定型的方法。 選取索引標籤會自動將本文中的所有索引標籤切換至相同索引標籤。您可以隨時選取另一個索引標籤。

若要連線到工作區,您需要識別碼參數 - 訂用帳戶、資源群組和工作區名稱。 您將使用來自 azure.ai.ml 命名空間 MLClient 中的這些資料,以取得所需的 Azure Machine Learning 工作區。 若要進行驗證,請使用預設的 Azure 驗證。 如需如何設定認證並連線至工作區的詳細資料,請參閱此範例 (英文)。

#import required libraries
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

#Enter details of your Azure Machine Learning workspace
subscription_id = '<SUBSCRIPTION_ID>'
resource_group = '<RESOURCE_GROUP>'
workspace = '<AZUREML_WORKSPACE_NAME>'

#connect to the workspace
ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group, workspace)

2.建立用於定型的計算資源

注意

若要嘗試無伺服器計算,請略過此步驟並繼續進行 3。提交定型作業

Azure Machine Learning 計算叢集是完全受控的計算資源,可用來執行定型作業。 在下列範例中,會建立名為 cpu-compute 的計算叢集。

from azure.ai.ml.entities import AmlCompute

# specify aml compute name.
cpu_compute_target = "cpu-cluster"

try:
    ml_client.compute.get(cpu_compute_target)
except Exception:
    print("Creating a new cpu compute target...")
    compute = AmlCompute(
        name=cpu_compute_target, size="STANDARD_D2_V2", min_instances=0, max_instances=4
    )
    ml_client.compute.begin_create_or_update(compute).result()

3.提交訓練作業

若要執行此指令碼,您將使用 command,其會執行位於 ./sdk/python/jobs/single-step/lightgbm/iris/src/ 底下的 main.py Python 指令碼。 此命令會透過將其以 job 的形式提交給 Azure Machine Learning 來加以執行。

注意

若要使用無伺服器計算,請刪除此程式碼中的 compute="cpu-cluster"

from azure.ai.ml import command, Input

# define the command
command_job = command(
    code="./src",
    command="python main.py --iris-csv ${{inputs.iris_csv}} --learning-rate ${{inputs.learning_rate}} --boosting ${{inputs.boosting}}",
    environment="AzureML-lightgbm-3.2-ubuntu18.04-py37-cpu@latest",
    inputs={
        "iris_csv": Input(
            type="uri_file",
            path="https://azuremlexamples.blob.core.windows.net/datasets/iris.csv",
        ),
        "learning_rate": 0.9,
        "boosting": "gbdt",
    },
    compute="cpu-cluster",
)
# submit the command
returned_job = ml_client.jobs.create_or_update(command_job)
# get a URL for the status of the job
returned_job.studio_url

在上面的範例中,您已設定:

  • code:要執行命令的程式碼所在位置的路徑
  • command:要執行的命令
  • environment - 執行定型指令碼所需的環境。 在此範例中,我們使用 Azure Machine Learning 所提供名為 AzureML-lightgbm-3.2-ubuntu18.04-py37-cpu 的策展或現成環境。 我們透過使用 @latest 指示詞使用此環境的最新版本。 您也可以指定基礎映像 Docker 映像,並在其上方指定 conda yaml,以使用自訂環境。
  • inputs:使用命令名稱值組之輸入的字典。 索引鍵是作業內容中的輸入名稱,值則是輸入值。 會使用 ${{inputs.<input_name>}} 運算式在 command 中參考輸入。 若要使用檔案或資料夾作為輸入,您可以使用 Input 類別。 如需詳細資訊,請參閱 SDK 和 CLI v2 運算式

如需詳細資訊,請參閱參考文件

提交作業時,URL 會傳回至 Azure Machine Learning 工作室中的作業狀態。 使用 Studio UI 來檢視作業進度。 您也可以使用 returned_job.status 來檢查作業的目前狀態。

註冊定型的模型。

下列範例示範如何在 Azure Machine Learning 工作區中註冊模型。

提示

定型作業傳回的 name 屬性會作為模型路徑的一部分使用。

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

run_model = Model(
    path="azureml://jobs/{}/outputs/artifacts/paths/model/".format(returned_job.name),
    name="run-model-example",
    description="Model created from run.",
    type=AssetTypes.MLFLOW_MODEL
)

ml_client.models.create_or_update(run_model)

下一步

既然您已將模型定型,請了解如何使用線上端點進行部署

如需更多範例,請參閱 Azure Machine Learning 範例 GitHub 存放庫。

如需本文中使用的 Azure CLI 命令、Python SDK 類別或 REST API 詳細資訊,請參閱下列參考文件: