

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# SDK for Python (Boto3)
<a name="debugger-built-in-rules-api.Boto3"></a>

Amazon SageMaker Debugger の組み込みルールは、 AWS Boto3 SageMaker AI クライアントの [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job)関数を使用してトレーニングジョブ用に設定できます。正しいイメージ URI を`RuleEvaluatorImage` パラメータに指定する必要があります。次の例では、[https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job) 関数のリクエストボディの設定方法を具体的に説明します。

次のコードは、トレーニングスクリプト `entry_point/train.py` が TensorFlow を使って準備されていることを前提に、`create_training_job()` リクエストボディにデバッガーを設定し、`us-west-2` でトレーニングジョブを開始する方法の完全な例を示しています。エンドツーエンドのサンプルノートブックを見つけるには、「[Amazon SageMaker デバッガー (Boto3) を使って TensorFlow マルチ GPU マルチノードトレーニングジョブをプロファイリングする](https://sagemaker-examples.readthedocs.io/en/latest/sagemaker-debugger/tensorflow_profiling/tf-resnet-profiling-multi-gpu-multi-node-boto3.html)」を参照してください。

**注記**  
正しい Docker コンテナイメージを使っていることを確認してください。利用可能な AWS Deep Learning Container イメージを確認するには、[「利用可能な Deep Learning Containers イメージ](https://github.com/aws/deep-learning-containers/blob/master/available_images.md)」を参照してください。デバッガールールを使用するために利用可能な Docker イメージの完全なリストを見つけるには、「[Debugger ルールの Docker イメージ](debugger-reference.md#debugger-docker-images-rules)」を参照してください。

```
import sagemaker, boto3
import datetime, tarfile

# Start setting up a SageMaker session and a Boto3 SageMaker client
session = sagemaker.Session()
region = session.boto_region_name
bucket = session.default_bucket()

# Upload a training script to a default Amazon S3 bucket of the current SageMaker session
source = 'source.tar.gz'
project = 'debugger-boto3-test'

tar = tarfile.open(source, 'w:gz')
tar.add ('entry_point/train.py') # Specify the directory and name of your training script
tar.close()

s3 = boto3.client('s3')
s3.upload_file(source, bucket, project+'/'+source)

# Set up a Boto3 session client for SageMaker
sm = boto3.Session(region_name=region).client("sagemaker")

# Start a training job
sm.create_training_job(
    TrainingJobName='debugger-boto3-'+datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S'),
    HyperParameters={
        'sagemaker_submit_directory': 's3://'+bucket+'/'+project+'/'+source,
        'sagemaker_program': '/entry_point/train.py' # training scrip file location and name under the sagemaker_submit_directory
    },
    AlgorithmSpecification={
        # Specify a training Docker container image URI (Deep Learning Container or your own training container) to TrainingImage.
        'TrainingImage': '763104351884.dkr.ecr.us-west-2.amazonaws.com/tensorflow-training:2.4.1-gpu-py37-cu110-ubuntu18.04',
        'TrainingInputMode': 'File',
        'EnableSageMakerMetricsTimeSeries': False
    },
    RoleArn='arn:aws:iam::111122223333:role/service-role/AmazonSageMaker-ExecutionRole-20201014T161125',
    OutputDataConfig={'S3OutputPath': 's3://'+bucket+'/'+project+'/output'},
    ResourceConfig={
        'InstanceType': 'ml.p3.8xlarge',
        'InstanceCount': 1,
        'VolumeSizeInGB': 30
    },
    StoppingCondition={
        'MaxRuntimeInSeconds': 86400
    },
    DebugHookConfig={
        'S3OutputPath': 's3://'+bucket+'/'+project+'/debug-output',
        'CollectionConfigurations': [
            {
                'CollectionName': 'losses',
                'CollectionParameters' : {
                    'train.save_interval': '500',
                    'eval.save_interval': '50'
                }
            }
        ]
    },
    DebugRuleConfigurations=[
        {
            'RuleConfigurationName': 'LossNotDecreasing',
            'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest',
            'RuleParameters': {'rule_to_invoke': 'LossNotDecreasing'}
        }
    ],
    ProfilerConfig={
        'S3OutputPath': 's3://'+bucket+'/'+project+'/profiler-output',
        'ProfilingIntervalInMilliseconds': 500,
        'ProfilingParameters': {
            'DataloaderProfilingConfig': '{"StartStep": 5, "NumSteps": 3, "MetricsRegex": ".*", }',
            'DetailedProfilingConfig': '{"StartStep": 5, "NumSteps": 3, }',
            'PythonProfilingConfig': '{"StartStep": 5, "NumSteps": 3, "ProfilerName": "cprofile", "cProfileTimer": "total_time"}',
            'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs
        }
    },
    ProfilerRuleConfigurations=[
        {
            'RuleConfigurationName': 'ProfilerReport',
            'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest',
            'RuleParameters': {'rule_to_invoke': 'ProfilerReport'}
        }
    ]
)
```

## モデルパラメータをデバッグするためのデバッガールールを設定するには
<a name="debugger-built-in-rules-api-debug.Boto3"></a>

次のコードサンプルは、この SageMaker API を使って組み込み `VanishingGradient` ルールを設定する方法を示しています。

**デバッガーが出力テンソルを収集できるようにするには**

デバッガーフック設定を次のように指定します。

```
DebugHookConfig={
    'S3OutputPath': 's3://<default-bucket>/<training-job-name>/debug-output',
    'CollectionConfigurations': [
        {
            'CollectionName': 'gradients',
            'CollectionParameters' : {
                'train.save_interval': '500',
                'eval.save_interval': '50'
            }
        }
    ]
}
```

これにより、トレーニングジョブは 500 ステップの `gradients` ごとに、テンソルコレクション、`save_interval` を保存します。利用可能な `CollectionName` 値を見つけるには、*SMDebug クライアントライブラリドキュメント*の「[デバッガー組み込みコレクション](https://github.com/awslabs/sagemaker-debugger/blob/master/docs/api.md#built-in-collections)」を参照してください。利用可能な `CollectionParameters` パラメータのキーと値を見つけるには、*SageMaker Python SDK ドキュメント*の [https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.CollectionConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.CollectionConfig) クラスを参照してください。

**出力テンソルをデバッグするためのデバッガールールを有効にするには**

次の `DebugRuleConfigurations` API の例は、保存された `gradients` コレクションで組み込みの `VanishingGradient` ルールを実行する方法を示しています。

```
DebugRuleConfigurations=[
    {
        'RuleConfigurationName': 'VanishingGradient',
        'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest',
        'RuleParameters': {
            'rule_to_invoke': 'VanishingGradient',
            'threshold': '20.0'
        }
    }
]
```

このサンプルのような設定では、デバッガーは `gradients` テンソルのコレクションで `VanishingGradient` ルールを使ってトレーニングジョブのルール評価ジョブを開始します。デバッガールールを使用するために利用可能な Docker イメージの完全なリストを見つけるには、「[Debugger ルールの Docker イメージ](debugger-reference.md#debugger-docker-images-rules)」を参照してください。`RuleParameters` のキーバリューのペアを見つけるには、「[デバッガーの組み込みルールのリスト](debugger-built-in-rules.md)」を参照してください。

## システムおよびフレームワークのメトリクスをプロファイリングするためのデバッガー組み込みルールを設定するには
<a name="debugger-built-in-rules-api-profile.Boto3"></a>

次のサンプルコードは、Profiler Config API オペレーションを指定して、システムおよびフレームワークのメトリクスの収集を有効にする方法を示しています。

**システムおよびフレームワークのメトリクスを収集するためにデバッガープロファイリングを有効にするには**

------
#### [ Target Step ]

```
ProfilerConfig={ 
    'S3OutputPath': 's3://<default-bucket>/<training-job-name>/profiler-output', # Optional. Path to an S3 bucket to save profiling outputs
    # Available values for ProfilingIntervalInMilliseconds: 100, 200, 500, 1000 (1 second), 5000 (5 seconds), and 60000 (1 minute) milliseconds.
    'ProfilingIntervalInMilliseconds': 500, 
    'ProfilingParameters': {
        'DataloaderProfilingConfig': '{
            "StartStep": 5, 
            "NumSteps": 3, 
            "MetricsRegex": ".*"
        }',
        'DetailedProfilingConfig': '{
            "StartStep": 5, 
            "NumSteps": 3 
        }',
        'PythonProfilingConfig': '{
            "StartStep": 5, 
            "NumSteps": 3, 
            "ProfilerName": "cprofile",  # Available options: cprofile, pyinstrument
            "cProfileTimer": "total_time"  # Include only when using cprofile. Available options: cpu, off_cpu, total_time
        }',
        'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs
    }
}
```

------
#### [ Target Time Duration ]

```
ProfilerConfig={ 
    'S3OutputPath': 's3://<default-bucket>/<training-job-name>/profiler-output', # Optional. Path to an S3 bucket to save profiling outputs
    # Available values for ProfilingIntervalInMilliseconds: 100, 200, 500, 1000 (1 second), 5000 (5 seconds), and 60000 (1 minute) milliseconds.
    'ProfilingIntervalInMilliseconds': 500,
    'ProfilingParameters': {
        'DataloaderProfilingConfig': '{
            "StartTimeInSecSinceEpoch": 12345567789, 
            "DurationInSeconds": 10, 
            "MetricsRegex": ".*"
        }',
        'DetailedProfilingConfig': '{
            "StartTimeInSecSinceEpoch": 12345567789, 
            "DurationInSeconds": 10
        }',
        'PythonProfilingConfig': '{
            "StartTimeInSecSinceEpoch": 12345567789, 
            "DurationInSeconds": 10, 
            "ProfilerName": "cprofile",  # Available options: cprofile, pyinstrument
            "cProfileTimer": "total_time"  # Include only when using cprofile. Available options: cpu, off_cpu, total_time
        }',
        'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs
    }
}
```

------

**メトリクスをプロファイリングするためのデバッガールールを有効にするには**

次のサンプルコードは、`ProfilerReport` ルールの設定方法を示しています。

```
ProfilerRuleConfigurations=[ 
    {
        'RuleConfigurationName': 'ProfilerReport',
        'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest',
        'RuleParameters': {
            'rule_to_invoke': 'ProfilerReport',
            'CPUBottleneck_cpu_threshold': '90',
            'IOBottleneck_threshold': '90'
        }
    }
]
```

デバッガールールを使用するために利用可能な Docker イメージの完全なリストを見つけるには、「[Debugger ルールの Docker イメージ](debugger-reference.md#debugger-docker-images-rules)」を参照してください。`RuleParameters` のキーバリューのペアを見つけるには、「[デバッガーの組み込みルールのリスト](debugger-built-in-rules.md)」を参照してください。

## `UpdateTrainingJob` API オペレーションを使ってデバッガープロファイリング設定を更新する
<a name="debugger-updatetrainingjob-api.Boto3"></a>

デバッガープロファイリング設定は、 AWS Boto3 SageMaker AI クライアントの [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.update_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.update_training_job)関数を使用して、トレーニングジョブの実行中に更新できます。新しい [ProfilerConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ProfilerConfig.html) および [ProfilerRuleConfiguration](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ProfilerRuleConfiguration.html) オブジェクトを設定し、`TrainingJobName` パラメータにトレーニングジョブ名を指定します。

```
ProfilerConfig={ 
    'DisableProfiler': boolean,
    'ProfilingIntervalInMilliseconds': number,
    'ProfilingParameters': { 
        'string' : 'string' 
    }
},
ProfilerRuleConfigurations=[ 
    { 
        'RuleConfigurationName': 'string',
        'RuleEvaluatorImage': 'string',
        'RuleParameters': { 
            'string' : 'string' 
        }
    }
],
TrainingJobName='your-training-job-name-YYYY-MM-DD-HH-MM-SS-SSS'
```

## CreateTrainingJob API オペレーションにデバッガーカスタムルール設定を追加する
<a name="debugger-custom-rules-api.Boto3"></a>

カスタムルールは、[DebugHookConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DebugHookConfig.html) オブジェクトと [ DebugRuleConfiguration](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DebugRuleConfiguration.html) オブジェクトを使用して AWS Boto3 SageMaker AI クライアントの [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job)関数を使用してトレーニングジョブ用に設定できます。次のコードサンプルは、この SageMaker API オペレーションを使用して、*smdebug* ライブラリを使って記述されたカスタム `ImproperActivation` ルールを設定する方法を示しています。この例では、カスタムルールを *custom\$1rules.py* ファイルに記述し、Amazon S3 バケットにアップロード済みであることを前提としています。例では、カスタムルールを実行するために使用できる構築済み Docker イメージを提供しています。これらについては、「[カスタムルール評価用の Amazon SageMaker Debugger イメージの URI](debugger-reference.md#debuger-custom-rule-registry-ids)」を参照してください。`RuleEvaluatorImage` パラメータで、ビルド済み Docker イメージの URL レジストリアドレスを指定します。

```
DebugHookConfig={
    'S3OutputPath': 's3://<default-bucket>/<training-job-name>/debug-output',
    'CollectionConfigurations': [
        {
            'CollectionName': 'relu_activations',
            'CollectionParameters': {
                'include_regex': 'relu',
                'save_interval': '500',
                'end_step': '5000'
            }
        }
    ]
},
DebugRulesConfigurations=[
    {
        'RuleConfigurationName': 'improper_activation_job',
        'RuleEvaluatorImage': '552407032007.dkr.ecr.ap-south-1.amazonaws.com/sagemaker-debugger-rule-evaluator:latest',
        'InstanceType': 'ml.c4.xlarge',
        'VolumeSizeInGB': 400,
        'RuleParameters': {
           'source_s3_uri': 's3://bucket/custom_rules.py',
           'rule_to_invoke': 'ImproperActivation',
           'collection_names': 'relu_activations'
        }
    }
]
```

デバッガールールを使用するために利用可能な Docker イメージの完全なリストを見つけるには、「[Debugger ルールの Docker イメージ](debugger-reference.md#debugger-docker-images-rules)」を参照してください。`RuleParameters` のキーバリューのペアを見つけるには、「[デバッガーの組み込みルールのリスト](debugger-built-in-rules.md)」を参照してください。