使用 Python SDK (v1) 管理 Azure Machine Learning 工作區

適用於:Python SDK azureml v1

在本文中,您將使用 SDK for Python,為 Azure Machine Learning 建立、檢視和刪除 Azure Machine Learning 工作區

當您的需求變更或自動化的需求增加時,您也可以使用 CLI透過 VS Code 延伸模組來管理工作區。

必要條件

限制

  • 建立新的工作區時,您可以自動建立工作區所需的服務,或使用現有的服務。 若要使用有別於工作區的不同 Azure 訂閱的現有服務,您必須在包含這些服務的訂閱中註冊 Azure Machine Learning 命名空間。 例如,若要在訂閱 A (使用訂閱 B 的儲存體帳戶) 中建立工作區,則必須先在訂閱 B 中註冊 Azure Machine Learning 命名空間,然後才能在工作區使用儲存體帳戶。

    Azure Machine Learning 的資源提供者為 Microsoft.MachineLearningServices。 如需如何檢查是否已註冊及如何註冊的相關資訊,請參閱《Azure 資源提供者和類型》一文。

    重要

    這僅適用於建立工作區期間所提供的資源;Azure 儲存體帳戶、Azure Container Register、Azure Key Vault 和 Application Insights。

  • 根據預設,建立工作區時也會建立 Azure Container Registry (ACR)。 由於 ACR 目前不支援資源群組名稱中的 Unicode 字元,請使用不包含這些字元的資源群組。

  • Azure Machine Learning 不支援工作區預設儲存體帳戶使用階層命名空間 (Azure Data Lake Storage Gen2 功能)。

提示

當您建立工作區時,會建立 Azure Application Insights 執行個體。 建立叢集後,您可以視需要刪除 Application Insights 執行個體。 刪除叢集會限制從工作區收集的資訊,且可能會使問題的疑難排解變得較為困難。 若您刪除工作區所建立的 Application Insights 執行個體,即無法在不刪除和重新建立工作區的情況下重新建立該執行個體

如需關於使用 Application Insights 執行個體的詳細資訊,請參閱監視和收集 Machine Learning Web 服務端點的資料

建立工作區

您可以直接在 Azure Machine Learning 工作室中建立工作區,來取得有限的可用選項。 或者,使用下列其中一種方法來取得更多選項控制。

  • 預設規格。 依預設會自動建立相依資源和資源群組。 此程式碼會在 eastus2 中建立名為 myworkspace 的工作區,以及名為 myresourcegroup 的資源群組。

    適用於:Python SDK azureml v1

    from azureml.core import Workspace
    
    ws = Workspace.create(name='myworkspace',
                   subscription_id='<azure-subscription-id>',
                   resource_group='myresourcegroup',
                   create_resource_group=True,
                   location='eastus2'
                   )
    

    若您已有要用於工作區的現有 Azure 資源群組,請將 create_resource_group 設定為 False。

  • 多租用戶。 如果您有多個帳戶,請新增您想要使用的 Microsoft Entra ID 租用戶識別碼。 在 Azure 入口網站的 [Microsoft Entra ID,外部身分識別] 下,尋找您的租用戶識別碼。

    適用於:Python SDK azureml v1

    from azureml.core.authentication import InteractiveLoginAuthentication
    from azureml.core import Workspace
    
    interactive_auth = InteractiveLoginAuthentication(tenant_id="my-tenant-id")
    ws = Workspace.create(name='myworkspace',
                subscription_id='<azure-subscription-id>',
                resource_group='myresourcegroup',
                create_resource_group=True,
                location='eastus2',
                auth=interactive_auth
                )
    
  • 主權雲端。 若您是在主權雲端中工作,則需要額外程式碼來向 Azure 進行驗證。

    適用於:Python SDK azureml v1

    from azureml.core.authentication import InteractiveLoginAuthentication
    from azureml.core import Workspace
    
    interactive_auth = InteractiveLoginAuthentication(cloud="<cloud name>") # for example, cloud="AzureUSGovernment"
    ws = Workspace.create(name='myworkspace',
                subscription_id='<azure-subscription-id>',
                resource_group='myresourcegroup',
                create_resource_group=True,
                location='eastus2',
                auth=interactive_auth
                )
    
  • 使用現有的 Azure 資源。 您也可以使用 Azure 資源識別碼格式,來建立使用現有 Azure 資源的工作區。 在 Azure 入口網站中或使用 SDK,尋找特定的 Azure 資源識別碼。 此範例假設資源群組、儲存體帳戶、金鑰保存庫、App Insights 以及容器登錄都已存在。

    適用於:Python SDK azureml v1

    import os
    from azureml.core import Workspace
    from azureml.core.authentication import ServicePrincipalAuthentication
    
    service_principal_password = os.environ.get("AZUREML_PASSWORD")
    
    service_principal_auth = ServicePrincipalAuthentication(
        tenant_id="<tenant-id>",
        username="<application-id>",
        password=service_principal_password)
    
                          auth=service_principal_auth,
                               subscription_id='<azure-subscription-id>',
                               resource_group='myresourcegroup',
                               create_resource_group=False,
                               location='eastus2',
                               friendly_name='My workspace',
                               storage_account='subscriptions/<azure-subscription-id>/resourcegroups/myresourcegroup/providers/microsoft.storage/storageaccounts/mystorageaccount',
                               key_vault='subscriptions/<azure-subscription-id>/resourcegroups/myresourcegroup/providers/microsoft.keyvault/vaults/mykeyvault',
                               app_insights='subscriptions/<azure-subscription-id>/resourcegroups/myresourcegroup/providers/microsoft.insights/components/myappinsights',
                               container_registry='subscriptions/<azure-subscription-id>/resourcegroups/myresourcegroup/providers/microsoft.containerregistry/registries/mycontainerregistry',
                               exist_ok=False)
    

