

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 SageMaker AI 上存取 TensorBoard 應用程式
<a name="debugger-htb-access-tb"></a>

您可以透過兩種方法存取 TensorBoard：透過程式設計方式使用產生未登入或預先登入 URL 的 `sagemaker.interactive_apps.tensorboard` 模組，或使用 SageMaker AI 主控台中的 TensorBoard 登陸頁面。開啟 Tensorboard 之後，SageMaker AI 會執行 TensorBoard 外掛程式，並自動尋找所有訓練任務輸出資料，並以 TensorBoard 相容的檔案格式自動尋找。

**Topics**
+ [使用`sagemaker.interactive_apps.tensorboard`模組開啟 TensorBoard](debugger-htb-access-tb-url.md)
+ [使用`get_app_url`函式做為`estimator`類方法來開啟 TensorBoard](debugger-htb-access-tb-get-app-url-estimator-method.md)
+ [透過 SageMaker AI 主控台開啟 TensorBoard](debugger-htb-access-tb-console.md)

# 使用`sagemaker.interactive_apps.tensorboard`模組開啟 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 環境，您可以透過依原樣執行 `get_app_url()` 函式來開啟 TensorBoard，也可以指定任務名稱，在 TensorBoard 應用程式開啟時開始追蹤。對於非 Studio Classic 環境，您可以將網域和使用者設定檔資訊提供給公用程式功能，以開啟 TensorBoard。使用此功能，無論您在何處或以何種方式執行訓練程式碼及啟動訓練任務，都可以透過在您的 Jupyter 筆記本或終端機中執行 `get_app_url` 函式直接存取 TensorBoard。

**注意**  
此功能可在 SageMaker Python SDK v2.184.0 及更新版本中使用。若要使用此功能，請務必透過`pip install sagemaker --upgrade`執行升級 SDK。

