首页 > 解决方案 > 如何解决 AttributeError:“NoneType”对象没有属性“CONTENT_TYPE”

问题描述

我正在按照教程在 Sagemaker 上使用 MXNET 来开发推荐引擎。

执行以下单元格后,我收到 AttributeError: 'NoneType' object has no attribute 'CONTENT_TYPE'

test_preds= []
for array in np.array_split(test_df[['customer_id', 
'product_id']].values,40):
    test_preds += 
predictor.predict(json.dumps({'customer_id':array[:,0].tolist(),
                               'product_id':array[:,1].tolist()}))
test_preds = np.array(test_preds)
print('MSE:', np.mean((test_df['star_rating'] -test_preds))**2)

显示的错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-38-a830dbc6ac84> in <module>
      2 for array in np.array_split(test_df[['customer_id', 'product_id']].values,40):
      3     test_preds += predictor.predict(json.dumps({'customer_id':array[:,0].tolist(),
----> 4                                    'product_id':array[:,1].tolist()}))
      5 test_preds = np.array(test_preds)
      6 print('MSE:', np.mean((test_df['star_rating'] -test_preds))**2)

~/anaconda3/envs/amazonei_mxnet_p36/lib/python3.6/site-packages/sagemaker/predictor.py in predict(self, data, initial_args, target_model, target_variant, inference_id)
    132 
    133         request_args = self._create_request_args(
--> 134             data, initial_args, target_model, target_variant, inference_id
    135         )
    136         response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)

~/anaconda3/envs/amazonei_mxnet_p36/lib/python3.6/site-packages/sagemaker/predictor.py in _create_request_args(self, data, initial_args, target_model, target_variant, inference_id)
    158 
    159         if "ContentType" not in args:
--> 160             args["ContentType"] = self.content_type
    161 
    162         if "Accept" not in args:

~/anaconda3/envs/amazonei_mxnet_p36/lib/python3.6/site-packages/sagemaker/predictor.py in content_type(self)
    511     def content_type(self):
    512         """The MIME type of the data sent to the inference endpoint."""
--> 513         return self.serializer.CONTENT_TYPE
    514 
    515     @property

AttributeError: 'NoneType' object has no attribute 'CONTENT_TYPE'

  

**预测器是训练模型的生产端点

test_df 在这里定义

df_titles = df[['customer_id','product_title', 'product_id', 
'star_rating']]

df = df[['customer_id', 'product_id', 'star_rating']]

customers = df['customer_id'].value_counts()
products = df['product_id'].value_counts()

# Filter long-tail
customers = customers[customers >= 5]
products = products[products >= 10]
reduced_df_titles = df_titles.merge(pd.DataFrame({'customer_id': 
customers.index})).merge(pd.DataFrame({'product_id': 
products.index}))

reduced_df = df.merge(pd.DataFrame({'customer_id': 
customers.index})).merge(pd.DataFrame({'product_id': 
products.index}))
customers = reduced_df['customer_id'].value_counts()
products = reduced_df['product_id'].value_counts()

# Number users and items
#Next, we'll number each user and item, giving them their own 
sequential index. This will allow us to hold
#the information in a sparse format where the sequential indices 
indicate the row and column in our ratings matrix.
customer_index = pd.DataFrame({'customer_id': customers.index, 
'user': 
np.arange(customers.shape[0])})
product_index = pd.DataFrame({'product_id': products.index, 'item': 
np.arange(products.shape[0])})

reduced_df = reduced_df.merge(customer_index).merge(product_index)
reduced_df_titles = 
reduced_df_titles.merge(customer_index).merge(product_index)

# Split train and test

train_df, test_df = train_test_split(reduced_df, test_size=0.2, 
                                                random_state=0)

标签: pythonamazon-sagemakerrecommendation-enginemxnet

解决方案


推荐阅读