使用 Azure Machine Learning Python SDK (v1) 訓練模型

適用於:Python SDK azureml v1

了解如何使用 SDK v1,將 Azure 計算資源連結至您的 Azure Machine Learning 工作區。 然後,您可以在機器學習工作中使用這些資源作為定型和推斷計算目標

在本文中,您將了解如何設定您的工作區以使用這些計算資源:

  • 您的本機電腦
  • 遠端虛擬機器
  • Apache Spark 集區 (由 Azure Synapse Analytics 支援)
  • Azure HDInsight
  • Azure Batch
  • Azure Databricks - 僅在機器學習管線中用來作為定型計算目標
  • Azure Data Lake Analytics
  • Azure Container Instance
  • Azure Machine Learning Kubernetes

若要使用受 Azure Machine Learning 管理的計算目標,請參閱:

重要

本文中標示為「預覽」的項目目前處於公開預覽狀態。 此預覽版本會在沒有服務等級協定的情況下提供,不建議用於實際執行工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

必要條件

限制

  • 請勿從您的工作區同時對相同計算建立多個連結。 例如,使用兩個不同的名稱,將一個 Azure Kubernetes Service 叢集連結至工作區。 每個新連結都會中斷先前現有的連結。

    如果您想要重新連結計算目標 (例如,為了變更 TLS 或其他叢集設定),必須先移除現有的連結。

什麼是計算目標?

使用 Azure Machine Learning,您可以在各種資源或環境上 (統稱為計算目標) 將您的模型定型。 計算目標可以是本機電腦或雲端資源,例如 Azure Machine Learning Compute、Azure HDInsight 或遠端虛擬機器。 您也可以使用用於部署模型的計算目標,如模型的部署位置和方法中所述。

本機電腦

當您使用本機電腦進行定型時,不需要建立計算目標。 只需從您的本機電腦提交定型執行

當您針對推斷使用本機電腦時,則必須安裝 Docker。 若要執行部署,請使用 LocalWebservice.deploy_configuration() 來定義 Web 服務將使用的連接埠。 然後使用一般部署程序,如使用 Azure Machine Learning 部署模型中所述。

遠端虛擬機器

Azure Machine Learning 也支援連結 Azure 虛擬機器。 VM 必須是 Azure 資料科學虛擬機器 (DSVM)。 VM 會針對整個生命週期的機器學習開發,提供精心選擇的工具和架構。 如需有關如何使用 DSVM 搭配 Azure Machine Learning 的詳細資訊,請參閱設定開發環境

提示

我們建議使用 Azure Machine Learning Compute 執行個體,而不是遠端 VM。 它是完全受控的雲端式計算解決方案,為 Azure Machine Learning 所特有。 如需詳細資訊,請參閱建立和管理 Azure Machine Learning Compute 執行個體

  1. 建立:Azure Machine Learning 無法為您建立遠端 VM。 相反地,您必須建立 VM,然後將它連結至您的 Azure Machine Learning 工作區。 如需建立 DSVM 的詳細資訊,請參閱佈建適用於 Linux (Ubuntu) 的資料科學虛擬機器

    警告

    Azure Machine Learning 只支援執行 Ubuntu 的虛擬機器。 當您建立 VM 或選擇現有的 VM 時,您必須選取使用 Ubuntu 的 VM。

    Azure Machine Learning 也需要虛擬機器具有公用 IP 位址

  2. 連結:若要連結現有的虛擬機器作為計算目標,您必須提供虛擬機器的資源識別碼、使用者名稱與密碼。 您可以使用訂用帳戶識別碼、資源群組名稱和 VM 名稱,以下列字串格式建構 VM 的資源識別碼:/subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.Compute/virtualMachines/<vm_name>

    from azureml.core.compute import RemoteCompute, ComputeTarget
    
    # Create the compute config 
    compute_target_name = "attach-dsvm"
    
    attach_config = RemoteCompute.attach_configuration(resource_id='<resource_id>',
                                                    ssh_port=22,
                                                    username='<username>',
                                                    password="<password>")
    
    # Attach the compute
    compute = ComputeTarget.attach(ws, compute_target_name, attach_config)
    
    compute.wait_for_completion(show_output=True)
    

    或者,您也可以使用 Azure Machine Learning Studio 將 DSVM 連結至您的工作區。

    警告

    請勿從您的工作區同時對相同 DSVM 建立多個連結。 每個新連結都會中斷先前現有的連結。

  3. 設定:為 DSVM 計算目標建立回合組態。 Docker 和 Conda 用來建立和設定 DSVM 上的定型環境。

    from azureml.core import ScriptRunConfig
    from azureml.core.environment import Environment
    from azureml.core.conda_dependencies import CondaDependencies
    
    # Create environment
    myenv = Environment(name="myenv")
    
    # Specify the conda dependencies
    myenv.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn'])
    
    # If no base image is explicitly specified the default CPU image "azureml.core.runconfig.DEFAULT_CPU_IMAGE" will be used
    # To use GPU in DSVM, you should specify the default GPU base Docker image or another GPU-enabled image:
    # myenv.docker.enabled = True
    # myenv.docker.base_image = azureml.core.runconfig.DEFAULT_GPU_IMAGE
    
    # Configure the run configuration with the Linux DSVM as the compute target and the environment defined above
    src = ScriptRunConfig(source_directory=".", script="train.py", compute_target=compute, environment=myenv) 
    

