

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 计划监控作业
<a name="model-monitor-scheduling"></a>

Amazon SageMaker 模型监控器使您能够监控从您的实时终端节点收集的数据。您可以按照定期计划监控数据，也可以立即进行一次性监控。您可以使用 [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-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 定期分析计划**  
如果要为实时端点计划模型监控，则使用基准约束和统计数据与实时流量进行比较。以下代码片段显示了用于为实时端点计划模型监控的一般格式。此示例将 Model Monitor 计划为每小时运行一次。  

```
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 一次性分析计划**  
您还可以通过向 `create_monitoring_schedule` 方法传递如下参数，将分析计划为运行一次而不重复运行：  

```
    schedule_cron_expression=CronExpressionGenerator.now(),
    data_analysis_start_time="-PT1H",
    data_analysis_end_time="-PT0H",
```
在这些参数中，`schedule_cron_expression` 参数将分析计划为立即运行一次，其值为 `CronExpressionGenerator.now()`。对于任何具有此设置的计划，都必须使用 `data_analysis_start_time` 和 `data_analysis_end_time` 参数。这些参数可设置分析时段的开始时间和结束时间。将这些时间定义为相对于当前时间的偏移量，并使用 ISO 8601 持续时间格式。在此示例中，时间 `-PT1H` 和 `-PT0H` 定义了过去一小时和当前时间之间的时段。根据此计划，分析只对指定时段内收集的数据进行求值。

**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']))
```