我正在使用如下所示的导出来为我的模型提供服务:
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": "您必须使用 dtype float\t 为 占位符张量 \'Placeholder_1\' 输入一个值 [[Node: Placeholder_1 = Placeholder_output_shapes=[], dtype=DT_FLOAT, shape=, _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"]]" }
有谁知道我做错了什么?
您必须确保两个占位符的形状features_placeholder
和labels_placeholder
对应于两个变量的形状features_feed
和labels_feed
以避免在输入字典时遇到的错误。
对于那些寻求这个问题的答案的人,我会试一试。
导出模型时,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
由于我已经将我的对数张量命名为"预测",我现在能够在保存模型之前以图形模式获取此张量:
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% 确定正确的方法。