提示

如果您想要從工作區 [移除] (中斷連結) VM,請使用 RemoteCompute.detach() 方法。

Azure Machine Learning 不會為您刪除 VM。 您必須使用 Azure 入口網站、CLI 或適用於 Azure VM 的 SDK 手動刪除 VM。

Apache Spark 集區

Azure Synapse Analytics 與 Azure Machine Learning (預覽) 整合可讓您連結 Azure Synapse 所支援的 Apache Spark 集區,以進行互動式資料探索和準備。 透過此整合,您可以有用於大規模資料整頓的專用計算。 如需詳細資訊,請參如何連結由 Azure Synapse Analytics 提供技術支援的 Apache Spark 集區

Azure HDInsight

Azure HDInsight 是巨量資料分析的常用平台。 此平台會提供 Apache Spark,可用來將您的模型定型。

  1. 建立:Azure Machine Learning 無法為您建立 HDInsight 叢集。 相反地,您必須建立叢集,然後將它連結至您的 Azure Machine Learning 工作區。 如需詳細資訊,請參閱在 HDInsight 中建立 Spark 叢集

    警告

    Azure Machine Learning 要求 HDInsight 叢集具有公用 IP 位址

    建立叢集時,必須指定 SSH 使用者名稱與密碼。 請記下這些值,因為使用 HDInsight 作為計算目標時會需要它們。

    建立叢集之後,使用下列主機名稱連結至叢集:<clustername>-ssh.azurehdinsight.net,其中 <clustername> 是您提供的叢集名稱。

  2. 連結:若要連結 HDInsight 叢集作為計算目標,必須提供 HDInsight 叢集的資源識別碼、使用者名稱與密碼。 您可以使用訂用帳戶識別碼、資源群組名稱和 HDInsight 叢集名稱,以下列字串格式建構 HDInsight 叢集的資源識別碼:/subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.HDInsight/clusters/<cluster_name>

    from azureml.core.compute import ComputeTarget, HDInsightCompute
    from azureml.exceptions import ComputeTargetException
    
    try:
    # if you want to connect using SSH key instead of username/password you can provide parameters private_key_file and private_key_passphrase
    
    attach_config = HDInsightCompute.attach_configuration(resource_id='<resource_id>',
                                                          ssh_port=22, 
                                                          username='<ssh-username>', 
                                                          password='<ssh-pwd>')
    hdi_compute = ComputeTarget.attach(workspace=ws, 
                                       name='myhdi', 
                                       attach_configuration=attach_config)
    
    except ComputeTargetException as e:
    print("Caught = {}".format(e.message))
    
    hdi_compute.wait_for_completion(show_output=True)
    

    或者,您也可以使用 Azure Machine Learning Studio 將 HDInsight 叢集連結至您的工作區。

    警告

    請勿從您的工作區同時對相同 HDInsight 建立多個連結。 每個新連結都會中斷先前現有的連結。

  3. 設定:為 HDI 計算目標建立回合組態。

    from azureml.core.runconfig import RunConfiguration
    from azureml.core.conda_dependencies import CondaDependencies
    
    
    # use pyspark framework
    run_hdi = RunConfiguration(framework="pyspark")
    
    # Set compute target to the HDI cluster
    run_hdi.target = hdi_compute.name
    
    # specify CondaDependencies object to ask system installing numpy
    cd = CondaDependencies()
    cd.add_conda_package('numpy')
    run_hdi.environment.python.conda_dependencies = cd
    

