loading and deploying a previously trained sagemaker xgboost model

0

I am trying to write an inference pipeline where I load a previously trained sagemaker xgboost model stored in s3 as a tar.gz file (following sagemaker tutorial) and deploy it as an endpoint for prediction. Here is my code:

trainedmodel = sagemaker.model.Model(    
    model_data='data-path-to-my-model-in-s3/model.tar.gz',
    image=container,  
    role=role)  

xgb_predictor = trainedmodel.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')

The code runs fine but after that when I try to call predict() on xgb_predictor I get an error saying 'NoneType' object has no attribute 'predict'. I followed the example here to train the xgboost model:

https://aws.amazon.com/blogs/machine-learning/simplify-machine-learning-with-xgboost-and-amazon-sagemaker/

Why am I getting this error? What's the correct way to load a previously trained model? Help would be appreciated.

hadi86
已提問 5 年前檢視次數 2501 次
3 個答案
0

thanks for using SageMaker! you're on the right path - you'll need to pass in an argument for "predictor_cls" when creating your Model instance in order for a predictor object to be returned after calling deploy(), e.g.

from sagemaker.model import Model
from sagemaker.predictor import RealTimePredictor, csv_serializer, csv_deserializer

class Predictor(RealTimePredictor):
    def __init__(self, endpoint_name, sagemaker_session=None):
        super(Predictor, self).__init__(
            endpoint_name, sagemaker_session, csv_serializer, csv_deserializer
        )

trainedmodel = Model(..., predictor_cls=Predictor)
xgb_predictor = trainedmodel.deploy(...)

xgb_predictor.predict(...)

API reference:

hope that helps!

已回答 5 年前
profile picture
專家
已審閱 19 天前
0

Thank you. This solution worked!

hadi86
已回答 5 年前
0

Any special reason for using csv serializer/deserializer? In my case I reload a model to analyze videos (frames in numpy array actually). What serializer/deserializer should I use?
Actually, any doc regarding how to properly use the argument predictor_cls would be highly appreicated.

已回答 3 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南