PacktPublishing / Learn-Amazon-SageMaker

Learn Amazon SageMaker
MIT License
103 stars 88 forks source link

Ch 4 Factorization Machines Endpoint Content_Type error #3

Closed WheaTex closed 3 years ago

WheaTex commented 4 years ago

In Chapter 4, Factorization Machines Notebook in the "Deploy Model" section there is an issue after creating the endpoint. While attempting to run this code:

'fm_predictor.content_type = "application/json" '

You get this error; 'AttributeError: can't set attribute'

If you try to comment it out & run the next block of code you get this error: 'AttributeError: "function" object has no attribute "CONTENT_TYPE"'

I attempted to follow @laurenyu's feedback: https://github.com/aws/sagemaker-python-sdk/issues/1126 but that code doesn't appear to work. Please advise.

khordoo commented 4 years ago

This appears to be due to the removal of the methods in version 2.x of the SageMaker:

content_type and accept in the Predictor Constructor
The content_type and accept parameters are now no-ops in the following classes and methods:

sagemaker.predictor.Predictor

sagemaker.estimator.Estimator.create_model

sagemaker.algorithms.AlgorithmEstimator.create_model

sagemaker.tensorflow.model.TensorFlowPredictor

Please specify content types in a serializer or deserializer class instead.

You need to specify the serializer and deserializer in the deploy method instead.

Here is a sample code:

from sagemaker.deserializers import JSONDeserializer
from sagemaker.serializers import JSONSerializer

class FMSerializer(JSONSerializer):
    def serialize(self, data):
       js = {'instances': []}
       for row in data:
              js['instances'].append({'features': row.tolist()})
       return json.dumps(js)

predictor = estimator.deploy(
    initial_instance_count=1,
    instance_type="ml.m4.xlarge",
    serializer=FMSerializer(),
    deserializer= JSONDeserializer()
)
juliensimon commented 3 years ago

That's correct. Fixing these is on my to do list, once AWS re:Invent is out the way :)

juliensimon commented 3 years ago

Fixed.