本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
为训练 Job 分析创建调试器自定义规则
您可以使用调试器规则 API 和开源创建自定义规则来监控训练作业smdebug Python 库
创建调试程序自定义规则的前提
要创建调试程序自定义规则,您需要满足以下先决条件。
使用调试器客户端库smdebug
创建自定义规则 Python 脚本
这些区域有:smdebug
规则 API 提供了一个界面来设置自己的自定义规则。以下 python 脚本是如何构建自定义规则的示例,CustomGradientRule
. 本教程自定义规则监视渐变是否太大并将默认阈值设置为 10。自定义规则在启动训练作业时采用 SageMaker 估算器创建的基本试用版。
from smdebug.rules.rule import Rule class CustomGradientRule(Rule): def __init__(self, base_trial, threshold=10.0): super().__init__(base_trial) self.threshold = float(threshold) def invoke_at_step(self, step): for tname in self.base_trial.tensor_names(collection="gradients"): t = self.base_trial.tensor(tname) abs_mean = t.reduction_value(step, "mean", abs=True) if abs_mean > self.threshold: return True return False
您可以在同一个 python 脚本中添加多个自定义规则类,并通过在下一节中构建自定义规则对象来将它们部署到任何训练作业试验中。
使用调试器 API 运行自己的自定义规则
以下代码示例演示如何使用Amazon SageMaker Python 开发工具包
from sagemaker.debugger import Rule, CollectionConfig custom_rule = Rule.custom( name='MyCustomRule', image_uri='759209512951.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rule-evaluator:latest', instance_type='ml.t3.medium', source='path/to/my_custom_rule.py', rule_to_invoke='CustomGradientRule', collections_to_save=[CollectionConfig("gradients")], rule_parameters={"threshold": "20.0"} )
以下列表解释了调试程序。Rule.custom
API 参数。
-
name
(str): 根据需要指定自定义规则名称。 -
image_uri
(str): 这是具有理解自定义规则逻辑的容器的图像。它来源和评估你在训练作业中保存的指定张量集合。您可以从以下位置找到开源 SageMaker 规则评估器图像列表:Amazon SageMaker 调试程序的自定义规则评估程序的注册表 URL. -
instance_type
(str): 您需要指定一个实例才能构建规则 docker 容器。这将与训练容器并行旋转实例。 -
source
(str): 这是自定义规则脚本的本地路径或 Amazon S3 URI。 -
rule_to_invoke
(str): 这指定了自定义规则脚本中的特定规则类实现。SageMaker 在规则作业中一次只支持一个要评估的规则。 -
collections_to_save
(str): 这指定了为了运行规则,您将保存哪些张量集合。 -
rule_parameters
(字典):这接受字典格式的参数输入。您可以调整在自定义规则脚本中配置的参数。
在设置完custom_rule
对象,您可以使用它为任何训练作业构建 SageMaker 估算器。指定entry_point
到你的训练脚本中。您无需对训练脚本进行任何更改。
from sagemaker.tensorflow import TensorFlow estimator = TensorFlow( role=sagemaker.get_execution_role(), base_job_name='smdebug-custom-rule-demo-tf-keras', entry_point='path/to/your_training_script.py' train_instance_type='ml.p2.xlarge' ... # debugger-specific arguments below rules = [custom_rule] ) estimator.fit()
有关使用调试器自定义规则的更多变体和高级示例,请参阅以下示例笔记本。