Azure ML Studio测试接口请求报错:source_words样本数不足排查
问题描述
在使用Azure ML Studio测试接口时,平台提供的POST请求payload模板如下:
{ "input_data": { "columns": [ "content_type", "project_name", "source_words", "source_text", "target_locale", "evaluated_phase", "evaluated_translation", "evaluated_vendor", "lqe_phase" ], "index": [], "data": [] } }
按如下方式填充后:
{ "input_data": { "columns": [ "content_type", "project_name", "source_words", "source_text", "target_locale", "evaluated_phase", "evaluated_translation", "evaluated_vendor", "lqe_phase" ], "index": [], "data": [ { "content_type": "some text", "project_name": "some text", "source_words": 1267, "source_text": "some text", "target_locale": "text", "evaluated_phase": "more text", "evaluated_translation": "text text", "evaluated_vendor": "text", "lqe_phase": "final text" } ] } }
其中仅source_words为整数类型,其余均为文本类型,但持续收到错误:InnerException: ValueError: ['source_words']: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required.
2023-10-05更新:部署多个其他模型后,均出现相同错误。
可能的原因及解决办法
数据格式不匹配:多数Azure ML部署的模型(尤其是基于Scikit-learn、XGBoost等框架训练的)要求
data字段为数组的数组,而非键值对对象数组。当前使用的对象格式无法被模型正确解析,导致source_words字段被识别为0样本。
修正后的payload格式如下:{ "input_data": { "columns": [ "content_type", "project_name", "source_words", "source_text", "target_locale", "evaluated_phase", "evaluated_translation", "evaluated_vendor", "lqe_phase" ], "index": [], "data": [ ["some text", "some text", 1267, "some text", "text", "more text", "text text", "text", "final text"] ] } }注意
data中的每个样本是一个数组,元素顺序必须和columns完全对应,无需指定键名。模型输入Schema配置错误:多个模型均报错,说明可能是部署时的输入数据集Schema与实际请求不匹配。检查模型部署时定义的输入列是否包含
source_words,且数据类型设为整数;同时确认输入格式要求为“表格型数组”而非“JSON对象”。推理脚本(score.py)逻辑问题:自定义模型的推理脚本可能未适配对象数组格式的输入。检查
score.py中的run()函数,确保它能正确读取input_data['data']并解析出source_words。例如如果脚本使用pd.DataFrame(input_data['data'], columns=input_data['columns'])构建数据集,必须保证data是数组数组,否则DataFrame无法正确加载,导致列样本数为0。
内容的提问来源于stack exchange,提问作者phil

