

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

# モニタリングジョブのスケジューリング
<a name="model-monitor-scheduling"></a>

Amazon SageMaker Model Monitor には、リアルタイムエンドポイントから収集されたデータをモニタリングする機能があります。データを定期的なスケジュールでモニタリングすることも、1 回だけ即時にモニタリングすることもできます。[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateMonitoringSchedule.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateMonitoringSchedule.html) API を使用してモニタリングスケジュールを作成できます。

モニタリングスケジュールを使用すると、SageMaker AIはジョブの処理を開始して、特定の期間に収集されたデータを分析できます。処理ジョブでは、SageMaker AI は現在の分析のデータセットを、指定されたベースライン統計および制約と比較します。次に、SageMaker AI は違反レポートを生成します。さらに、分析中の各特徴量について CloudWatch メトリクスが出力されます。

SageMaker AI には、表形式のデータセットを分析するためのビルド済みコンテナが用意されています。または、「[Amazon SageMaker Model Monitor を使用した独自のコンテナのサポート](model-monitor-byoc-containers.md)」のトピックで概説されているように、独自のコンテナを持ち込むこともできます。

リアルタイムエンドポイントまたはバッチ変換ジョブのモデルモニタリングスケジュールを作成できます。ベースラインリソース (制約および統計) を使用して、リアルタイムトラフィックまたはバッチジョブと比較します。

**Example ベースライン割り当て**  
次の例では、モデルのトレーニングに使用されたトレーニングデータセットが Amazon S3 にアップロードされました。データセットが Amazon S3 にすでに存在する場合は、それを直接指定できます。  

```
# copy over the training dataset to Amazon S3 (if you already have it in Amazon S3, you could reuse it)
baseline_prefix = prefix + '/baselining'
baseline_data_prefix = baseline_prefix + '/data'
baseline_results_prefix = baseline_prefix + '/results'

baseline_data_uri = 's3://{}/{}'.format(bucket,baseline_data_prefix)
baseline_results_uri = 's3://{}/{}'.format(bucket, baseline_results_prefix)
print('Baseline data uri: {}'.format(baseline_data_uri))
print('Baseline results uri: {}'.format(baseline_results_uri))
```

```
training_data_file = open("test_data/training-dataset-with-header.csv", 'rb')
s3_key = os.path.join(baseline_prefix, 'data', 'training-dataset-with-header.csv')
boto3.Session().resource('s3').Bucket(bucket).Object(s3_key).upload_fileobj(training_data_file)
```

**Example 定期分析のスケジュール**  
リアルタイムエンドポイントのモデルモニタリングをスケジュールする場合は、ベースラインの制約と統計を使用してリアルタイムのトラフィックと比較します。次のコードスニペットは、リアルタイムエンドポイントのモデルモニターをスケジュールするために使用する一般的な形式を示しています。この例では、モデルモニターを 1 時間ごとに実行するようにスケジュールしています。  

```
from sagemaker.model_monitor import CronExpressionGenerator
from time import gmtime, strftime

mon_schedule_name = 'my-model-monitor-schedule-' + strftime("%Y-%m-%d-%H-%M-%S", gmtime())
my_default_monitor.create_monitoring_schedule(
    monitor_schedule_name=mon_schedule_name,
    endpoint_input=EndpointInput(
        endpoint_name=endpoint_name,
        destination="/opt/ml/processing/input/endpoint"
    ),
    post_analytics_processor_script=s3_code_postprocessor_uri,
    output_s3_uri=s3_report_path,
    statistics=my_default_monitor.baseline_statistics(),
    constraints=my_default_monitor.suggested_constraints(),
    schedule_cron_expression=CronExpressionGenerator.hourly(),
    enable_cloudwatch_metrics=True,
)
```

**Example 1 回限りの分析のスケジュール**  
`create_monitoring_schedule` メソッドに次のような引数を渡すことで、分析を繰り返し実行せずに 1 回実行するようにスケジュールすることもできます。  

