準備訓練任務以收集 TensorBoard 輸出資料 - Amazon SageMaker

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

準備訓練任務以收集 TensorBoard 輸出資料

中的機器學習典型訓練任務 SageMaker 包含兩個主要步驟:準備訓練指令碼和設定 SageMaker Python 的 SageMaker 估算器物件SDK。在本節中,您將了解從 SageMaker 訓練任務收集 TensorBoard相容 資料所需的變更。

必要條件

下列清單顯示與 SageMaker 開始使用 的先決條件 TensorBoard。

  • VPC AWS 帳戶中使用 Amazon 設定的 SageMaker 網域。

    如需設定網域的指示,請參閱使用快速設定加入 Amazon SageMaker 網域。您也需要為個別使用者新增網域使用者設定檔,才能存取 TensorBoard 上的 SageMaker。如需詳細資訊,請參閱新增使用者設定檔

  • 下列清單是在 TensorBoard 上使用 的最小許可集 SageMaker。

    • sagemaker:CreateApp

    • sagemaker:DeleteApp

    • sagemaker:DescribeTrainingJob

    • sagemaker:Search

    • s3:GetObject

    • s3:ListBucket

步驟 1:使用開放原始碼 TensorBoard 協助工具修改訓練指令碼

請務必決定要收集的輸出張量和純量,並使用下列任何工具修改訓練指令碼中的程式碼行: TensorBoardX、 TensorFlow 摘要寫入器、 PyTorch 摘要寫入器或 SageMaker 偵錯器。

此外,請確定您在訓練容器中將 TensorBoard 資料輸出路徑指定為回呼的日誌目錄 (log_dir)。

如需每個架構回調的更多相關資訊,請參閱下列資源。

步驟 2:使用 TensorBoard 輸出組態建立 SageMaker 訓練估算器物件

在設定 SageMaker 架構估算器sagemaker.debugger.TensorBoardOutputConfig時使用 。此組態會API映射您為儲存 TensorBoard 資料所指定的 S3 儲存貯體,並在訓練容器中使用本機路徑 (/opt/ml/output/tensorboard)。將模組的物件傳遞給估算器類別的tensorboard_output_config參數。下列程式碼片段顯示使用 TensorBoard 輸出組態參數準備 TensorFlow 估算器的範例。

注意

此範例假設您使用 SageMaker Python SDK。如果您使用低階 SageMaker API,則應在 CreateTrainingJob 的請求語法中包含下列項目API。

"TensorBoardOutputConfig": { "LocalPath": "/opt/ml/output/tensorboard", "S3OutputPath": "s3_output_bucket" }
from sagemaker.tensorflow import TensorFlow from sagemaker.debugger import TensorBoardOutputConfig # Set variables for training job information, # such as s3_out_bucket and other unique tags. ... LOG_DIR="/opt/ml/output/tensorboard" output_path = os.path.join( "s3_output_bucket", "sagemaker-output", "date_str", "your-training_job_name" ) tensorboard_output_config = TensorBoardOutputConfig( s3_output_path=os.path.join(output_path, 'tensorboard'), container_local_output_path=LOG_DIR ) estimator = TensorFlow( entry_point="train.py", source_dir="src", role=role, image_uri=image_uri, instance_count=1, instance_type="ml.c5.xlarge", base_job_name="your-training_job_name", tensorboard_output_config=tensorboard_output_config, hyperparameters=hyperparameters )
注意

TensorBoard 應用程式不支援 out-of-the-box SageMaker超參數調校任務,因為 CreateHyperParameterTuningJob API 未與映射的 TensorBoard 輸出組態整合。若要使用 TensorBoard應用程式進行超參數調校任務,您需要在訓練指令碼中編寫程式碼,以將指標上傳至 Amazon S3。將指標上傳至 Amazon S3 儲存貯體後,您就可以將儲存貯體載入 上的 TensorBoard 應用程式 SageMaker。