首页 > 解决方案 > Tensorflow 服务 - “您必须为占位符张量“Placeholder_1”提供一个值”

问题描述

我正在使用这样生成的导出来服务我的模型:

features_placeholder = tf.placeholder(tf.float32, None)
labels_placeholder = tf.placeholder(tf.float32, None)

# Training loop code
......
# Train is finished.

# Export model
tf.saved_model.simple_save(sess,param.logs_dir + 'model_export', 
            {"features": features_placeholder}, {"binary_classif": labels_placeholder})

然后,我发出以下 POST 请求(原始正文):

{“实例”:[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]}

我得到的错误如下:

{ "error": "您必须为占位符张量 \'Placeholder_1\' 提供一个值,其 dtype 为 float\n\t [[Node: Placeholder_1 = Placeholder_output_shapes=[], dtype=DT_FLOAT, shape=, _device=\"/job :localhost/replica:0/task:0/device:CPU:0\"]]" }

有谁知道我做错了什么?

标签: pythontensorflowtensorflow-serving

解决方案


对于那些寻求这个问题的答案的人,我会试一试。

导出模型时,simple_save 函数需要张量指针,而不是占位符。一种方法是在定义模型时命名张量,如下所示:

def inference(features):
    layer_1 = nn_layer(features, get_num_features(), get_num_hidden1(), 'layer1', act=tf.nn.relu)
    logits = nn_layer(layer_1, get_num_hidden1(), get_num_classes(), 'out', act=tf.identity)
    logits = tf.identity(logits, name='predictions')
    return logits

由于我已将我的 logits 张量命名为“预测”,因此我现在可以在保存模型之前以图形模式获取此张量:

features = graph.get_tensor_by_name('features:0')
predictions = graph.get_tensor_by_name('predictions:0')
tf.saved_model.simple_save(sess,param.logs_dir + 'model_export', 
                {"features": features}, 
                {"predictions": predictions})

注意:Tensorflow 文档非常简短,尤其是关于 simple_save 函数。这是我可以使它工作的唯一方法,但我不能 100% 确定正确的方法。


推荐阅读