```
    schedule_cron_expression=CronExpressionGenerator.now(),
    data_analysis_start_time="-PT1H",
    data_analysis_end_time="-PT0H",
```
これらの引数では、`schedule_cron_expression` パラメータは値 `CronExpressionGenerator.now()` を使用して、分析が 1 回だけ即時に実行されるようにスケジュールします。この設定を使用するどのスケジュールでも、`data_analysis_start_time` と `data_analysis_end_time` のパラメータは必須です。これらのパラメータは、分析時間枠の開始時間と終了時間を設定します。これらの時間を現在の時刻を基準にしたオフセットとして定義し、ISO 8601 の期間形式を使用します。この例では、時間 `-PT1H` と `-PT0H` を定義し、過去 1 時間から現在の時刻までの時間枠を定義します。このスケジュールでは、分析は指定された時間枠に収集されたデータのみを評価します。

**Example バッチ変換ジョブのスケジュール**  
次のコードスニペットは、バッチ変換ジョブのモデルモニターをスケジュールするために使用する一般的な形式を示しています。  

```
from sagemaker.model_monitor import (
    CronExpressionGenerator,
    BatchTransformInput, 
    MonitoringDatasetFormat, 
)
from time import gmtime, strftime

mon_schedule_name = 'my-model-monitor-schedule-' + strftime("%Y-%m-%d-%H-%M-%S", gmtime())
my_default_monitor.create_monitoring_schedule(
    monitor_schedule_name=mon_schedule_name,
    batch_transform_input=BatchTransformInput(
        destination="opt/ml/processing/input",
        data_captured_destination_s3_uri=s3_capture_upload_path,
        dataset_format=MonitoringDatasetFormat.csv(header=False),
    ),
    post_analytics_processor_script=s3_code_postprocessor_uri,
    output_s3_uri=s3_report_path,
    statistics=my_default_monitor.baseline_statistics(),
    constraints=my_default_monitor.suggested_constraints(),
    schedule_cron_expression=CronExpressionGenerator.hourly(),
    enable_cloudwatch_metrics=True,
)
```

```
desc_schedule_result = my_default_monitor.describe_schedule()
print('Schedule status: {}'.format(desc_schedule_result['MonitoringScheduleStatus']))
```

# モニタリングスケジュールの cron 式
<a name="model-monitor-schedule-expression"></a>

モニタリングスケジュールの詳細を指定するには、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ScheduleConfig.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ScheduleConfig.html) を使用します。これは、モニタリングスケジュールに関する詳細を記述する `cron` 式です。

Amazon SageMaker Model Monitor では、次の `cron` 式をサポートしています。
+ ジョブを 1 時間おきに開始するように設定するには、以下を使用します。

  `Hourly: cron(0 * ? * * *)`
+ ジョブを毎日実行するには、以下を使用します。

  `cron(0 [00-23] ? * * *)`
+ ジョブを 1 回だけ即座に実行するには、次のキーワードを使用します。

  `NOW`

例えば、有効な `cron` 式は次のとおりです。
+ 毎日午後 12 時 (UTC): `cron(0 12 ? * * *)`
+ 毎日午前 12 時 (UTC): `cron(0 0 ? * * *)`

6、12 時間ごとの実行をサポートするために、Model Monitor では次の式をサポートしています。

`cron(0 [00-23]/[01-24] ? * * *)`

例えば、有効な `cron` 式は次のとおりです。
+ 12 時間ごと、午後 5 時 (UTC) から開始: `cron(0 17/12 ? * * *)`
+ 2 時間ごと、午前 12 時 (UTC) から開始: `cron(0 0/2 ? * * *)`

**注意事項**  
`cron` 式は午後 5 時 (UTC) に開始するように設定されていますが、実際にリクエストされた時間から実行までに 0 ～ 20 分の遅延が生じる可能性があります。
日次スケジュールで実行する場合、このパラメータを指定しないでください。毎日実行する時間は、SageMaker AI が選択します。
現時点では、SageMaker は 1～24 時間の整数での時間間隔のみをサポートしています。

# モニタリングスケジュールのサービスコントロールポリシーの設定
<a name="model-monitor-scp-rules"></a>

 モニタリングジョブのスケジュールを作成または更新するときに [CreateMonitoringSchedule](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateMonitoringSchedule.html) API または [UpdateMonitoringSchedule](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateMonitoringSchedule.html) API をそれぞれ使用して、モニタリングジョブのパラメータをそれぞれ指定する必要があります。ユースケースに応じて、次のいずれかの方法でこれを行えます。
