简体   繁体   中英

AWS Sagemaker, InvokeEndpoint operation, Model error: “setting an array element with a sequence.”

I am trying to Invoke Endpoint, previously deployed on Amazon SageMaker. Here is my code:

import numpy as np
import boto3

client = boto3.client('sagemaker-runtime')

def np2csv(arr):
    csv = io.BytesIO()
    np.savetxt(csv, arr, delimiter=',', fmt='%g')
    return csv.getvalue().decode().rstrip()

endpoint_name = 'DEMO-XGBoostEndpoint-2018-12-12-22-07-28'
test_vector = np.array([3.60606061e+00, 
                        3.91395664e+00, 
                        1.34200000e+03, 
                        4.56100000e+03,
                        2.00000000e+02, 
                        2.00000000e+02]) 
csv_test_vector = np2csv(test_vector)

response = client.invoke_endpoint(EndpointName=endpoint_name,
                               ContentType='text/csv',
                               Body=csv_test_vector)

And here is the error I get:

ModelErrorTraceback (most recent call last) in () 1 response = client.invoke_endpoint(EndpointName=endpoint_name, 2 ContentType='text/csv', ----> 3 Body=csv_test_vector)

/home/ec2-user/anaconda3/envs/python2/lib/python2.7/site-packages/botocore/client.pyc in _api_call(self, *args, **kwargs) 318 "%s() only accepts keyword arguments." % py_operation_name) 319 # The "self" in this scope is referring to the BaseClient. --> 320 return self._make_api_call(operation_name, kwargs) 321 322 _api_call. name = str(py_operation_name)

/home/ec2-user/anaconda3/envs/python2/lib/python2.7/site-packages/botocore/client.pyc in _make_api_call(self, operation_name, api_params) 621 error_code = parsed_response.get("Error", {}).get("Code") 622 error_class = self.exceptions.from_code(error_code) --> 623 raise error_class(parsed_response, operation_name) 624 else: 625 return parsed_response

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (415) from model with message "setting an array element with a sequence.". See https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#logEventViewer:group=/aws/sagemaker/Endpoints/DEMO-XGBoostEndpoint-2018-12-12-22-07-28 in account 249707424405 for more information.

This works:

import numpy as np
import boto3

client = boto3.client('sagemaker-runtime')
endpoint_name = 'DEMO-XGBoostEndpoint-2018-12-12-22-07-28'
test_vector = [3.60606061e+00, 
               3.91395664e+00, 
               1.34200000e+03, 
               4.56100000e+03,
               2.00000000e+02, 
               2.00000000e+02]) 

body = ',',join([str(item) for item in test_vector])
response = client.invoke_endpoint(EndpointName=endpoint_name,
                               ContentType='text/csv',
                               Body=body)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM