不可能用sagemaker调用端点

时间:2018-09-09 13:24:53

标签: amazon-sagemaker

我正在使用aws sagemaker调用端点:

payload = pd.read_csv('payload.csv', header=None)

>> payload


    0   1   2   3   4
0   setosa  5.1     3.5     1.4     0.2
1   setosa  5.1     3.5     1.4     0.2

使用此代码:

response = runtime.invoke_endpoint(EndpointName=r_endpoint,
                                   ContentType='text/csv',
                                   Body=payload)

但是我遇到了这个问题:

ParamValidationError                      Traceback (most recent call last)
<ipython-input-304-f79f5cf7e0e0> in <module>()
      1 response = runtime.invoke_endpoint(EndpointName=r_endpoint,
      2                                    ContentType='text/csv',
----> 3                                    Body=payload)
      4 
      5 result = json.loads(response['Body'].read().decode())

~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    312                     "%s() only accepts keyword arguments." % py_operation_name)
    313             # The "self" in this scope is referring to the BaseClient.
--> 314             return self._make_api_call(operation_name, kwargs)
    315 
    316         _api_call.__name__ = str(py_operation_name)

~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    584         }
    585         request_dict = self._convert_to_request_dict(
--> 586             api_params, operation_model, context=request_context)
    587 
    588         handler, event_response = self.meta.events.emit_until_response(

~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _convert_to_request_dict(self, api_params, operation_model, context)
    619             api_params, operation_model, context)
    620         request_dict = self._serializer.serialize_to_request(
--> 621             api_params, operation_model)
    622         prepare_request_dict(request_dict, endpoint_url=self._endpoint.host,
    623                              user_agent=self._client_config.user_agent,

~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/validate.py in serialize_to_request(self, parameters, operation_model)
    289                                                     operation_model.input_shape)
    290             if report.has_errors():
--> 291                 raise ParamValidationError(report=report.generate_report())
    292         return self._serializer.serialize_to_request(parameters,
    293                                                      operation_model)

ParamValidationError: Parameter validation failed:
Invalid type for parameter Body, value:         0    1    2    3    4
0  setosa  5.1  3.5  1.4  0.2
1  setosa  5.1  3.5  1.4  0.2, type: <class 'pandas.core.frame.DataFrame'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object

我只是使用与aws教程中相同的代码/步骤。

您能帮我解决这个问题吗?

谢谢

1 个答案:

答案 0 :(得分:2)

有效负载变量是Pandas的DataFrame,而invoke_endpoint()期望Body=b'bytes'|file

尝试这样的事情(盲人编码):

response = runtime.invoke_endpoint(EndpointName=r_endpoint,
                                   ContentType='text/csv',
                                   Body=open('payload.csv'))

有关expected formats here的更多信息。 确保文件不包含标题。

或者,将您的DataFrame转换为字节like in this example,然后传递这些字节而不是传递DataFrame。