1

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

任何帮助将不胜感激!

4

1 回答 1

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})
于 2018-04-29T16:33:54.973 回答