如需詳細資訊,請參閱工作區 SDK 參考

若您在存取訂閱時遇到問題,請參閱設定 Azure Machine Learning 資源與工作流程的驗證,以及筆記本 Azure Machine Learning 中的驗證

網路

重要

如需將私人端點和虛擬網路與工作區搭配使用的詳細資訊,請參閱網路隔離和隱私權

Azure Machine Learning Python SDK 提供可搭配 Workspace.create() 使用的 PrivateEndpointConfig 類別,以建立具有私人端點的工作區。 此類別需要現有的虛擬網路。

進階

根據預設,工作區的中繼資料會儲存在 Microsoft 所維護的 Azure Cosmos DB 執行個體中。 此資料會使用 Microsoft 受控金鑰進行加密。

如要限制 Microsoft 在工作區上收集的資料,請在入口網站中選取 [高業務影響工作區],或在 Python 中設定 hbi_workspace=true 。 如需此設定的詳細資訊,請參閱靜態加密

重要

只有在建立工作區時,才能選取 [高業務影響]。 建立工作區之後,就無法變更此設定。

使用您自己的資料加密金鑰

您可以提供自己的金鑰進行資料加密。 這麼做會建立 Azure Cosmos DB 執行個體,以將中繼資料儲存在您的 Azure 訂閱中。 如需詳細資訊,請參閱適用於 Azure Machine Learning 的客戶自控金鑰

使用下列步驟來提供您自己的金鑰:

重要

在遵循這些步驟之前,您必須先執行下列動作:

遵循設定客戶自控金鑰中的步驟,執行以下動作:

  • 註冊 Azure Cosmos DB 提供者
  • 建立及設定 Azure Key Vault
  • 產生金鑰

使用 cmk_keyvaultresource_cmk_uri 來指定客戶自控金鑰。

from azureml.core import Workspace
   ws = Workspace.create(name='myworkspace',
               subscription_id='<azure-subscription-id>',
               resource_group='myresourcegroup',
               create_resource_group=True,
               location='eastus2'
               cmk_keyvault='subscriptions/<azure-subscription-id>/resourcegroups/myresourcegroup/providers/microsoft.keyvault/vaults/<keyvault-name>', 
               resource_cmk_uri='<key-identifier>'
               )

下載設定檔

如果您要在工作區中使用計算執行個體 來執行程式碼,請略過此步驟。 計算執行個體會為您建立及儲存此檔案的複本。

若您打算在本機環境上使用參考此工作區 (ws) 的程式碼,請撰寫設定檔:

適用於:Python SDK azureml v1

ws.write_config()

使用 Python 指令碼或 Jupyter Notebook,將檔案置於目錄結構中。 可以位於相同的目錄,名為 aml_config 的子目錄,或位於父目錄。 當您建立計算執行個體時,系統會為您將此檔案新增至 VM 上的正確目錄。

連線到工作區

在您的 Python 程式碼中,您會建立工作區物件以連線至工作區。 此程式碼會讀取組態檔的內容,以尋找您的工作區。 若您尚未經過驗證,將會收到登入的提示。

適用於:Python SDK azureml v1

from azureml.core import Workspace

ws = Workspace.from_config()
  • 多租用戶。 如果您有多個帳戶,請新增您想要使用的 Microsoft Entra ID 租用戶識別碼。 在 Azure 入口網站的 [Microsoft Entra ID,外部身分識別] 下,尋找您的租用戶識別碼。

    適用於:Python SDK azureml v1

    from azureml.core.authentication import InteractiveLoginAuthentication
    from azureml.core import Workspace
    
    interactive_auth = InteractiveLoginAuthentication(tenant_id="my-tenant-id")
    ws = Workspace.from_config(auth=interactive_auth)
    
  • 主權雲端。 若您是在主權雲端中工作,則需要額外程式碼來向 Azure 進行驗證。

    適用於:Python SDK azureml v1

    from azureml.core.authentication import InteractiveLoginAuthentication
    from azureml.core import Workspace
    
    interactive_auth = InteractiveLoginAuthentication(cloud="<cloud name>") # for example, cloud="AzureUSGovernment"
    ws = Workspace.from_config(auth=interactive_auth)
    