提示

如果您想要從工作區 [移除] (中斷連結) HDInsight 叢集,請使用 HDInsightCompute.detach() 方法。

Azure Machine Learning 不會為您刪除 HDInsight 叢集。 您必須使用 Azure 入口網站、CLI 或適用於 Azure HDInsight 的 SDK 手動刪除它。

Azure Batch

Azure Batch 可用來在雲端有效率地執行大規模的平行和高效能運算 (HPC) 應用程式。 AzureBatchStep 可在 Azure Machine Learning 管線中用來將作業提交至 Azure Batch 電腦集區。

若要連結 Azure Batch 來作為計算目標,您必須使用 Azure Machine Learning SDK,並提供下列資訊:

  • Azure Batch 計算名稱:要用於工作區內計算的自訂名稱
  • Azure Batch 帳戶名稱:Azure Batch 帳戶的名稱
  • 資源群組:包含 Azure Batch 帳戶的資源群組。

下列程式碼示範如何連結 Azure Batch 來作為計算目標:

from azureml.core.compute import ComputeTarget, BatchCompute
from azureml.exceptions import ComputeTargetException

# Name to associate with new compute in workspace
batch_compute_name = 'mybatchcompute'

# Batch account details needed to attach as compute to workspace
batch_account_name = "<batch_account_name>"  # Name of the Batch account
# Name of the resource group which contains this account
batch_resource_group = "<batch_resource_group>"

try:
    # check if the compute is already attached
    batch_compute = BatchCompute(ws, batch_compute_name)
except ComputeTargetException:
    print('Attaching Batch compute...')
    provisioning_config = BatchCompute.attach_configuration(
        resource_group=batch_resource_group, account_name=batch_account_name)
    batch_compute = ComputeTarget.attach(
        ws, batch_compute_name, provisioning_config)
    batch_compute.wait_for_completion()
    print("Provisioning state:{}".format(batch_compute.provisioning_state))
    print("Provisioning errors:{}".format(batch_compute.provisioning_errors))

print("Using Batch compute:{}".format(batch_compute.cluster_resource_id))

警告

請勿從您的工作區同時對相同 Azure Batch 建立多個連結。 每個新連結都會中斷先前現有的連結。

Azure Databricks

Azure Databricks 是 Azure 雲端中的 Apache Spark 型環境。 它可與 Azure Machine Learning 管線搭配使用作為計算目標。

重要

Azure Machine Learning 無法建立 Azure Databricks 計算目標。 相反地,您必須建立 Azure Databricks 工作區,然後將其連結至您的 Azure Machine Learning 工作區。 若要建立工作區資源,請參閱在 Azure Databricks 上執行 Spark 作業文件。

若要從不同的 Azure 訂用帳戶連結 Azure Databricks 工作區,您 (Microsoft Entra 帳戶) 必須獲授與 Azure Databricks 工作區上的參與者角色。 在 Azure 入口網站中檢查您的存取權。

若要連結 Azure Databricks 作為計算目標,請提供下列資訊:

  • Databricks 計算名稱:您想要指派給此計算資源的名稱。
  • Databricks 工作區名稱:Azure Databricks 工作區的名稱。
  • Databricks 存取權杖:用來向 Azure Databricks 進行驗證的存取權杖。 若要產生存取權杖,請參閱驗證文件。

下列程式碼示範如何搭配 Azure Machine Learning SDK 來連結 Azure Databricks 作為計算目標:

import os
from azureml.core.compute import ComputeTarget, DatabricksCompute
from azureml.exceptions import ComputeTargetException

databricks_compute_name = os.environ.get(
    "AML_DATABRICKS_COMPUTE_NAME", "<databricks_compute_name>")
databricks_workspace_name = os.environ.get(
    "AML_DATABRICKS_WORKSPACE", "<databricks_workspace_name>")
databricks_resource_group = os.environ.get(
    "AML_DATABRICKS_RESOURCE_GROUP", "<databricks_resource_group>")
databricks_access_token = os.environ.get(
    "AML_DATABRICKS_ACCESS_TOKEN", "<databricks_access_token>")

try:
    databricks_compute = ComputeTarget(
        workspace=ws, name=databricks_compute_name)
    print('Compute target already exists')
