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

SageMaker Tensorflow - how to write my serving_input_fn()

我是 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 "".

如有任何帮助,非常感谢

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

经过反复试验,我设法通过如下编写服务输入函数来解决我的问题:

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})