配置 SageMaker Debugger 以保存张量 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

配置 SageMaker Debugger 以保存张量

张量是在每次训练迭代中,向后和向前传递所更新参数的数据集合。SageMaker Debugger 收集输出张量以分析训练作业的状态。SageMaker Debugger 的 CollectionConfigDebuggerHookConfig API 操作提供了方法,用于将张量分组到集合中,然后将它们保存到目标 S3 存储桶。

注意

除非另外指定,否则在正确地进行配置和激活后,SageMaker Debugger 会将输出张量保存在默认 S3 存储桶中。默认 S3 存储桶 URI 的格式为 s3://sagemaker-<region>-<12digit_account_id>/<training-job-name>/debug-output/

在构造 SageMaker 估计器时,通过指定 debugger_hook_config 参数来激活 SageMaker Debugger。以下步骤提供的示例说明了如何使用 CollectionConfigDebuggerHookConfig API 操作设置 debugger_hook_config,以从训练作业中提取张量并保存它们。

使用 CollectionConfig API 配置张量集合

使用 CollectionConfig API 操作配置张量集合。在使用 Debugger 支持的深度学习框架和机器学习算法时,Debugger 提供预构建的张量集合,涵盖了参数的各种正则表达式 (regex)。如以下示例代码所示,添加要调试的内置张量集合。

from sagemaker.debugger import CollectionConfig collection_configs=[ CollectionConfig(name="weights"), CollectionConfig(name="gradients") ]

前面的集合设置 Debugger 钩子基于默认 "save_interval" 值,每 500 个步骤保存一次张量值。

有关可用的 Debugger 内置集合的完整列表,请参阅 Debugger 内置集合

如果您希望自定义内置集合,例如更改保存时间间隔和张量正则表达式,请使用以下 CollectionConfig 模板来调整参数。

from sagemaker.debugger import CollectionConfig collection_configs=[ CollectionConfig( name="tensor_collection", parameters={ "key_1": "value_1", "key_2": "value_2", ... "key_n": "value_n" } ) ]

有关可用参数键的详细信息,请参阅 Amazon SageMaker Python SDK 中的 CollectionConfig。例如,以下代码示例展示了如何调整在训练的不同阶段保存“losses”张量集合的时间间隔:在训练阶段每 100 个步骤保存一次损失,在验证阶段每 10 个步骤保存一次验证损失。

from sagemaker.debugger import CollectionConfig collection_configs=[ CollectionConfig( name="losses", parameters={ "train.save_interval": "100", "eval.save_interval": "10" } ) ]
提示

此张量集合配置对象可以用于 DebuggerHookConfigRule API 操作。

配置 DebuggerHookConfig API 以保存张量

通过 DebuggerHookConfig API,使用您在之前步骤中创建的 collection_configs 来创建 debugger_hook_config 对象。

from sagemaker.debugger import DebuggerHookConfig debugger_hook_config=DebuggerHookConfig( collection_configs=collection_configs )

Debugger 将模型训练输出张量保存到默认 S3 存储桶中。默认 S3 存储桶 URI 的格式为 s3://sagemaker-<region>-<12digit_account_id>/<training-job-name>/debug-output/.

如果您要指定确切的 S3 存储桶 URI,请使用以下代码示例:

from sagemaker.debugger import DebuggerHookConfig debugger_hook_config=DebuggerHookConfig( s3_output_path="specify-your-s3-bucket-uri" collection_configs=collection_configs )

有关更多信息,请参阅 Amazon SageMaker Python SDK 中的 DebuggerHookConfig

配置 Debugger 钩子的示例笔记本和代码示例

以下部分提供了如何使用 Debugger 钩子保存、访问并可视化输出张量的笔记本和代码示例。

张量可视化示例笔记本

