Amazon Sagemaker - Unable to evaluate payload provided

Viewed 988

I built a Sagemaker endpoint that I am attempting to evoke using Lambda+API Gateway. I'm getting the following error:

"An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from model with message \"unable to evaluate payload provided\"

I know why what it's complaining about, but I don't quite understand why it's occuring. I have confirmed that the shape of the input data of my lambda function is the same as how I trained the model. The following is my input payload in lambda:

X = pd.concat([X, rx_norm_dummies, urban_dummies], axis = 1)
payload = X.to_numpy()

response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                               ContentType='application/json',
                               Body=payload)

In the jupyter notebook where I created my endpoint/trained my model, I can also access the model using a numpy ndarray so I'm confused why I'm getting this error.

y = X[0:10]
result = linear_predictor.predict(y)
print(result)

Here is a modificaiton I make to serialization of the endpoint:

from sagemaker.predictor import csv_serializer, json_deserializer

    linear_predictor.content_type = 'text/csv'
    linear_predictor.serializer = csv_serializer
    linear_predictor.deserializer = json_deserializer

I'm new when it comes to Sagemaker/Lambda, so any help would be appreciated and I can send more code to add context if needed. Tried various foramts and cannot get this to work.

2 Answers

Not sure what algorithm you are trying to use in Sage but they have the quote below on their documentation. If your CSV has a header or if you don't specify the label_size a bad request (http status code 400) might be what you get because the model isn't compliant with what AWS expects to receive.

Amazon SageMaker requires that a CSV file does not have a header record and that the target variable is in the first column. To run unsupervised learning algorithms that don't have a target, specify the number of label columns in the content type. For example, in this case 'content_type=text/csv;label_size=0'.

From: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html#cdf-csv-format

In your code, you're indicating to the SDK that you are passing JSON but payload in your code is actually a NumPy array.

I would suggest replacing payload = X.to_numpy() to payload = X.to_json(orient='records') or something similar to convert it to JSON first.

Related