

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

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