本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在资产的传感器上启用异常检测
创建计算模型 ()Amazon CLI
要创建计算模型,请使用 Amazon 命令行界面 (Amazon CLI)。定义计算模型后,训练模型并安排推理以对中的资产进行异常检测。 Amazon IoT SiteWise
-
创建包含以下内容
anomaly-detection-computation-model-payload.json的文件:{ "computationModelName": "anomaly-detection-computation-model-name", "computationModelConfiguration": { "anomalyDetection": { "inputProperties": "${input_properties}", "resultProperty": "${result_property}" } }, "computationModelDataBinding": { "input_properties": { "list": [{ "assetModelProperty": { "assetModelId": "asset-model-id", "propertyId": "input-property-id-1" } }, { "assetModelProperty": { "assetModelId": "asset-model-id", "propertyId": "input-property-id-2" } } ] }, "result_property": { "assetModelProperty": { "assetModelId": "asset-model-id", "propertyId": "results-property-id" } } } } -
运行以下命令创建计算模型:
aws iotsitewise create-computation-model \ --cli-input-jsonfile://anomaly-detection-computation-model-payload.json
ExecuteAction API 有效负载准备
执行训练和推理的后续步骤是使用 ExecuteActionAPI 执行的。训练和推理均使用 JSON 操作负载配置进行配置。调用 ExecuteActionAPI 时,必须将操作负载作为带有效stringValue载荷的值提供。
有效载荷必须严格遵守 API 要求。具体而言,该值必须是没有控制字符(例如,换行符、制表符或回车符)的扁平字符串。
以下选项提供了两种可靠的方式来提供有效的动作有效载荷:
选项 1:使用干净的有效载荷文件
以下过程描述了清理有效载荷文件的步骤:
-
清理文件以删除控制字符。
tr -d '\n\r\t' < original-action-payload.json > training-or-inference-action-payload.json -
使用文件执行操作
@=file://...。aws iotsitewise execute-action \ --target-resource computationModelId=<MODEL_ID> \ --action-definition-id <ACTION_DEFINITION_ID> \ --resolve-to assetId=<ASSET_ID> \ --action-payload stringValue@=file://training-or-inference-action-payload.json
选项 2:带转义引号的内联字符串
以下步骤描述了以内联方式提供有效负载并避免使用中间文件的步骤:
-
在 JSON 字符串中使用转义的双引号 (
\")。 -
将整个
StringValue=..表达式用双引号包起来。
例 逃脱的动作有效载荷:
aws iotsitewise execute-action \ --target-resource computationModelId=<MODEL_ID> \ --action-definition-id <ACTION_DEFINITION_ID> \ --resolve-to assetId=<ASSET_ID> \ --action-payload "stringValue={\"exportDataStartTime\":1717225200,\"exportDataEndTime\":1722789360,\"targetSamplingRate\":\"PT1M\"}"
训练模型 (Amazon CLI)
创建计算模型后,您可以根据资产训练模型。按照以下步骤为资产训练模型:
-
运行以下命令以查找
Amazon/ANOMALY_DETECTION_TRAINING操作的actionDefinitionId。computation-model-id替换为上一步中返回的 ID。aws iotsitewise describe-computation-model \ --computation-model-idcomputation-model-id -
创建一个名为的文件
anomaly-detection-training-payload.json并添加以下值:注意
有效载荷必须符合选项 1:使用干净的有效载荷文件。
-
StartTime以训练数据的开头为单位,以纪元秒为单位提供。 -
EndTime最后是训练数据,以纪元秒为单位提供。 -
您可以选择进行配置高级训练配置,以提高模型性能。
-
(可选)
TargetSamplingRate使用数据的采样率。 -
(可选)
LabelInputConfiguration指定发生异常行为的时间段,以改进模型训练。 -
(可选)
ModelEvaluationConfiguration通过在训练完成后的指定时间范围内运行推理来评估模型性能。 -
(可选)
ModelMetricsDestination用于收集全面的性能数据(精度、召回率、曲线下方区域)。
-
{ "trainingMode": "TRAIN_MODEL", "exportDataStartTime": StartTime, "exportDataEndTime": EndTime }例 训练有效载荷示例:
{ "trainingMode": "TRAIN_MODEL", "exportDataStartTime": 1717225200, "exportDataEndTime": 1722789360 } -
-
运行以下命令开始训练。在命令中替换以下参数:
-
computation-model-id带有目标计算模型的 ID。 -
asset-id其中包含你要训练模型的资产的 ID。 -
training-action-definition-id使用步骤 1 中Amazon/ANOMALY_DETECTION_TRAINING操作的 ID。
aws iotsitewise execute-action \ --target-resource computationModelId=computation-model-id\ --resolve-to assetId=asset-id\ --action-definition-idtraining-action-definition-id\ --action-payload stringValue@=file://anomaly-detection-training-payload.json例 执行动作的:
aws iotsitewise execute-action --target-resource computationModelId=27cb824c-fd84-45b0-946b-0a5b0466d890 --resolve-to assetId=cefd4b68-481b-4735-b466-6a4220cd19ee --action-definition-id e54cea94-5d1c-4230-a59e-4f54dcbc972d --action-payload stringValue@=file://anomaly-detection-training-payload.json -
-
运行以下命令以检查模型训练过程的状态。最新的执行摘要显示执行状态 (
RUNNING/COMPLETED/FAILED)。aws iotsitewise list-executions \ --target-resource-type COMPUTATION_MODEL \ --target-resource-idcomputation-model-id\ --resolve-to-resource-type ASSET \ --resolve-to-resource-idasset-id -
运行以下命令以检查最新训练模型的配置。仅当至少一个模型成功训练后,此命令才会生成输出。
aws iotsitewise describe-computation-model-execution-summary \ --computation-model-idcomputation-model-id\ --resolve-to-resource-type ASSET \ --resolve-to-resource-idasset-id -
使用
ComputationModel时 AssetModelProperty,使用 ListComputationModelResolveToResourcesAPI 识别已执行操作的资产。aws iotsitewise list-computation-model-resolve-to-resources \ --computation-model-idcomputation-model-id
开始和停止重新训练模型 ()Amazon CLI
在初始模型训练后,您可以配置自动重新训练以解决数据漂移问题并随着时间的推移保持模型的准确性。再训练调度器允许您使用可配置的升级模式来设置定期模型更新。
开始重新训练调度程序
-
运行以下命令以查找
Amazon/ANOMALY_DETECTION_TRAINING操作的actionDefinitionId。computation-model-id替换为计算模型创建时返回的 ID。aws iotsitewise describe-computation-model \ --computation-model-idcomputation-model-id -
创建一个名为的文件
anomaly-detection-start-retraining-payload.json并添加以下代码。将参数替换为所述的值。注意
有效载荷必须符合选项 1:使用干净的有效载荷文件。
-
lookbackWindow带有用于再训练的历史数据窗口 (P180D//P360DP540D/P720D)。 -
retrainingFrequency以及重新训练模型的频率(最小值P30D、最大值P1Y)。 -
(可选)
promotion使用模型升级模式(SERVICE_MANAGED或CUSTOMER_MANAGED)。默认值为SERVICE_MANAGED。 -
(可选)
retrainingStartDate以重新训练时间表的开始日期为单位,以纪元秒为单位。将时间截断到最近的 UTC 日期。可选,默认为当前日期。 -
您可以选择配置高级训练配置以提高模型性能。
-
(可选)
ModelMetricsDestination以获取全面的性能数据(精度、召回率、曲线下方区域)。
-
{ "trainingMode": "START_RETRAINING_SCHEDULER", "retrainingConfiguration": { "lookbackWindow": "P180D", "promotion": "SERVICE_MANAGED", "retrainingFrequency": "P30D", "retrainingStartDate": "StartDate" } } -
-
运行以下命令启动重新训练调度程序。在命令中替换以下参数:
-
computation-model-id带有目标计算模型的 ID。 -
asset-id其中包含你要训练模型的资产的 ID。 -
training-action-definition-id使用步骤 1 中Amazon/ANOMALY_DETECTION_TRAINING操作的 ID。
aws iotsitewise execute-action \ --target-resource computationModelId=computation-model-id\ --resolve-to assetId=asset-id\ --action-definition-idtraining-action-definition-id\ --action-payload stringValue@=file://anomaly-detection-start-retraining-payload.json例 执行操作命令示例
aws iotsitewise execute-action --target-resource computationModelId=27cb824c-fd84-45b0-946b-0a5b0466d890 --resolve-to assetId=cefd4b68-481b-4735-b466-6a4220cd19ee --action-definition-id e54cea94-5d1c-4230-a59e-4f54dcbc972d --action-payload stringValue@=file://anomaly-detection-start-retraining-payload.json -
停止重新训练调度程序
-
运行以下命令以查找
Amazon/ANOMALY_DETECTION_TRAINING操作的actionDefinitionId。computation-model-id替换为之前创建的计算模型的实际 ID。aws iotsitewise describe-computation-model \ --computation-model-idcomputation-model-id -
创建文件
anomaly-detection-stop-retraining-payload.json并添加以下内容:注意
有效载荷必须符合选项 1:使用干净的有效载荷文件。
{ "trainingMode": "STOP_RETRAINING_SCHEDULER" } -
运行以下命令以停止重新训练调度程序。在命令中替换以下参数:
-
computation-model-id带有目标计算模型的 ID。 -
asset-id其中包含你要训练模型的资产的 ID。 -
training-action-definition-id使用步骤 1 中Amazon/ANOMALY_DETECTION_TRAINING操作的 ID。
aws iotsitewise execute-action \ --target-resource computationModelId=computation-model-id\ --resolve-to assetId=asset-id\ --action-definition-idtraining-action-definition-id\ --action-payload stringValue@=file://anomaly-detection-stop-retraining-payload.json -
开始和停止推理 ()Amazon CLI
训练完模型后,开始推理。这指示 Amazon IoT SiteWise 您主动监控您的工业资产是否存在异常情况。
开始推理
-
运行以下命令以查找
Amazon/ANOMALY_DETECTION_INFERENCE操作的actionDefinitionId。computation-model-id替换为之前创建的计算模型的实际 ID。aws iotsitewise describe-computation-model \ --computation-model-idcomputation-model-id -
创建文件
anomaly-detection-start-inference-payload.json并添加以下值:注意
有效载荷必须符合选项 1:使用干净的有效载荷文件。
"inferenceMode": "START", "dataUploadFrequency": "DataUploadFrequency"-
DataUploadFrequency:配置推理计划的运行频率,以执行异常检测。支持的值为:PT5M, PT10M, PT15M, PT30M, PT1H, PT2H..PT12H, PT1D。 -
(可选)
DataDelayOffsetInMinutes延迟偏移量以分钟为单位。将此值设置在 0 到 60 分钟之间。 -
(可选)
TargetModelVersion使用要激活的模型版本。 -
(可选)
weeklyOperatingWindow使用轮班配置进行配置。 -
您可以选择配置高级推理配置。
-
-
运行以下命令开始推理。替换负载文件中的以下参数。
-
computation-model-id带有目标计算模型的 ID。 -
asset-id其中包含训练模型所依据的资产的 ID。 -
inference-action-definition-id使用步骤 1 中Amazon/ANOMALY_DETECTION_INFERENCE操作的 ID。
aws iotsitewise execute-action \ --target-resource computationModelId=computation-model-id\ --resolve-to assetId=asset-id\ --action-definition-idinference-action-definition-id\ --action-payload stringValue@=file://anomaly-detection-inference-payload.json -
-
运行以下命令以检查推理是否仍在运行。当推理处于活动状态
TRUE时,该inferenceTimerActive字段设置为。aws iotsitewise describe-computation-model-execution-summary \ --computation-model-idcomputation-model-id\ --resolve-to-resource-type ASSET \ --resolve-to-resource-idasset-id -
以下命令列出了所有的推理执行:
aws iotsitewise list-executions \ --target-resource-type COMPUTATION_MODEL \ --target-resource-idcomputation-model-id\ --resolve-to-resource-type ASSET \ --resolve-to-resource-idasset-id -
运行以下命令来描述单个执行。
execution-id替换为先前步骤 5 中的 ID。aws iotsitewise describe-execution \ --execution-idexecution-id
停止推理
-
运行以下命令以查找
Amazon/ANOMALY_DETECTION_INFERENCE操作的actionDefinitionId。computation-model-id替换为之前创建的计算模型的实际 ID。aws iotsitewise describe-computation-model \ --computation-model-idcomputation-model-id -
创建文件
anomaly-detection-stop-inference-payload.json并添加以下代码。{ "inferenceMode": "STOP" }注意
有效载荷必须符合选项 1:使用干净的有效载荷文件。
-
运行以下命令停止推理。替换负载文件中的以下参数:
-
computation-model-id带有目标计算模型的 ID。 -
asset-id其中包含训练模型所依据的资产的 ID。 -
inference-action-definition-id使用步骤 1 中Amazon/ANOMALY_DETECTION_INFERENCE操作的 ID。
例 停止推理命令中的:
aws iotsitewise execute-action \ --target-resource computationModelId=computation-model-id\ --resolve-to assetId=asset-id\ --action-definition-idinference-action-definition-id\ --action-payload stringValue@=file://anomaly-detection-stop-inference-payload.json -
查找在数据绑定中使用给定资源的计算模型
要列出绑定到给定资源的计算模型,请执行以下操作:
-
资产模型(获取绑定了该资产模型的任何属性的所有计算模型)。
-
资产(获取绑定该资产任何属性的所有计算模型)
-
资产模型属性(获取绑定此属性的所有计算模型)
-
asset 属性(获取绑定此属性的所有计算模型。 这可能是为了提供信息,或者当用户尝试将此属性绑定到另一个计算模型但它已经绑定到其他地方时需要这样做)
使用 ListComputationModelDataBindingUsagesAPI 获取以资产(属性)或资产模型(属性)作为数据绑定的列表。ComputationModelId
request.json使用以下信息准备:
{ "dataBindingValueFilter": { "asset": { "assetId": "<string>" } // OR "assetModel": { "assetModelId": "<string>" } // OR "assetProperty": { "assetId": "<string>", "propertyId": "<string>" } // OR "assetModelProperty": { "assetModelId": "<string>", "propertyId": "<string>" } }, "nextToken": "<string>", "maxResults": "<number>" }
使用list-computation-model-data-binding-usages命令检索以资产或资产模型作为数据绑定的模型。
aws iotsitewise list-computation-model-data-binding-usages \ --cli-input-json file://request.json