except ComputeTargetException:
    print('compute not found')
    print('databricks_compute_name {}'.format(databricks_compute_name))
    print('databricks_workspace_name {}'.format(databricks_workspace_name))
    print('databricks_access_token {}'.format(databricks_access_token))

    # Create attach config
    attach_config = DatabricksCompute.attach_configuration(resource_group=databricks_resource_group,
                                                           workspace_name=databricks_workspace_name,
                                                           access_token=databricks_access_token)
    databricks_compute = ComputeTarget.attach(
        ws,
        databricks_compute_name,
        attach_config
    )

    databricks_compute.wait_for_completion(True)

如需更詳細的範例,請參閱 GitHub 上的範例筆記本

警告

請勿從您的工作區同時對相同 Azure Databricks 建立多個連結。 每個新連結都會中斷先前現有的連結。

Azure Data Lake Analytics

Azure Data Lake Analytics 是 Azure 雲端中的巨量資料分析平台。 它可與 Azure Machine Learning 管線搭配使用作為計算目標。

在使用 Azure Data Lake Analytics 之前,請先建立其帳戶。 若要建立此資源,請參閱開始使用 Azure Data Lake Analytics 文件。

若要連結 Data Lake Analytics 來作為計算目標,您必須使用 Azure Machine Learning SDK,並提供下列資訊:

  • 計算名稱:您想要指派給這個計算資源的名稱。
  • 資源群組:包含 Data Lake Analytics 帳戶的資源群組。
  • 帳戶名稱:Data Lake Analytics 帳戶名稱。

下列程式碼示範如何連結 Data Lake Analytics 來作為計算目標:

import os
from azureml.core.compute import ComputeTarget, AdlaCompute
from azureml.exceptions import ComputeTargetException


adla_compute_name = os.environ.get(
    "AML_ADLA_COMPUTE_NAME", "<adla_compute_name>")
adla_resource_group = os.environ.get(
    "AML_ADLA_RESOURCE_GROUP", "<adla_resource_group>")
adla_account_name = os.environ.get(
    "AML_ADLA_ACCOUNT_NAME", "<adla_account_name>")

try:
    adla_compute = ComputeTarget(workspace=ws, name=adla_compute_name)
    print('Compute target already exists')
except ComputeTargetException:
    print('compute not found')
    print('adla_compute_name {}'.format(adla_compute_name))
    print('adla_resource_id {}'.format(adla_resource_group))
    print('adla_account_name {}'.format(adla_account_name))
    # create attach config
    attach_config = AdlaCompute.attach_configuration(resource_group=adla_resource_group,
                                                     account_name=adla_account_name)
    # Attach ADLA
    adla_compute = ComputeTarget.attach(
        ws,
        adla_compute_name,
        attach_config
    )

    adla_compute.wait_for_completion(True)

如需更詳細的範例,請參閱 GitHub 上的範例筆記本

警告

請勿從您的工作區同時對相同 ADLA 建立多個連結。 每個新連結都會中斷先前現有的連結。

提示

Azure Machine Learning 管線只能使用 Data Lake Analytics 帳戶的預設資料存放區中所儲存的資料來運作。 如果您運作所需的資料位於非預設的存放區,則可以先使用 DataTransferStep 複製資料再進行訓練。

Azure Container Instance

部署模型時,會動態建立 Azure 容器執行個體 (ACI)。 您無法以任何其他方式建立或連結 ACI 至您的工作區。 如需詳細資訊,請參閱將模型部署到 Azure 容器執行個體

Kubernetes

Azure Machine Learning 提供選項,以連結您自己的 Kubernetes 叢集以進行定型和推斷。 請參閱設定適用於 Azure Machine Learning 的 Kubernetes 叢集

若要從您的工作區中斷連結 Kubernetes 叢集,請使用下列方法:

compute_target.detach()

警告

中斷連結叢集並不會刪除該叢集。 若要刪除 Azure Kubernetes Service 叢集,請參閱使用 Azure CLI 搭配 AKS。 若要刪除已啟用 Azure Arc 的 Kubernetes 叢集,請參閱 Azure Arc 快速入門

筆記本範例

如需具有各種計算目標的定型範例,請參閱下列筆記:

了解如何依照使用 Jupyter 筆記本來探索這項服務一文來執行筆記本。

下一步