以下两个笔记本示例说明了使用 Amazon SageMaker Debugger 可视化张量的高级用法。Debugger 提供了对训练深度学习模型的透明视图。

  • 使用 MXNet 的 SageMaker Studio 笔记本中的交互式张量分析

    此笔记本示例说明了如何使用 Amazon SageMaker Debugger 可视化保存的张量。通过可视化张量,您可以在训练深度学习算法时查看张量值如何变化。该笔记本包含一个具有未正确配置的神经网络的训练作业,并使用 Amazon SageMaker 调试程序聚合和分析张量,包括梯度、激活输出和权重。例如,下图显示了出现梯度消失问题的卷积层的梯度分布情况。

    
                        该图显示了出现梯度消失问题的卷积层的梯度分布情况

    该笔记本还说明了正确的初始超参数设置如何生成相同的张量分布图,从而改进了训练过程。

  • 从 MXNet 模型训练中可视化和调试张量

    此笔记本示例说明了如何使用 Amazon SageMaker Debugger 从 MXNet Gluon 模型训练作业中保存张量并进行可视化。它演示了将 Debugger 设置为将所有张量保存到 Amazon S3 存储桶中,并检索 ReLu 激活输出以进行可视化。下图显示了 ReLu 激活输出的三维可视化。颜色方案设置为以蓝色表示接近于 0 的值,以黄色表示接近于 1 的值。

    
                        ReLU 激活输出的可视化

    在此笔记本中,从 tensor_plot.py 导入的 TensorPlot 类设计用于绘制将二维图像作为输入的卷积神经网络 (CNN)。笔记本附带的 tensor_plot.py 脚本使用 Debugger 检索张量,并对 CNN 进行可视化。您可以在 SageMaker Studio 上运行此笔记本以重现张量可视化内容,并实施您自己的卷积神经网络模型。

  • 使用 MXNet 的 SageMaker 笔记本中的实时张量分析

    本示例将指导您完成安装所需的组件,以在 Amazon SageMaker 训练作业中发出张量,并在训练运行期间使用 Debugger API 操作访问这些张量。gluon CNN 模型在 Fashion MNIST 数据集上进行训练。在作业运行期间,您将看到 Debugger 如何从 100 个批次的每个批次中检索第一个卷积层的激活输出并对其进行可视化。此外,它还会向您展示作业完成后如何对权重进行可视化。

使用 Debugger 内置集合保存张量

您可以通过 CollectionConfig API 使用内置张量集合,并通过 DebuggerHookConfig API 保存它们。以下示例说明如何使用 Debugger 钩子配置的默认设置构造 SageMaker TensorFlow 估算器。您也可以将此用于 MXNet、PyTorch 和 XGBoost 估算器。

注意

在以下示例代码中,DebuggerHookConfigs3_output_path 参数可选。如果您没有指定该参数,Debugger 将张量保存在 s3://<output_path>/debug-output/,其中 <output_path> 是 SageMaker 训练作业的默认输出路径。例如:

"s3://sagemaker-us-east-1-111122223333/sagemaker-debugger-training-YYYY-MM-DD-HH-MM-SS-123/debug-output"
import sagemaker from sagemaker.tensorflow import TensorFlow from sagemaker.debugger import DebuggerHookConfig, CollectionConfig # use Debugger CollectionConfig to call built-in collections collection_configs=[ CollectionConfig(name="weights"), CollectionConfig(name="gradients"), CollectionConfig(name="losses"), CollectionConfig(name="biases") ] # configure Debugger hook # set a target S3 bucket as you want sagemaker_session=sagemaker.Session() BUCKET_NAME=sagemaker_session.default_bucket() LOCATION_IN_BUCKET='debugger-built-in-collections-hook' hook_config=DebuggerHookConfig( s3_output_path='s3://{BUCKET_NAME}/{LOCATION_IN_BUCKET}'. format(BUCKET_NAME=BUCKET_NAME, LOCATION_IN_BUCKET=LOCATION_IN_BUCKET), collection_configs=collection_configs ) # construct a SageMaker TensorFlow estimator sagemaker_estimator=TensorFlow( entry_point='directory/to/your_training_script.py', role=sm.get_execution_role(), base_job_name='debugger-demo-job', instance_count=1, instance_type="ml.p3.2xlarge", framework_version="2.9.0", py_version="py39", # debugger-specific hook argument below debugger_hook_config=hook_config ) sagemaker_estimator.fit()

