

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

# TensorBoard 使用`sagemaker.interactive_apps.tensorboard`模块打开
<a name="debugger-htb-access-tb-url"></a>

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

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

**Topics**
+ [选项 1：适用于 SageMaker AI Studio 经典版](#debugger-htb-access-tb-url-unsigned)
+ [选项 2：对于非 Studio Classic 环境](#debugger-htb-access-tb-url-presigned)

## 选项 1：适用于 SageMaker AI Studio 经典版
<a name="debugger-htb-access-tb-url-unsigned"></a>

如果您使用的是 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，请参阅以下部分中的选项 2。

## 选项 2：对于非 Studio Classic 环境
<a name="debugger-htb-access-tb-url-presigned"></a>

如果您使用非 Studio Classic 环境（例如 SageMaker 笔记本实例或 Amazon EC2），并且想要 TensorBoard 直接从您所在的环境中打开，则需要生成一个预先签名的 URL，其中包含您的域名和用户个人资料信息。*预签名* URL 是在使用您的域名和用户个人资料创建 URL 时登录到 Amazon SageMaker Studio Classic 的 URL，因此被授予访问与您的域名关联的所有域应用程序和文件的权限。要 TensorBoard 通过预签名 URL 打开，请使用带有您的域名和用户个人资料名称的`get_app_url`函数，如下所示。

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

**重要**  
请勿共享任何预签名 URLs。该`get_app_url`函数创建预签名 URLs，它会自动使用您的域和用户配置文件进行身份验证，并允许访问与您的域关联的任何应用程序和文件。

```
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`函数在后端运行 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker/client/create_presigned_domain_url.html](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker/client/create_presigned_domain_url.html)API。 适用于 Python (Boto3) 的 AWS SDK 由于 Boto3 `create_presigned_domain_url` API 会创建默认在 300 秒后过期 URLs的预签名域，因此预签名的 TensorBoard 应用程序 URLs也将在 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
```