SageMaker Tensorflow - 如何编写我的serving_input_fn()

时间:2018-04-27 19:14:45

标签: tensorflow amazon-sagemaker

我对Tensorflow和SageMaker很陌生,我试图找出如何编写我的serving_input_fn()。我已经尝试了很多方法来做到这一点,但无济于事。

我的输入功能有3个功能列:amount_normalized, x_month and y_month

def construct_feature_columns():
    amount_normalized = tf.feature_column.numeric_column(key='amount_normalized')
    x_month = tf.feature_column.numeric_column(key='x_month')
    y_month = tf.feature_column.numeric_column(key='y_month')
    return set([amount_normalized, x_month, y_month])

我希望能够使用deployed_model.predict([1.23,0.3,0.8])

之类的东西来调用我部署的模型

第一个元素是amount_normalized,第二个元素是x_month,第三个元素是y_month

我试过这个:

FEATURES = ['amount_normalized', 'x_month', 'y_month']
def serving_input_fn(params):
    feature_placeholders = {
      key : tf.placeholder(tf.float32, [None]) \
        for key in FEATURES
    }
return tf.estimator.export.build_raw_serving_input_receiver_fn(feature_placeholders)()

但我得到的只是: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "".

任何帮助都非常赞赏!

1 个答案:

答案 0 :(得分:3)

如果其他人有此问题,请在此处发布。

经过一系列的反复试验后,我设法通过编写我的服务输入函数解决了我的问题:

FEATURES = ['amount_normalized', 'x_month', 'y_month']
def serving_input_fn(hyperparameters):
    feature_spec = {
        key : tf.FixedLenFeature(shape=[], dtype = tf.float32) \
          for key in FEATURES
    }
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()

然后我可以通过传入哈希来调用我部署的模型:

deployed_model.predict({"amount_normalized": 2.3, "x_month": 0.2, "y_month": -0.3})