使用 Azure Pipelines 搭配 Azure Machine Learning

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

您可以使用 Azure DevOps 管線,將機器學習生命週期自動化。 您可以自動化的一些作業包括:

  • 資料準備 (擷取、轉換、載入作業)
  • 使用隨選相應放大和相應增加來定型機器學習模型
  • 將機器學習模型部署為公用或私人 Web 服務
  • 監視已部署的機器學習模型 (例如效能或資料漂移分析)

本文會教您如何建立 Azure 管線,以將機器學習模型建置並部署到 Azure Machine Learning

本教學課程使用 Azure Machine Learning Python SDK v2Azure CLI ML 延伸模組 v2

必要條件

步驟 1:取得程式碼

在 GitHub 派生下列存放庫:

https://github.com/azure/azureml-examples

步驟 2:登入 Azure Pipelines

登入 Azure Pipelines。 登入之後,您的瀏覽器會前往 https://dev.azure.com/my-organization-name 並顯示您的 Azure DevOps 儀表板。

在您選取的組織內,建立一個「專案」。 如果您的組織中沒有任何專案,您會看到 [建立專案開始使用] 畫面。 否則,請選取儀表板右上角的 [新建專案] 按鈕。

步驟 3:建立服務連線

您可以使用現有的服務連線。

您需要 Azure Resource Manager 連線,才能使用 Azure 入口網站進行驗證。

  1. 在 Azure DevOps 中,選取 [專案設定] 頁面,然後開啟 [服務連線] 頁面。

  2. 選擇 [+ 新增服務連線],然後選取 Azure Resource Manager

  3. 選取預設驗證方法、服務主體 (自動)

  4. 建立服務連線。 設定您慣用的範圍層級、訂用帳戶、資源群組和連線名稱。

    Screenshot of ARM service connection.

步驟 4:建立管線

  1. 前往 [管線],然後選取 [新增管線]

  2. 執行精靈的步驟,首先選取 [GitHub] 作為您的原始程式碼位置。

  3. 系統可能會將您重新導向至 GitHub 以進行登入。 若是如此,請輸入 GitHub 認證。

  4. 當您看到存放庫清單時,請選取您的存放庫。

  5. 系統可能會將您重新導向至 GitHub,以安裝 Azure Pipelines 應用程式。 如果發生此情況,請選取 [核准並安裝]

  6. 選取 [入門管線]。 您將更新入門管線範本。

步驟 5:建置 YAML 管線以提交 Azure Machine Learning 作業

刪除入門管線,並將其取代為下列 YAML 程式碼。 在此管線中,您將:

  • 使用 Python 版本工作來設定 Python 3.8 並安裝 SDK 需求。
  • 使用 Bash 工作,來執行 Azure Machine Learning SDK 和 CLI 的 Bash 指令碼。
  • 使用 Azure CLI 工作提交 Azure Machine Learning 作業。

視您使用 Azure Resource Manager 服務連線或一般服務連線而定,選取下列索引標籤。 在管線 YAML 中,使用您的資源來取代變數的值。

name: submit-azure-machine-learning-job

trigger:
- none

variables:
  service-connection: 'machine-learning-connection' # replace with your service connection name
  resource-group: 'machinelearning-rg' # replace with your resource group name
  workspace: 'docs-ws' # replace with your workspace name

jobs:
- job: SubmitAzureMLJob
  displayName: Submit AzureML Job
  timeoutInMinutes: 300
  pool:
    vmImage: ubuntu-latest
  steps:
  - task: UsePythonVersion@0
    displayName: Use Python >=3.8
    inputs:
      versionSpec: '>=3.8'

  - bash: |
      set -ex

      az version
      az extension add -n ml
    displayName: 'Add AzureML Extension'

  - task: AzureCLI@2
    name: submit_azureml_job_task
    displayName: Submit AzureML Job Task
    inputs:
      azureSubscription: $(service-connection)
      workingDirectory: 'cli/jobs/pipelines-with-components/nyc_taxi_data_regression'
      scriptLocation: inlineScript
      scriptType: bash
      inlineScript: |
      
        # submit component job and get the run name
        job_name=$(az ml job create --file single-job-pipeline.yml -g $(resource-group) -w $(workspace) --query name --output tsv)

        # Set output variable for next task
        echo "##vso[task.setvariable variable=JOB_NAME;isOutput=true;]$job_name"

步驟 6:等待 Azure Machine Learning 作業完成

在步驟 5 中,您已新增一個作業以提交 Azure Machine Learning 作業。 在此步驟中,您會新增另一個作業以等待 Azure Machine Learning 作業完成。

如果您使用 Azure Resource Manager 服務連線,您可以使用「Machine Learning」延伸模組。 您可以在 Azure DevOps 延伸模組 Marketplace 中搜尋此延伸模組,或直接移至延伸模組。 安裝「Machine Learning」延伸模組。

重要

請勿錯誤地安裝 Machine Learning (傳統) 延伸模組;這是較舊的延伸模組,未提供相同的功能。

在 [管線檢閱] 視窗中,新增伺服器作業。 在作業的步驟部分,選取 [顯示助理],然後搜尋 AzureML。 選取 [AzureML 作業等待] 工作,並填入作業的資訊。

工作有四個輸入:Service ConnectionAzure Resource Group NameAzureML Workspace NameAzureML Job Name。 填入這些輸入。 這些步驟產生的 YAML 會類似下列範例:

注意

  • Azure Machine Learning 作業等待工作會在伺服器作業上執行,這不會耗用昂貴的代理程式集區資源,而且不需要額外費用。 伺服器作業 (以 pool: server 表示) 在與管線相同的機器上執行。 如需詳細資訊,請參閱伺服器作業 (部分機器翻譯)。
  • 一個 Azure Machine Learning 作業等待作業只能等待一個作業。 您必須為您想要等待的每個作業設定個別的作業。
  • Azure Machine Learning 作業等待工作最多可以等待 2 天。 這是 Azure DevOps Pipelines 所設定的硬性限制。
- job: WaitForAzureMLJobCompletion
  displayName: Wait for AzureML Job Completion
  pool: server
  timeoutInMinutes: 0
  dependsOn: SubmitAzureMLJob
  variables: 
    # We are saving the name of azureMl job submitted in previous step to a variable and it will be used as an inut to the AzureML Job Wait task
    azureml_job_name_from_submit_job: $[ dependencies.SubmitAzureMLJob.outputs['submit_azureml_job_task.JOB_NAME'] ] 
  steps:
  - task: AzureMLJobWaitTask@1
    inputs:
      serviceConnection: $(service-connection)
      resourceGroupName: $(resource-group)
      azureMLWorkspaceName: $(workspace)
      azureMLJobName: $(azureml_job_name_from_submit_job)

步驟 7:提交管線並確認管線執行

選取儲存並執行。 管線會等待 Azure Machine Learning 作業完成,並以與 Azure Machine Learning 作業相同的狀態來結束 WaitForJobCompletion 下的工作。 例如:Azure Machine Learning 作業 Succeeded == WaitForJobCompletion 作業下的 Azure DevOps 工作 Succeeded Azure Machine Learning 作業 Failed == WaitForJobCompletion 作業下的 Azure DevOps 工作 Failed Azure Machine Learning 作業 Cancelled == WaitForJobCompletion 作業下的 Azure DevOps 工作 Cancelled

提示

您可以在 Azure Machine Learning 工作室中檢視完整的 Azure Machine Learning 作業。

清除資源

如果您不打算繼續使用管線,請刪除 Azure DevOps 專案。 在 Azure 入口網站,刪除資源群組和 Azure Machine Learning 執行個體。