本部分介绍如何在 Vertex AI Workbench 实例中创建 Jupyter 笔记本。Vertex AI Workbench 实例是基于 Jupyter 笔记本的开发环境,适用于整个数据科学工作流。Vertex AI Workbench 实例预封装了 JupyterLab,并预安装了深度学习软件包套件,并且都支持 TensorFlow 和 PyTorch 框架。如需了解详情,请参阅 Vertex AI Workbench 实例简介。
在 Vertex AI Workbench 中创建笔记本后,运行 Python 代码的连续部分,以执行大部分工作来生成预测结果。
准备工作
-
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 Gemini Enterprise Agent Platform and Notebooks APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
创建 Vertex AI Workbench 实例
如需创建 Vertex AI Workbench 实例,请执行以下操作:
在 Google Cloud 控制台中,打开您的 Google Cloud 项目(如果尚未打开)。
在 Google Cloud 控制台中,前往 Vertex AI Workbench 实例页面。
如果系统显示用于启用 Notebooks API 的选项,请点击启用。启用过程可能需要一些时间才能完成。
点击新建。
在新建实例对话框的名称中,为实例输入名称。
在区域处,选择 us-central1 (爱荷华)。
在区域处,选择 us-central1-a。
点击创建。如需详细了解您的实例,请在该实例出现在实例列表中时点击其名称,以查看其属性。
准备笔记本
您的 Vertex AI Workbench 实例已通过身份验证,可以使用您的 项目。 Google Cloud 但您必须安装并初始化 Agent Platform SDK for Python。此部分会引导您完成这些步骤。
创建笔记本后,您可以使用它输入并运行本教程中的连续代码段。每个代码段必须按顺序单独运行。
创建并打开笔记本
在本教程中,代码在笔记本中运行。这是一个扩展程序为 .ipynb 的文件。当您创建笔记本时,它是未命名的。您可以在将其打开后进行重命名。如需创建并打开笔记本,请执行以下操作:
在 Google Cloud 控制台中,前往实例页面。
在实例名称旁边,点击打开 JupyterLab 。
您的 Agent Platform Workbench 实例会打开 JupyterLab 环境。
在 JupyterLab 中,选择 文件 > 新建 > 笔记本。
系统会打开您的新笔记本文件,并显示选择内核对话框。
在选择内核对话框中,选择 Python 3 内核。
在 JupyterLab 的左侧导航窗格中,找到名为 Untitled.ipynbUntitled.ipynb 的新笔记本。如需重命名,请右键点击 笔记本的名称,点击 重命名,然后输入新名称。
安装 Agent Platform SDK for Python
打开笔记本后,您必须安装 Agent Platform SDK for Python。 您可以使用 Agent Platform SDK for Python 进行 Agent Platform API 调用,以创建数据集、创建模型、训练和部署模型及使用模型进行预测。如需了解详情,请参阅使用 Agent Platform SDK for Python。
安装 Agent Platform SDK for Python 时,系统还会安装其所依赖的其他 Google Cloud SDK。本教程中使用了其中的两种 SDK:
Cloud Storage - 使用 Agent Platform SDK for Python 进行 Agent Platform API 调用时,Gemini Enterprise Agent Platform 将制品存储在 Cloud Storage 存储桶中。该存储桶称为暂存存储桶。 您可以在 初始化Agent Platform SDK for Python 时指定暂存存储桶。如需了解详情, 请参阅适用于 Storage Google Cloud API 的 Python 客户端。
BigQuery - Gemini Enterprise Agent Platform 使用 BigQuery 公共数据集训练您的模型。必须安装 BigQuery SDK 才能访问和下载本教程中使用的数据集。如需了解详情,请参阅 BigQuery API 客户端 库。
如需安装 Agent Platform SDK for Python 及其从属 SDK,请运行以下代码。
# Install the Agent Platform SDK
! pip3 install --upgrade --quiet google-cloud-aiplatform
--quiet 标志会抑制输出,以便仅显示错误(如果有)。感叹号 (!) 表示,这是 shell 命令。
由于这是您在新笔记本中运行的第一段代码,因此请将其输入到笔记本顶部的空白代码单元中。在代码单元中输入代码后,点击 运行所选单元并前进或使用键盘快捷键 Shift + Enter 运行代码。
在继续学习本教程时,请在自动显示在最近运行的代码下方的空代码单元中运行代码。如果您想手动添加新的代码单元,请点击笔记本文件的 在下方插入单元按钮。
设置项目 ID 和区域
在此步骤中,您将项目 ID 和区域分配给变量,以便在本教程后面的部分中轻松引用它们。
设置项目 ID
如需设置项目 ID,请执行以下操作:
找到您的 Google Cloud 项目 ID。如需了解详情,请参阅查找项目 ID。
在笔记本的代码单元中运行以下命令。在代码中,将 PROJECT_ID 替换为您刚刚找到的项目 ID。此命令生成的输出为
Updated property [core/project].project_id = "PROJECT_ID" # @param {type:"string"}
设置区域
本教程使用 us-central1 区域。如需设置您的区域,请执行以下操作:
运行以下代码,将 Gemini Enterprise Agent Platform 使用的
region变量设置为us-central1。此命令不会生成输出。如需了解详情,请参阅选择适合您的版本。region = "us-central1" # @param {type: "string"}
创建 Cloud Storage 存储桶
本教程需要 Gemini Enterprise Agent Platform 用于暂存工件的 Cloud Storage 存储桶。Gemini Enterprise Agent Platform 将与您创建的数据集和模型资源关联的数据存储在暂存存储桶中。此数据会保留下来,并且在各个会话中可用。在本教程中,Gemini Enterprise Agent Platform 还会将您的数据集存储在暂存存储桶中。您可以在初始化 Agent Platform SDK for Python 时指定暂存存储桶。
每个 Cloud Storage 存储桶的名称都必须是全局唯一的。如果您选择已使用的名称,则用于创建存储桶的命令将失败。以下代码使用日期时间戳和您的项目名称来创建唯一的存储桶名称。您可以将存储桶名称附加到
gs://,以创建 Cloud Storage 存储桶的 URI。echo shell 命令会显示 URI,以便您验证其是否已正确创建。
如需设置存储桶的名称和 URI,请运行以下代码。最后一行显示 Cloud Storage 存储桶的 URI。
bucket_name = "bucket-name-placeholder" # @param {type:"string"} bucket_uri = f"gs://{bucket_name}" from datetime import datetime timestamp = datetime.now().strftime("%Y%m%d%H%M%S") if bucket_name == "" or bucket_name is None or bucket_name == "bucket-name-placeholder": bucket_name = project_id + "aip-" + timestamp bucket_uri = "gs://" + bucket_name ! echo $bucket_uri如需使用 Cloud Storage 客户端库和存储桶 URI 创建存储桶,请运行以下代码。此代码不会生成输出。
from google.cloud import storage client = storage.Client(project=project_id) # Create a bucket bucket = client.create_bucket(bucket_name, location=region)如需验证您的存储桶是否已成功创建,请运行以下命令:
print("Bucket {} created.".format(bucket.name))
初始化 Agent Platform SDK for Python
如需初始化 Agent Platform SDK for Python,请先导入其库 aiplatform。接下来,调用
aiplatform.init 并传入以下参数的值:
project-project指定使用 Agent Platform SDK for Python 调用 Agent Platform API 时要使用的 Google Cloud 项目。在本教程中,您将使用项目的名称来指定该 Google Cloud 项目。您也可以使用项目编号来指定项目。location-location指定进行 API 调用时使用的 Google Cloud 区域。如果您未指定位置,Agent Platform SDK for Python 将使用us-central1。staging_bucket-staging_bucket指定使用 Agent Platform SDK for Python 暂存制品时使用的 Cloud Storage 存储桶。您可以使用以gs://开头的 URI 指定存储桶。在本教程中,您将使用之前在创建 Cloud Storage 存储桶中创建的 URI。
如需设置 Google Cloud 项目、区域和暂存存储桶,请运行以下命令。此命令不会生成输出。
from google.cloud import aiplatform
# Initialize the Agent Platform SDK
aiplatform.init(project=project_id, location=region, staging_bucket=bucket_uri)
初始化 BigQuery
本教程使用 BigQuery 的企鹅公共数据集来训练模型。在 Gemini Enterprise Agent Platform 训练模型后,您可以指定代表企鹅特征的参数,而模型使用这些特征来预测它们代表的企鹅种类。如需详细了解 公共数据集,请参阅 BigQuery 公共数据集。
在使用 BigQuery 数据集之前,您必须使用项目 ID 初始化 BigQuery。如需执行此操作,请运行以下命令:此命令不会生成输出。
from google.cloud import bigquery
# Set up BigQuery client
bq_client = bigquery.Client(project=project_id)