使用 Debugger API 运行自定义规则
以下代码示例演示了如何使用 Amazon SageMaker Python SDK
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"} )
下面的列表解释了 Debugger Rule.custom API 参数。
-
name(字符串):指定所需的自定义规则名称。 -
image_uri(字符串):这是容器的映像,该容器中具有了解您的自定义规则的逻辑。它获取您保存在训练作业中的指定张量集合并进行评估。您可以在自定义规则评估器的 Amazon SageMaker Debugger 映像 URI 中找到开源 SageMaker AI 规则估算器映像列表。 -
instance_type(字符串):您需要指定一个实例来构建规则 Docker 容器。这将启动一个与训练容器并行的实例。 -
source(字符串):这是自定义规则脚本的本地路径或 Amazon S3 URI。 -
rule_to_invoke(字符串):此项指定了自定义规则脚本中的特定规则类实施。SageMaker AI 在规则作业中只支持一次评估一个规则。 -
collections_to_save(字符串):此项指定了要保存哪些张量集合用于要运行的规则。 -
rule_parameters(字典):这将接受字典格式的参数输入。您可以调整在自定义规则脚本中配置的参数。
在设置 custom_rule 对象之后,您可以使用它为任意训练作业构建 SageMaker AI 估算器。对您的训练脚本指定 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()
有关使用 Debugger 自定义规则的更多变体和高级示例,请参阅以下示例笔记本。