若您在存取訂閱時遇到問題,請參閱設定 Azure Machine Learning 資源與工作流程的驗證,以及筆記本 Azure Machine Learning 中的驗證

尋找工作區

查看您可以使用的所有工作區清單。

在 Azure 入口網站的訂閱頁面上尋找您的訂閱。 複製識別碼,並在以下程式碼中使用它來查看該訂閱可使用的所有工作區。

適用於:Python SDK azureml v1

from azureml.core import Workspace

Workspace.list('<subscription-id>')

Workspace.list(..) 方法不會傳回完整的工作區物件。 其僅包含訂閱中現有工作區的基本資訊。 如要取得特定工作區的完整物件,請使用 Workspace.get(..)。

刪除工作區

當您不再需要工作區時,請將其刪除。

警告

如果工作區已啟用虛刪除,可在刪除後加以復原。 如果未啟用虛刪除,或您選取永久刪除工作區的選項,則無法加以復原。 如需詳細資訊,請參閱復原已刪除的工作區

提示

Azure Machine Learning 的預設行為是虛刪除工作區。 這表示工作區不會立即刪除,而是標示為要刪除。 如需詳細資訊,請參閱虛刪除

刪除工作區 ws

適用於:Python SDK azureml v1

ws.delete(delete_dependent_resources=False, no_wait=False)

預設動作不會刪除與工作區相關聯的資源,也就是容器登錄、儲存體帳戶、金鑰保存庫和 Application Insights。 將 delete_dependent_resources 設為 True 以一併刪除這些資源。

清除資源

重要

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

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

  1. 在 Azure 入口網站中,選取最左邊的 [資源群組]

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

  3. 選取 [刪除資源群組]

    Screenshot of the selections to delete a resource group in the Azure portal.

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

疑難排解

  • Azure Machine Learning 工作室中支援的瀏覽器:建議您使用與作業系統相容的最新瀏覽器。 支援下列瀏覽器:

    • Microsoft Edge (新的 Microsoft Edge,最新版本,並非舊版 Microsoft Edge)
    • Safari (最新版本,僅限 Mac)
    • Chrome (最新版本)
    • Firefox (最新版本)
  • Azure 入口網站

工作區診斷

您可以從 Azure Machine Learning 工作室或 Python SDK 診斷工作區。 診斷執行完成後,就會傳回所有偵測到的問題清單。 這份清單包含可能的解決方案連結。 如需詳細資訊,請參閱如何使用工作區診斷

資源提供者錯誤

建立 Azure Machine Learning 工作區或工作區所使用的資源時,您可能會收到類似下列訊息的錯誤:

  • No registered resource provider found for location {location}
  • The subscription is not registered to use namespace {resource-provider-namespace}

大部分資源提供者都會自動註冊,但並非全部。 如果您收到此訊息,則需要註冊先前提及的提供者。

下表包含 Azure Machine Learning 所需的資源提供者清單:

資源提供者 需要的原因
Microsoft.MachineLearningServices 建立 Azure Machine Learning 工作區。
Microsoft.Storage Azure 儲存體帳戶用來做為工作區的預設儲存體。
Microsoft.ContainerRegistry 工作區會使用 Azure Container Registry 來建置 Docker 映像。
Microsoft.KeyVault 工作區會使用 Azure Key Vault 來儲存秘密。
Microsoft.Notebooks Azure Machine Learning 計算執行個體上的整合式筆記本。
Microsoft.ContainerService 若您打算將已定型的模型部署至 Azure Kubernetes Service。

若您打算搭配 Azure Machine Learning 使用客戶自控金鑰,則必須註冊下列服務提供者:

資源提供者 需要的原因
Microsoft.DocumentDB 記錄工作區中繼資料的 Azure CosmosDB 執行個體。
Microsoft.Search Azure 搜尋可為工作區提供索引編製功能。

如果您打算搭配 Azure Machine Learning 使用受控虛擬網路,則必須註冊 Microsoft.Network 資源提供者。 建立受控虛擬網路的私人端點時,工作區會使用此資源提供者。

如需有關註冊資源提供者的詳細資訊,請參閱解決資源提供者註冊的錯誤

刪除 Azure Container Registry

Azure Machine Learning 工作區會使用 Azure Container Registry (ACR) 進行某些作業。 它會在第一次需要 ACR 執行個體時自動建立一個。

警告

為工作區建立 Azure Container Registry 之後,請勿將其刪除。 這樣做將會造成您的 Azure Machine Learning 工作區中斷。

下一步

擁有工作區之後,請瞭解如何定型和部署模型

如要深入瞭解如何針對組織的需求規劃工作區,請參閱組織和設定 Azure Machine Learning

若要了解如何使用最新的安全性更新讓 Azure Machine Learning 保持最新狀態的相關資訊,請參閱弱點管理