

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

# 在所有资产的传感器上启用异常检测
<a name="anom-detection-sensors-across-asset"></a>

## 创建计算模型 ()AWS CLI
<a name="create-computation-model-across-assets"></a>

要创建计算模型，请使用 AWS 命令行界面 (AWS CLI)。定义计算模型后，训练模型并安排推理，以便对中的资产进行异常检测。 AWS IoT SiteWise

以下步骤说明了此过程：

1. 要设置异常检测，请使用 [UpdateAssetModel(AWS CLI)](https://docs.aws.amazon.com/cli/latest/reference/iotsitewise/update-asset-model.html)，并满足以下要求：

   1. 至少一个`INTEGER`数据类型为`DOUBLE`或的输入属性。它要么是测量属性，要么是变换属性，用于训练模型。

   1. `STRING`数据类型的结果属性。它必须是测量属性，并存储异常检测结果。

1. 创建包含以下内容`anomaly-detection-computation-model-payload.json`的文件：
**注意**  
通过直接提供`assetProperty`作为数据源来创建计算模型。

   ```
   {
       "computationModelName": "name of ComputationModel",
       "computationModelConfiguration": {
           "anomalyDetection": {
               "inputProperties": "${properties}",
               "resultProperty": "${p3}"
           }
       },
       "computationModelDataBinding": {
           "properties": {
               "list": [
                   {
                       "assetProperty": { 
                           "assetId": "asset-id",
                           "propertyId": "input-property-id-1"
                       }
                   },
                   {
                       "assetProperty": { 
                           "assetId": "asset-id",
                           "propertyId": "input-property-id-2"
                       }
                   }
               ]
           },
           "p3": {
               "assetProperty": { 
                   "assetId": "asset-id",
                   "propertyId": "results-property-id"
               }
           }
       }
   }
   ```

1. 运行以下命令创建计算模型：

   ```
   aws iotsitewise create-computation-model \
       --cli-input-json file://anomaly-detection-computation-model-payload.json
   ```

## ExecuteAction API 有效负载准备
<a name="create-action-payload-across-assets"></a>

 执行训练和推理的后续步骤是使用 [ExecuteAction](https://docs.aws.amazon.com/iot-sitewise/latest/APIReference/API_ExecuteAction.html)API 执行的。训练和推理均使用 JSON 操作负载配置进行配置。调用 [ExecuteAction](https://docs.aws.amazon.com/iot-sitewise/latest/APIReference/API_ExecuteAction.html)API 时，必须将操作负载作为带有效`stringValue`载荷的值提供。

 有效载荷必须严格遵守 API 要求。具体而言，该值必须是没有**控制字符（例如，换行符**、制表符或回车符）的**扁平字符串**。以下选项提供了两种可靠的方式来提供有效的动作有效载荷。

### 选项 1：使用干净的有效载荷文件
<a name="clean-payload-file-across-assets"></a>

以下过程描述了清理有效载荷文件的步骤：

1. 清理文件以删除控制字符。

   ```
   tr -d '\n\r\t' < original-action-payload.json > training-or-inference-action-payload.json
   ```

1. 使用文件执行操作`@=file://...`。

   ```
   aws iotsitewise execute-action \
       --target-resource computationModelId=<MODEL_ID> \
       --action-definition-id <ACTION_DEFINITION_ID> \
       --action-payload stringValue@=file://training-or-inference-action-payload.json
   ```

### 选项 2：带转义引号的内联字符串
<a name="inline-string-quotes-across-assets"></a>

以下步骤描述了以内联方式提供有效负载和避免中间文件的步骤：
+ 在 JSON 字符串中使用转义的双引号 (`\"`)。
+ 将整个`StringValue=..`表达式用双引号包起来。

**Example 逃脱的动作有效载荷：**  

```
aws iotsitewise execute-action \
    --target-resource computationModelId=<MODEL_ID> \
    --action-definition-id <ACTION_DEFINITION_ID> \
    --action-payload "stringValue={\"exportDataStartTime\":1717225200,\"exportDataEndTime\":1722789360,\"targetSamplingRate\":\"PT1M\"}"
```

## 训练模型 (AWS CLI)
<a name="start-training-cli-across-assets"></a>

1. 运行以下命令以查找 `AWS/ANOMALY_DETECTION_TRAINING` 操作的 `actionDefinitionId`。`computation-model-id`替换为上一步中返回的 ID。

   ```
   aws iotsitewise describe-computation-model \
       --computation-model-id computation-model-id
   ```

1. 创建一个名为的文件`anomaly-detection-training-payload.json`并添加以下值：
**注意**  
 有效载荷必须符合[选项 1：使用干净的有效载荷文件](#clean-payload-file-across-assets)。

   1. `StartTime`以训练数据的开头为单位，以纪元秒为单位提供。

   1. `EndTime`最后是训练数据，以纪元秒为单位提供。

   1. 您可以选择配置[高级推理配置](advanced-inference-configurations.md)。

      1. （可选）`TargetSamplingRate`使用数据的采样率。

      1. （可选）`LabelInputConfiguration`指定发生异常行为的时间段，以改进模型训练。

      1. （可选）`ModelEvaluationConfiguration`通过在训练完成后的指定时间范围内运行推理来评估模型性能。

   ```
   {
     "exportDataStartTime": StartTime,
     "exportDataEndTime": EndTime
   }
   ```  
**Example 训练有效载荷示例：**  

   ```
   {
     "exportDataStartTime": 1717225200,
     "exportDataEndTime": 1722789360
   }
   ```

1. 运行以下命令开始训练（不提供资产作为目标资源）。在命令中替换以下参数：

   ```
   aws iotsitewise execute-action \
       --target-resource computationModelId=computation-model-id \
       --action-definition-id training-action-definition-id \
       --action-payload stringValue@=file://anomaly-detection-training-payload.json
   ```

1. 运行以下命令以检查模型训练过程的状态。最新的执行摘要显示执行状态 (`RUNNING`/`COMPLETED`/`FAILED`)。

   ```
   aws iotsitewise list-executions \
       --target-resource-type COMPUTATION_MODEL \
       --target-resource-id computation-model-id
   ```

1. 运行以下命令以检查最新训练模型的配置。只有当至少一个模型成功完成训练时，此命令才会生成输出。

   ```
   aws iotsitewise describe-computation-model-execution-summary \
       --computation-model-id computation-model-id
   ```

## 开始和停止重新训练模型 ()AWS CLI
<a name="start-stop-retraining-cli-model"></a>

 在初始模型训练后，您可以配置自动重新训练以解决数据漂移问题并随着时间的推移保持模型的准确性。再训练调度器允许您使用可配置的升级模式来设置定期模型更新。

### 开始重新训练调度程序
<a name="start-retraining-scheduler-model"></a>

1. 准备与中提到的相同的有效载荷[开始重新训练调度程序](anom-detection-sensors-asset.md#start-retraining-scheduler)。

1. 执行训练操作（不提供资产作为目标资源）。在命令中替换以下参数：

   1. `computation-model-id`带有目标计算模型的 ID。

   1. `training-action-definition-id`带有`AWS/ANOMALY_DETECTION_TRAINING`操作的 ID。

   ```
   aws iotsitewise execute-action \
       --target-resource computationModelId=computation-model-id \
       --action-definition-id training-action-definition-id \
       --action-payload stringValue@=file://anomaly-detection-start-retraining-payload.json
   ```

1. 运行以下命令以检查启动重新训练调度程序进程的状态。最新的执行摘要显示执行状态 (`RUNNING`/`COMPLETED`/`FAILED`)。

   ```
   aws iotsitewise list-executions \
       --target-resource-type COMPUTATION_MODEL \
       --target-resource-id computation-model-id
   ```

### 停止重新训练调度程序
<a name="stop-retraining-scheduler-model"></a>

1. 准备与中提到的相同的有效载荷[停止重新训练调度程序](anom-detection-sensors-asset.md#stop-retraining-scheduler)。

1. 执行训练操作（不提供资产作为目标资源）。在命令中替换以下参数：

   1. `computation-model-id`带有目标计算模型的 ID。

   1. `training-action-definition-id`带有`AWS/ANOMALY_DETECTION_TRAINING`操作的 ID。

   ```
   aws iotsitewise execute-action \
       --target-resource computationModelId=computation-model-id \
       --action-definition-id training-action-definition-id \
       --action-payload stringValue@=file://anomaly-detection-stop-retraining-payload.json
   ```

1. 运行以下命令以检查停止重新训练调度程序进程的状态。最新的执行摘要显示执行状态 (`RUNNING`/`COMPLETED`/`FAILED`)。

   ```
   aws iotsitewise list-executions \
       --target-resource-type COMPUTATION_MODEL \
       --target-resource-id computation-model-id
   ```

## 开始和停止推理 ()AWS CLI
<a name="start-stop-inference-across-assets"></a>

训练完模型后，开始推理，这会指示 AWS IoT SiteWise 您开始监控您的工业资产是否存在异常。

### 开始推理
<a name="start-inference-across-assets"></a>

1. 运行以下命令以查找 `AWS/ANOMALY_DETECTION_INFERENCE` 操作的 `actionDefinitionId`。`computation-model-id`替换为之前创建的计算模型的实际 ID。

   ```
   aws iotsitewise describe-computation-model \
       --computation-model-id computation-model-id
   ```

1. 创建文件`anomaly-detection-start-inference-payload.json`并添加以下代码。按所述替换以下参数：
**注意**  
 有效载荷必须符合[选项 1：使用干净的有效载荷文件](#clean-payload-file-across-assets)。

   1. `DataUploadFrequency`：配置推理计划运行频率以执行异常检测。支持的值为：`PT5M, PT10M, PT15M, PT30M, PT1H, PT2H..PT12H, PT1D`。

      ```
      "inferenceMode": "START",
      "dataUploadFrequency": "DataUploadFrequency"
      ```

   1. （可选）`DataDelayOffsetInMinutes`延迟偏移量以分钟为单位。将此值设置在 0 到 60 分钟之间。

   1. （可选）`TargetModelVersion`使用要激活的模型版本。

   1. （可选）`weeklyOperatingWindow`使用轮班配置进行配置。

   1. 您可以选择配置[高级推理配置](advanced-inference-configurations.md)。

      1. [高频推理（5 分钟 — 1 小时）](advanced-inference-configurations.md#high-frequency-inferencing).

      1. [低频推理（2 小时 — 1 天）](advanced-inference-configurations.md#low-frequency-inferencing).

      1. [灵活的日程安排](advanced-inference-configurations.md#flexible-scheduling).

1. 运行以下命令开始推理。替换负载文件中的以下参数。

   1. `computation-model-id`带有目标计算模型的 ID。

   1. `inference-action-definition-id`使用步骤 1 中`AWS/ANOMALY_DETECTION_INFERENCE`操作的 ID。

   ```
   aws iotsitewise execute-action \
       --target-resource computationModelId=computation-model-id \
       --action-definition-id inference-action-definition-id \
       --action-payload stringValue@=file://anomaly-detection-inference-payload.json
   ```

1. 运行以下命令以检查推理是否仍在运行。当推理处于活动状态`TRUE`时，该`inferenceTimerActive`字段设置为。

   ```
   aws iotsitewise describe-computation-model-execution-summary \
       --computation-model-id computation-model-id
   ```

1. 以下命令列出了所有的推理执行：

   ```
   aws iotsitewise list-executions \
       --target-resource-type COMPUTATION_MODEL \
       --target-resource-id computation-model-id
   ```

1. 运行以下命令来描述单个执行。`execution-id`替换为先前步骤 5 中的 ID。

   ```
   aws iotsitewise describe-execution \
       --execution-id execution-id
   ```

### 停止推理
<a name="stop-inference-across-assets"></a>

1. 运行以下命令以查找 `AWS/ANOMALY_DETECTION_INFERENCE` 操作的 `actionDefinitionId`。`computation-model-id`替换为之前创建的计算模型的实际 ID。

   ```
   aws iotsitewise describe-computation-model \
       --computation-model-id computation-model-id
   ```

1. 创建文件`anomaly-detection-stop-inference-payload.json`并添加以下代码。

   ```
   {
       "inferenceMode": "STOP"
   }
   ```
**注意**  
 有效载荷必须符合[选项 1：使用干净的有效载荷文件](anom-detection-sensors-asset.md#clean-payload-file)。

1. 运行以下命令停止推理。替换负载文件中的以下参数：

   1. `computation-model-id`带有目标计算模型的 ID。

   1. `inference-action-definition-id`使用步骤 1 中`AWS/ANOMALY_DETECTION_INFERENCE`操作的 ID。  
**Example 停止推理命令的：**  

   ```
   aws iotsitewise execute-action \
   --target-resource computationModelId=computation-model-id \
   --action-definition-id inference-action-definition-id \
   --action-payload stringValue@=file://anomaly-detection-stop-inference-payload.json
   ```