要查看 Debugger 内置集合的列表,请参阅 Debugger 内置集合

使用修改后的 Debugger 内置集合保存张量

您可以使用 CollectionConfig API 操作修改 Debugger 内置集合。下面的示例演示了如何调整内置 losses 集合并构造 SageMaker TensorFlow 估算器。您也可以将此用于 MXNet、PyTorch 和 XGBoost 估算器。

import sagemaker from sagemaker.tensorflow import TensorFlow from sagemaker.debugger import DebuggerHookConfig, CollectionConfig # use Debugger CollectionConfig to call and modify built-in collections collection_configs=[ CollectionConfig( name="losses", parameters={"save_interval": "50"})] # configure Debugger hook # set a target S3 bucket as you want sagemaker_session=sagemaker.Session() BUCKET_NAME=sagemaker_session.default_bucket() LOCATION_IN_BUCKET='debugger-modified-collections-hook' hook_config=DebuggerHookConfig( s3_output_path='s3://{BUCKET_NAME}/{LOCATION_IN_BUCKET}'. format(BUCKET_NAME=BUCKET_NAME, LOCATION_IN_BUCKET=LOCATION_IN_BUCKET), collection_configs=collection_configs ) # construct a SageMaker TensorFlow estimator sagemaker_estimator=TensorFlow( entry_point='directory/to/your_training_script.py', role=sm.get_execution_role(), base_job_name='debugger-demo-job', instance_count=1, instance_type="ml.p3.2xlarge", framework_version="2.9.0", py_version="py39", # debugger-specific hook argument below debugger_hook_config=hook_config ) sagemaker_estimator.fit()

有关 CollectionConfig 参数的完整列表,请参阅 Debugger CollectionConfig API

使用 Debugger 自定义集合保存张量

您也可以选择保存精简数量的张量而不是完整的张量集(例如,当您希望减少保存在 Amazon S3 存储桶中的数据量时)。以下示例说明了如何自定义 Debugger 钩子配置以指定要保存的目标张量。您可以将此用于 TensorFlow、MXNet、PyTorch 和 XGBoost 估算器。

import sagemaker from sagemaker.tensorflow import TensorFlow from sagemaker.debugger import DebuggerHookConfig, CollectionConfig # use Debugger CollectionConfig to create a custom collection collection_configs=[ CollectionConfig( name="custom_activations_collection", parameters={ "include_regex": "relu|tanh", # Required "reductions": "mean,variance,max,abs_mean,abs_variance,abs_max" }) ] # configure Debugger hook # set a target S3 bucket as you want sagemaker_session=sagemaker.Session() BUCKET_NAME=sagemaker_session.default_bucket() LOCATION_IN_BUCKET='debugger-custom-collections-hook' hook_config=DebuggerHookConfig( s3_output_path='s3://{BUCKET_NAME}/{LOCATION_IN_BUCKET}'. format(BUCKET_NAME=BUCKET_NAME, LOCATION_IN_BUCKET=LOCATION_IN_BUCKET), collection_configs=collection_configs ) # construct a SageMaker TensorFlow estimator sagemaker_estimator=TensorFlow( entry_point='directory/to/your_training_script.py', role=sm.get_execution_role(), base_job_name='debugger-demo-job', instance_count=1, instance_type="ml.p3.2xlarge", framework_version="2.9.0", py_version="py39", # debugger-specific hook argument below debugger_hook_config=hook_config ) sagemaker_estimator.fit()

有关 CollectionConfig 参数的完整列表,请参阅 Debugger CollectionConfig