**Topics**
+ [選項 1：適用於 SageMaker AI Studio Classic](#debugger-htb-access-tb-url-unsigned)
+ [選項 2：適用於非 Studio Classic 環境](#debugger-htb-access-tb-url-presigned)

## 選項 1：適用於 SageMaker AI Studio Classic
<a name="debugger-htb-access-tb-url-unsigned"></a>

如果您使用的是 SageMaker Studio Classic，您可以按照下列說明執行 `get_app_url` 函式，直接開啟 TensorBoard 應用程式或擷取未登入的 URL。由於您已經在 Studio Classic 環境中以網域使用者身分登入，不需要再次驗證，因此 `get_app_url()` 會產生未登入的 URL。

**開啟 TensorBoard 應用程式** 

下列程式碼會將從`get_app_url()`函式傳回的未登入 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，您可以複製到網頁瀏覽器並開啟 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 環境之外執行前兩個程式碼範例，該函式會將 TensorBoard 登陸頁面的 URL 傳回至 SageMaker AI 主控台，因為這些範例沒有您的網域和使用者設定檔的登入資訊。如需建立預先登入的 URL，請參閱下一節中的選項 2。

## 選項 2：適用於非 Studio Classic 環境
<a name="debugger-htb-access-tb-url-presigned"></a>

如果您使用非 Studio Classic 環境 (例如 SageMaker 筆記本執行個體或 Amazon EC2)，並且想要直接從您所在的環境開啟 TensorBoard，則需要使用網域和使用者設定檔資訊產生預先登入的 URL。*預先登入*的 URL 是已登入 Amazon SageMaker Studio Classic 的 URL，由於您當時正在使用您的網域和使用者設定檔，因此會授予與您網域相關聯的所有網域應用程式和檔案的存取權。若要透過預先登入的網址開啟 TensorBoard，請依照下列方式使用您的網域和使用者設定檔名稱的 `get_app_url` 功能。

請注意，此選項需要網域使用者擁有 `sagemaker:CreatePresignedDomainUrl` 許可權限。如果沒有許可權限，網域使用者將會收到例外狀況錯誤。

**重要**  
請勿與他人分享任何預先登入的 URL。此 `get_app_url` 功能會建立預先登入的 URL，該 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 中執行 [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。由於 Boto3 `create_presigned_domain_url` API 建立預先登入的網域網址，預設會在 300 秒內到期，因此預先登入的 TensorBoard 應用程式 URL 也會在 300 秒後到期。如果要延長到期時間，請將`ExpiresInSeconds`引數發送至`get_app_url`功能的`optional_create_presigned_url_kwargs`參數，如下所示。  

```
optional_create_presigned_url_kwargs={"ExpiresInSeconds": 1500}
```

**注意**  
如果您傳遞給`get_app_url`引數的輸入有任何無效值，函式會將 URL 輸出到 TensorBoard 登陸頁面，而不會開啟 TensorBoard 應用程式。輸出訊息可能類似以下內容。  

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

# 使用`get_app_url`函式做為`estimator`類方法來開啟 TensorBoard
<a name="debugger-htb-access-tb-get-app-url-estimator-method"></a>

如果您正在使用 SageMaker Python SDK 的`estimator`類別執行訓練任務，並且具有`estimator`類別的使用中物件，您也可以將[`get_app_url`函式當做`estimator`類的類別方法](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.EstimatorBase.get_app_url)存取。開啟 TensorBoard 應用程式，或透過執行`get_app_url`方法擷取未登入的 URL，如下所示。`get_app_url`類方法會從估算器擷取訓練任務名稱，並開啟具有指定任務的 TensorBoard 應用程式。

**注意**  
此功能可在 SageMaker Python SDK v2.184.0 及更新版本中使用。若要使用此功能，請務必透過`pip install sagemaker --upgrade`執行升級 SDK。

**Topics**
+ [選項 1：適用於 SageMaker Studio Classic](#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 Studio Classic
<a name="debugger-htb-access-tb-get-app-url-estimator-method-studio"></a>

**開啟 TensorBoard 應用程式** 

下列程式碼會將從`get_app_url()`方法傳回的未登入 URL，以環境預設的網頁瀏覽器自動開啟 TensorBoard 應用程式。

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

**擷取未登入的 URL 並手動開啟 TensorBoard 應用程式**

下列程式碼會列印未登入的 URL，您可以複製到網頁瀏覽器並開啟 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 環境之外執行前兩個程式碼範例，該函式會將 TensorBoard 登陸頁面的 URL 傳回至 SageMaker AI 主控台，因為這些範例沒有您的網域和使用者設定檔的登入資訊。如需建立預先登入的 URL，請參閱下一節中的選項 2。

## 選項 2：適用於非 Studio Classic 環境
<a name="debugger-htb-access-tb-get-app-url-estimator-method-non-studio"></a>

如果您使用非 Studio Classic 環境 (例如 SageMaker 筆記本執行個體或 Amazon EC2)，並且想要產生預先登入 URL 以開啟 TensorBoard，您需要使用 `get_app_url` 方法，透過您的網域和使用者設定檔資訊進行，如下所示。

請注意，此選項需要網域使用者擁有 `sagemaker:CreatePresignedDomainUrl` 許可權限。如果沒有許可權限，網域使用者將會收到例外狀況錯誤。

**重要**  
請勿與他人分享任何預先登入的 URL。此 `get_app_url` 功能會建立預先登入的 URL，該 URL 會自動使用您的網域和使用者設定檔進行驗證，並可存取與您網域相關聯的任何應用程式和檔案。

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

# 透過 SageMaker AI 主控台開啟 TensorBoard
<a name="debugger-htb-access-tb-console"></a>

您也可以使用 SageMaker AI 主控台使用者介面來開啟 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 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 在左側導覽窗格中，選擇**管理員組態**。

1. 在**管理員組態**下，選擇**網域**。

1. 從網域清單中選取您要啟動 TensorBoard 應用程式的網域。

**啟動使用者設定檔應用程式**

下列程序顯示如何啟動範圍為使用者設定檔的 Studio Classic 應用程式。

1. 在網域詳細資訊頁面，選取**使用者設定檔**索引標籤。

1. 識別您要啟動 Studio Classic 應用程式的使用者設定檔。

1. 以選定的使用者設定檔選擇**啟動**，然後選擇 **TensorBoard**。

## 選項 2：從 TensorBoard 登陸頁面啟動 TensorBoard
<a name="debugger-htb-access-tb-console-landing-pg"></a>

下列程序說明如何從 TensorBoard 登陸頁面啟動 TensorBoard 應用程式。

1. 開啟位在 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/) 的 Amazon SageMaker AI 主控台。

1. 在左側導覽面板上，選擇 **TensorBoard**。

1. 在**立即開始**下方，選取您要啟動 Studio Classic 應用程式的網域。如果您的使用者設定檔只屬於一個網域，您將看不到選取網域的選項。

1. 選取您要用來啟動 Studio Classic 應用程式的使用者設定檔。如果網域中沒有使用者設定檔，請選取**建立使用者設定檔**。如需更多資訊，請參閱[新增和移除使用者設定檔](https://docs.aws.amazon.com/sagemaker/latest/dg/domain-user-profile-add.html)。

1. 選擇**開啟 TensorBoard**。

下列螢幕擷取畫面示範 TensorBoard 在 SageMaker AI 主控台的左側導覽面板中的位置，以及主面板中具有 TensorBoard 登陸頁面的 SageMaker AI。

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