

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

# 在 SageMaker AI 上访问 TensorBoard 应用程序
<a name="debugger-htb-access-tb"></a>

您可以 TensorBoard 通过两种方法进行访问：以编程方式使用生成未签名或预签名 URL 的`sagemaker.interactive_apps.tensorboard`模块，或者使用 AI 控制台中的 TensorBoard SageMaker 登录页面。打开后 TensorBoard， SageMaker AI 会运行 TensorBoard 插件并自动查找所有训练作业输出数据均为 TensorBoard兼容文件格式。

**Topics**
+ [TensorBoard 使用`sagemaker.interactive_apps.tensorboard`模块打开](debugger-htb-access-tb-url.md)
+ [TensorBoard 使用`get_app_url`函数作为`estimator`类方法打开](debugger-htb-access-tb-get-app-url-estimator-method.md)
+ [TensorBoard 通过 SageMaker AI 控制台打开](debugger-htb-access-tb-console.md)

# 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
```

# TensorBoard 使用`get_app_url`函数作为`estimator`类方法打开
<a name="debugger-htb-access-tb-get-app-url-estimator-method"></a>

如果您正在使用 SageMaker Python SDK 的`estimator`类运行训练作业，并且有该`estimator`类的活动对象，则也可以将该[`get_app_url`函数作为该类的类方法](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.EstimatorBase.get_app_url)进行`estimator`访问。打开 TensorBoard 应用程序或通过运行`get_app_url`方法来检索未签名的 URL，如下所示。`get_app_url`类方法从估算器中提取训练作业名称，然后使用指定作业打开 TensorBoard 应用程序。

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

**Topics**
+ [选项 1：适用于经典 SageMaker 工作室](#debugger-htb-access-tb-get-app-url-estimator-method-studio)
+ [选项 2：对于非 Studio Classic 环境](#debugger-htb-access-tb-get-app-url-estimator-method-non-studio)

## 选项 1：适用于经典 SageMaker 工作室
<a name="debugger-htb-access-tb-get-app-url-estimator-method-studio"></a>

**打开 TensorBoard 应用程序** 

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

```
estimator.get_app_url(
    app_type=SupportedInteractiveAppTypes.TENSORBOARD # Required.
)
```

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

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

```
print(
    estimator.get_app_url(
        app_type=SupportedInteractiveAppTypes.TENSORBOARD, # Required.
        open_in_default_web_browser=False, # Optional. 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-get-app-url-estimator-method-non-studio"></a>

如果您使用非 Studio Classic 环境（例如 SageMaker 笔记本实例和 Amazon EC2），并且想要生成用于打开 TensorBoard 应用程序的预签名 URL，请按如下方式使用包含您的域和用户个人资料信息的`get_app_url`方法。

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

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

```
print(
    estimator.get_app_url(
        app_type=SupportedInteractiveAppTypes.TENSORBOARD, # Required
        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
    )
)
```

# TensorBoard 通过 SageMaker AI 控制台打开
<a name="debugger-htb-access-tb-console"></a>

您也可以使用 SageMaker AI 控制台 UI 打开 TensorBoard 应用程序。通过 SageMaker AI 控制台打开 TensorBoard 应用程序有两个选项。

**Topics**
+ [选项 1： TensorBoard 从域名详情页面启动](#debugger-htb-access-tb-console-domain-detail)
+ [选项 2： TensorBoard 从 TensorBoard 登录页面启动](#debugger-htb-access-tb-console-landing-pg)

## 选项 1： TensorBoard 从域名详情页面启动
<a name="debugger-htb-access-tb-console-domain-detail"></a>

**导航到域详细信息页面**

 以下过程展示如何导航到域详细信息页面。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 在左侧导航窗格中，选择**管理员配置**。

1. 在**管理员配置**下，选择**域**。

1. 从域列表中，选择要在其中启动 TensorBoard 应用程序的域。

**启动用户配置文件应用程序**

以下过程说明如何启动作用域为用户配置文件的 Studio Classic 应用程序。

1. 在域详细信息页面上，选择**用户配置文件**选项卡。

1. 确定要为其启动 Studio Classic 应用程序的用户配置文件。

1. 为所选用户个人资料选择 La **unch**，然后选择**TensorBoard**。

## 选项 2： TensorBoard 从 TensorBoard 登录页面启动
<a name="debugger-htb-access-tb-console-landing-pg"></a>

以下过程介绍如何从 TensorBoard 登录页面启动 TensorBoard 应用程序。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 在左侧导航窗格中，选择**TensorBoard**。

1. 在**开始使用**下，选择要在其中启动 Studio Classic 应用程序的域。如果您的用户配置文件仅属于一个域，则看不到用于选择域的选项。

1. 选择要为其启动 Studio Classic 应用程序的用户配置文件。如果域中没有用户配置文件，请选择**创建用户配置文件**。有关更多信息，请参阅[添加和删除用户配置文件](https://docs.aws.amazon.com/sagemaker/latest/dg/domain-user-profile-add.html)。

1. 选择**打开 TensorBoard**。

以下屏幕截图显示了 SageMaker AI 控制台左侧导航窗格 TensorBoard 中的位置，以及主窗格中带有 TensorBoard 登录页面的 SageMaker AI 的位置。

![\[TensorBoard 登陆页面\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/debugger/htb-landing-page.png)