+  `CreateMonitoringSchedule` または `UpdateMonitoringSchedule` を呼び出すときに、[MonitoringScheduleConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_MonitoringJobDefinition.html) の [MonitoringJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_MonitoringScheduleConfig.html) フィールドを指定できます。これを使用できるのは、データ品質モニタリングジョブのスケジュールを作成または更新する場合に限られます。
+  `CreateMonitoringSchedule` または `UpdateMonitoringSchedule` を呼び出すときに、`MonitoringScheduleConfig` の `MonitoringJobDefinitionName` フィールドに、作成済みのモニタリングジョブ定義の名前を指定できます。これは、次の API のいずれかを使用して作成するどのジョブ定義にも使用できます。
  +  [CreateDataQualityJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateDataQualityJobDefinition.html) 
  +  [CreateModelQualityJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelQualityJobDefinition.html) 
  +  [CreateModelBiasJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelBiasJobDefinition.html) 
  +  [CreateModelExplainabilityJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelExplainabilityJobDefinition.html) 

   SageMaker Python SDK を使用してスケジュールを作成または更新する場合は、このプロセスを使用する必要があります。

 前述のプロセスは相互に排他的です。つまり、モニタリングスケジュールを作成または更新するときに、`MonitoringJobDefinition` フィールドまたは `MonitoringJobDefinitionName` フィールドのいずれかを指定できます。

 モニタリングジョブ定義を作成するか、`MonitoringJobDefinition` フィールドでモニタリングジョブ定義を指定すると、`NetworkConfig` や `VolumeKmsKeyId` などのセキュリティパラメータを設定できます。管理者は、モニタリングジョブが常に安全な環境で実行されるように、これらのパラメータを常に特定の値に設定するとよいでしょう。そのためには、適切な[サービスコントロールポリシー](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html) (SCP) を設定します。SCP は、組織のアクセス許可の管理に使用できる組織ポリシーの一種です。

 以下の例は、モニタリングジョブのスケジュールを作成または更新する際にインフラストラクチャーパラメータが適切に設定されてようにするために使用できる SCP を示しています。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Deny",
            "Action": [
                "sagemaker:CreateDataQualityJobDefinition",
                "sagemaker:CreateModelBiasJobDefinition",
                "sagemaker:CreateModelExplainabilityJobDefinition",
                "sagemaker:CreateModelQualityJobDefinition"
            ],
            "Resource": "arn:*:sagemaker:*:*:*",
            "Condition": {
                "Null": {
                    "sagemaker:VolumeKmsKey":"true",
                    "sagemaker:VpcSubnets": "true",
                    "sagemaker:VpcSecurityGroupIds": "true"
                }
            }
        },
        {
            "Effect": "Deny",
            "Action": [
                "sagemaker:CreateDataQualityJobDefinition",
                "sagemaker:CreateModelBiasJobDefinition",
                "sagemaker:CreateModelExplainabilityJobDefinition",
                "sagemaker:CreateModelQualityJobDefinition"
            ],
            "Resource": "arn:*:sagemaker:*:*:*",
            "Condition": {
                "Bool": {
                    "sagemaker:InterContainerTrafficEncryption": "false"
                }
            }
        },
        {
            "Effect": "Deny",
            "Action": [
                "sagemaker:CreateMonitoringSchedule",
                "sagemaker:UpdateMonitoringSchedule"
            ],
            "Resource": "arn:*:sagemaker:*:*:monitoring-schedule/*",
            "Condition": {
                "Null": {
                    "sagemaker:ModelMonitorJobDefinitionName": "true"
                }
            }
        }
    ]
}
```

------

 この例の最初の 2 つのルールでは、モニタリングジョブ定義のセキュリティパラメータが常に設定されるようになります。最後のルールでは、組織内でスケジュールを作成または更新する人は誰でも、必ず `MonitoringJobDefinitionName` フィールドを指定する必要があります。これにより、組織内の誰もスケジュールの作成または更新時に、`MonitoringJobDefinition` フィールドを指定してセキュリティパラメータに安全でない値を設定できなくなります。