ModelDataCollector 类

定义可用于在 Azure 机器学习 AKS WebService 部署到 Blob 存储中的数据的模型数据收集器。

通过 ModelDataCollector 类,可以在 Azure 机器学习 AKS 部署中为模型定义数据收集器。 数据收集器对象可用于将模型数据(例如输入和预测)收集到工作区的 Blob 存储。 在部署中启用模型数据收集后,收集的数据将显示在以下容器路径中,如 csv 文件:/modeldata/{workspace_name}/{webservice_name}/{model_name}/{model_version}/{指定}/{year}/{month}/{day}/{collection_name}.csv

ModelDataCollector 构造函数。

启用模型数据收集后,数据将发送到以下容器路径:/modeldata/{workspace}/{webservice_name}/{model_name}/{model_version}/{指定}/{year}/{month}/{day}/{collection_name}.csv

构造函数

ModelDataCollector(model_name, designation='default', feature_names=None, workspace='default/default/default', webservice_name='default', model_version='default', collection_name='default')

参数

名称 说明
model_name
必需
str

要为其收集数据的模型的名称。

designation
str

数据收集位置的唯一指定。 支持的指定为“inputs”、“predictions”、“labels”、“signals 和”general”。

默认值: default
feature_names

提供时成为 csv 标头的功能名称列表。

默认值: None
workspace
str

Azure 机器学习工作区的标识符,格式为 {subscription_id}/{resource_group}/{workspace_name}。 当模型通过 Azure 机器学习作时,会自动填充此值。

默认值: default/default/default
webservice_name
str

此模型当前部署到的 Web 服务的名称。 当模型通过 Azure 机器学习作时,会自动填充此值。

默认值: default
model_version
str

模型的版本。 当模型通过 Azure 机器学习作时,会自动填充此值。

默认值: default
collection_name
str

ModelDataCollector 收集数据的文件的名称。 此参数仅用于“信号”和“常规”指定。 对于其他类型的指定,指定名称用作文件名。

默认值: default

注解

目前,ModelDataCollector 仅适用于 Azure 机器学习 AKS 部署。 若要在部署中收集模型数据,需要执行以下步骤:

  • 更新映像entry_script以添加 ModelDataCollector 对象和 collect 语句(s)。 可以在脚本中定义多个 ModelDataCollector 对象,例如一个用于输入,一个用于预测同一模型。 有关如何定义和使用entry_script的更多详细信息,请参阅以下类: InferenceConfig

  • 在 AKS 模型部署步骤中设置enable_data_collection标志。 部署模型后,可以使用此标志打开/关闭模型数据收集,而无需修改entry_script。 有关如何配置模型部署的更多详细信息,请参阅以下类: AksWebservice

以下代码片段演示如何使用 ModelDataCollection entry_script:


   from azureml.monitoring import ModelDataCollector

   def init():
       global inputs_dc

       # Define your models and other scoring related objects
       # ...

       # Define input data collector to model "bestmodel". You need to define one object per model and
       # designation. For the sake of simplicity, we are only defining one object here.
       inputs_dc = ModelDataCollector(model_name="bestmodel", designation="inputs", feature_names=["f1", "f2"])

   def run(raw_data):
       global inputs_dc

       # Convert raw_data to proper format and run prediction
       # ...

       # Use inputs_dc to collect data. For any data that you want to collect, you need to call collect method
       # on respective ModelDataCollector objects. For the sake of simplicity, we are only working on a single
       # object.
       inputs_dc.collect(input_data)

上面的示例说明了有关 ModelDataCollector 的几个事项。 首先,每个模型和每个指定定义一个对象,在本例中为“bestmodel”和“inputs”。 其次,ModelDataCollector 需要表格数据作为输入,并将数据保留为 csv 文件。 可以提供可选功能名称来设置这些 csv 文件的标头。

以下代码片段演示如何在模型部署期间启用 ModelDataCollector:


   webservice_config = AksWebservice.deploy_configuration(collect_model_data=True)
   Model.deploy(ws, "myservice", [model], inference_config, webservice_config, aks_cluster)

部署 Azure 机器学习 AKS WebService 并在服务上运行评分后,收集的数据将显示在工作区的存储帐户中。 ModelDataCollector 将分区数据,以便轻松访问和使用。 所有数据都将在“modeldata”存储容器下收集。 下面是分区格式:

/modeldata/{workspace_name}/{webservice_name}/{model_name}/{model_version}/{指定}/{year}/{month}/{day}/{collection_name}.csv

请注意,文件名中的collection_name将只考虑用于“信号”和“常规”指定。 对于“inputs”、“predictions”和“labels”文件名,将设置为 {name}.csv。

方法

add_correlations

帮助程序函数,用于向给定的输入数据添加相关标头和值。

collect

将数据收集到存储。

add_correlations

帮助程序函数,用于向给定的输入数据添加相关标头和值。

add_correlations(input_data, correlations)

参数

名称 说明
input_data
必需

要向其添加相关标头和值的数据。

correlations
必需

从 collect() 函数返回的相关标头和值。

返回

类型 说明

input_data添加了相关标头和值。

注解

调用后 collect ,它将返回一组相关标头和值。 其中包括由 ModelDataCollector 生成或作为参数提供的唯一关联 ID,例如请求 ID、时间戳和唯一关联 ID。 这些值可用于稍后分析和关联不同类型的数据。 以下示例演示如何向输入数据和预测数据添加相关性。 请注意,“输入”指定类型默认具有相关数据。


   # Define inputs_dc and predictions_dc for the same model and "inputs" and "predictions" designations
   # respectively
   # ...

   correlations = inputs_dc.collect(input_data)
   predictions_data = predictions_dc.add_correlations(predictions_data, correlations)
   predictions_dc.collect(predictions_data)

collect

将数据收集到存储。

collect(input_data, user_correlation_id='')

参数

名称 说明
input_data
必需

要收集的数据。 对于数据帧类型,如果标头存在特征名称,则此信息包含在数据目标中,而无需在 ModelDataCollector 构造函数中显式传递功能名称。

user_correlation_id
必需
str

可选的关联 ID 用于稍后关联此数据。

返回

类型 说明

包含相关标头和值的字典。

属性

AML_DC_BOUNDARY_HEADER

AML_DC_BOUNDARY_HEADER = '$aml_dc_boundary'

AML_DC_CORRELATION_HEADER

AML_DC_CORRELATION_HEADER = '$aml_dc_correlation_id'

AML_DC_SCORING_TIMESTAMP_HEADER

AML_DC_SCORING_TIMESTAMP_HEADER = '$aml_dc_scoring_timestamp'

AML_MODEL_NAME_HEADER

AML_MODEL_NAME_HEADER = '$aml_model_name'

AML_MODEL_VERSION_HEADER

AML_MODEL_VERSION_HEADER = '$aml_model_version'

AML_REQUEST_ID_HEADER

AML_REQUEST_ID_HEADER = '$aml_request_id'

AML_SERVICE_NAME_HEADER

AML_SERVICE_NAME_HEADER = '$aml_service_name'

AML_WORKSPACE_HEADER

AML_WORKSPACE_HEADER = '$aml_workspace'

dllpath

dllpath = 'C:\\hostedtoolcache\\windows\\Python\\3.9.13\\x64\\lib\\site-packages\\azureml\\monitoring\\tools\\modeldatacollector\\lib\\native\\Windows'