使用參數執行筆記本
您可以在 Vertex AI Workbench 代管筆記本執行個體中,使用筆記本執行作業中的參數值,指定筆記本檔案程式碼的執行方式差異。本頁說明如何設定筆記本檔案以使用參數,以及如何執行作業,為筆記本參數指定不同的值。
使用參數執行不同疊代的筆記本檔案
您可以在執行作業時使用筆記本參數值,執行相同的筆記本程式碼,同時指定下列差異:
指定要使用的其他資料集,或資料集的不同樣本大小。
指定不同的模型設定,例如學習率或最佳化工具類型。
執行不同模型,或執行同一模型的多個版本。
如何在筆記本執行作業中使用參數
使用參數執行筆記本的程序主要有兩個步驟:
在筆記本檔案的其中一個儲存格中新增
parameters標記。 雖然這不是技術上的必要條件,但這個儲存格通常會包含將值指派給參數變數的程式碼,不過這並非技術上的必要條件。如果您未在執行作業中指派不同的參數值,執行作業會使用筆記本檔案中的參數值做為預設值。為筆記本檔案建立執行作業,其中包含參數的新值。請使用下列模式設定參數和參數值的格式:
parameter1=value1,parameter2=value2。格式規定參數值組之間須以半形逗號分隔,不得有空格,也不得使用半形引號。 執行作業時,執行器會在筆記本中新增儲存格,直接更新標記為parameters的儲存格後方參數的值。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Notebooks and Vertex AI APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Notebooks and Vertex AI APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Notebooks 檢視者 (
roles/notebooks.viewer) -
Vertex AI 使用者 (
roles/aiplatform.user) -
儲存空間管理員 (
roles/storage.admin) 在代管型筆記本執行個體的 JupyterLab 使用者介面中,開啟要執行的筆記本檔案。
在一個儲存格中編寫程式碼,為參數變數指派值。如果您在執行作業時未指派不同的參數值,筆記本檔案就會使用這些值。
確認參數儲存格仍處於選取狀態,然後在右側邊欄中,按一下「屬性檢查器」。
在屬性檢查器中,按一下「儲存格標記」部分的「新增標記」,輸入
parameters,然後按Enter。在代管型筆記本執行個體的 JupyterLab 使用者介面中,按一下「執行者」按鈕。
在「Submit notebooks to Executor」(將筆記本提交至執行器) 對話方塊中,於「Execution name」(執行名稱) 欄位輸入執行名稱。
選取「Machine type」(機器類型) 和「Accelerator type」(加速器類型)。
選取「環境」。
在「Type」欄位中,選取「One-time execution」,或選取「Schedule-based recurring executions」,然後完成對話方塊,排定執行時間。
在「Advanced options」(進階選項) 中,選取要執行筆記本的「Region」(區域)。
在「Cloud Storage bucket」(Cloud Storage bucket) 欄位中,選取可用的 Cloud Storage bucket,或輸入新 bucket 的名稱,然後按一下「Create and select」(建立並選取)。執行器會將筆記本輸出內容儲存在這個 Cloud Storage 值區中。
在「Notebook parameterization」(筆記本參數化) 區段和「Input parameters」(輸入參數) 文字方塊中,新增以半形逗號分隔的筆記本參數,例如
optimizer=SGD,learning_rate=0.01。格式不得包含空格和引號。設定其餘執行作業,然後按一下「提交」。
- 進一步瞭解如何在執行器中執行筆記本程式碼。
必要的角色
為確保執行個體的服務帳戶具備與 Vertex AI Workbench 執行器互動的必要權限,請要求管理員將專案的下列 IAM 角色授予執行個體的服務帳戶:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
管理員或許也能透過自訂角色或其他預先定義的角色,將必要權限授予執行個體的服務帳戶。
開啟 JupyterLab
如要開啟 JupyterLab 並準備要執行的筆記本檔案,請完成下列步驟。