AWS DeepAR预测返回400错误问题排查求助
问题
尝试使用AWS DeepAR基于当前及过往月份的数据,对时间序列的下15条记录进行预测。已参照官方示例完成配置,但调用predict方法时返回400 ModelError,报错信息为"Unable to evaluate payload provided"。尝试移除serializer/deserializer后,仍出现格式相关错误。
部署代码
predictor = estimator.deploy( initial_instance_count=1, instance_type='ml.m5.large', serializer=JSONSerializer(), deserializer=JSONDeserializer() ) json_request = json.dumps({ "instances": ts, "configuration": { "num_samples": 10, "output_types": ["quantiles", "samples"], "quantiles": ['0.2', '0.5', '0.8'] } }) prediction = predictor.predict(json_request)
请求JSON示例
{"instances": [{"start": "2024-03-01", "target":[60,10,86,62,21,25,7,79,33,82,34,43,14,99,5,37,85,84,88,25,2,14,15,98,14,75,70,99,12] }, {"start": "2024-04-01", "target": [55,89,40,81,87,7,49,77,37,42,48,27,89,45,85] }], "configuration": {"num_samples": 15, "output_types": ["quantiles", "samples"], "quantiles": ["0.2", "0.5", "0.8"]}}
报错堆栈
--------------------------------------------------------------------------- ModelError Traceback (most recent call last) Cell In[22], line 2 1 print(type('json_request')) ----> 2 prediction = predictor.predict(json_request) 3 print(prediction) File c:\Users\civan\PycharmProjects\JupyterBooks\.venv\Lib\site-packages\sagemaker\base_predictor.py:212, in Predictor.predict(self, data, initial_args, target_model, target_variant, inference_id, custom_attributes, component_name) 209 if inference_component_name: 210 request_args["InferenceComponentName"] = inference_component_name --> 212 response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args) 213 return self._handle_response(response) File c:\Users\civan\PycharmProjects\JupyterBooks\.venv\Lib\site-packages\botocore\client.py:553, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs) 549 raise TypeError( 550 f"{py_operation_name}() only accepts keyword arguments." 551 ) 552 # The "self" in this scope is referring to the BaseClient. --> 553 return self._make_api_call(operation_name, kwargs) File c:\Users\civan\PycharmProjects\JupyterBooks\.venv\Lib\site-packages\botocore\client.py:1009, in BaseClient._make_api_call(self, operation_name, api_params) 1005 error_code = error_info.get("QueryErrorCode") or error_info.get( 1006 "Code" 1007 ) 1008 error_class = self.exceptions.from_code(error_code) --> 1009 raise error_class(parsed_response, operation_name) 1010 else: 1011 return parsed_response ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "Unable to evaluate payload provided".
排查思路与解决方法
1. 修复双重序列化问题
代码中同时使用json.dumps()手动序列化,又配置了JSONSerializer(),导致请求数据被双重序列化(JSON字符串被再次转义),模型无法解析:
- 解决:直接传入Python字典给
predict方法,去掉json.dumps():prediction = predictor.predict({ "instances": ts, "configuration": { "num_samples": 10, "output_types": ["quantiles", "samples"], "quantiles": ['0.2', '0.5', '0.8'] } })
2. 匹配时间序列频率
DeepAR要求训练与预测的时间序列频率完全一致:
- 检查训练时指定的
freq参数(如'D'代表日度),确认请求中start日期+target长度对应的周期数是否匹配。示例中3月只有29条数据,若freq为'D'则缺失2条,会触发格式错误。 - 解决:补全缺失时间点的数据(用NaN填充),确保每个序列的长度与对应时间段的频率周期数一致。
3. 校验配置参数合法性
检查configuration字段的参数格式:
num_samples需为正整数,quantiles取值需在0-1之间(字符串或数字类型均可,但需统一)。- 若训练时使用了动态特征,预测请求需补充对应的
dynamic_feat字段。
4. 确保端点与模型兼容性
- 确认部署的模型是DeepAR estimator训练生成的,未混用其他模型类型。
- 若移除序列化器后仍报错,显式指定请求的Content-Type:
prediction = predictor.predict(data_dict, initial_args={"ContentType": "application/json"})
内容的提问来源于stack exchange,提问作者Claudiu
相关产品推荐
相关产品推荐

