使用 sagemaker.interactive_apps.tensorboard 模块打开 TensorBoard - 亚马逊 SageMaker AI

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 sagemaker.interactive_apps.tensorboard 模块打开 TensorBoard

sagemaker.interactive_apps.tensorboard 模块提供了一个名为 get_app_url 的函数,该函数生成未签名或预签名的 URL,以便在 SageMaker AI 或 Amazon EC2 的任意环境中打开 TensorBoard 应用程序。这是为了向 Studio Classic 用户和非 Studio Classic 用户提供统一的体验。对于 Studio 环境,您可以通过按原样运行 get_app_url() 函数来打开 TensorBoard,也可以指定作业名称以便在 TensorBoard 应用程序打开时开始跟踪。对于非 Studio Classic 环境,您可以通过向实用程序函数提供您的域和用户配置文件信息来打开 TensorBoard。使用此功能,无论您在哪里或以何种方式运行训练代码和启动训练作业,都可以通过在 Jupyter 笔记本或终端中运行 get_app_url 函数来直接访问 TensorBoard。

注意

此功能在 SageMaker Python SDK v2.184.0 及更高版本中可用。要使用此功能,请确保通过运行 pip install sagemaker --upgrade 来升级 SDK。

选项 1:对于 SageMaker AI Studio Classic

如果您使用的是 SageMaker Studio Classic,则可以直接打开 TensorBoard 应用程序,也可以通过运行 get_app_url 函数来检索未签名的 URL,如下所示。由于您已在 Studio Classic 环境中并以域用户身份登录,因此,get_app_url() 会生成未签名的 URL,因为无需再次进行身份验证。

打开 TensorBoard 应用程序

以下代码自动从 get_app_url() 函数在环境的默认 Web 浏览器中返回的未签名的 URL 打开 TensorBoard 应用程序。

from sagemaker.interactive_apps import tensorboard region = "us-west-2" app = tensorboard.TensorBoardApp(region) app.get_app_url( training_job_name="your-training_job_name" # Optional. Specify the job name to track a specific training job )

检索未签名的 URL 并手动打开 TensorBoard 应用程序

以下代码将打印一个未签名的 URL,您可以将其复制到 Web 浏览器并打开 TensorBoard 应用程序。

from sagemaker.interactive_apps import tensorboard region = "us-west-2" app = tensorboard.TensorBoardApp(region) print("Navigate to the following URL:") print( app.get_app_url( training_job_name="your-training_job_name", # Optional. Specify the name of the job to track. open_in_default_web_browser=False # Set to False to print the URL to terminal. ) )

请注意,如果您在 SageMaker AI Studio Classic 环境之外运行前两个代码示例,则该函数将返回指向 SageMaker AI 控制台中 TensorBoard 登录页面的 URL,因为没有您的域和用户配置文件的登录信息。要创建预签名的 URL,请参阅以下部分中的选项 2。

选项 2:对于非 Studio Classic 环境

如果您使用非 Studio Classic 环境(例如 SageMaker 笔记本实例或 Amazon EC2),并且要直接从您所在的环境中打开 TensorBoard,则需要使用您的域和用户配置文件信息生成一个预先指定的网址。预先指定的 URL 是用于登录到 Amazon SageMaker Studio Classic 的网址,它是使用您的域和用户配置文件创建的,因此已获得对与您的域关联的所有域应用程序和文件的访问权限。要通过预先指定的 URL 打开 TensorBoard,请将 get_app_url 函数与您的域和用户配置文件名称结合使用,如下所示。

请注意,此选项要求域用户拥有 sagemaker:CreatePresignedDomainUrl 权限。如果不具有此权限,域用户将收到异常错误。

重要

请勿共享任何预签名的 URL。get_app_url 函数会创建预先指定的网址,这些网址会自动使用您的域和用户配置文件进行身份验证,并允许访问与您的域关联的任何应用程序和文件。

print( app.get_app_url( training_job_name="your-training_job_name", # Optional. Specify the name of the job to track. create_presigned_domain_url=True, # Reguired to be set to True for creating a presigned URL. domain_id="your-domain-id", # Required if creating a presigned URL (create_presigned_domain_url=True). user_profile_name="your-user-profile-name", # Required if creating a presigned URL (create_presigned_domain_url=True). open_in_default_web_browser=False, # Optional. Set to False to print the URL to terminal. optional_create_presigned_url_kwargs={} # Optional. Add any additional args for Boto3 create_presigned_domain_url ) )
提示

get_app_url 函数在后端的 适用于 Python (Boto3) 的 AWS SDK 中运行 SageMaker.Client.create_presigned_domain_url API。由于 Boto3 create_presigned_domain_url API 创建的预签名的 URL 默认在 300 秒后过期,因此,预签名的 TensorBoard 应用程序 URL 也将在 300 秒后过期。如果要延长过期时间,请将 ExpiresInSeconds 参数传递给 get_app_url 函数的 optional_create_presigned_url_kwargs 参数,如下所示。

optional_create_presigned_url_kwargs={"ExpiresInSeconds": 1500}
注意

如果您传递给 get_app_url 的参数的任何输入无效,则该函数将输出指向 TensorBoard 登录页面的 URL,而不是打开 TensorBoard 应用程序。输出消息将与以下内容类似。

Navigate to the following URL: https://us-west-2.console.aws.amazon.com/sagemaker/home?region=us-west-2#/tensor-board-landing