我应该如何使用tf.saved_model.simple_save保存经过训练的模型,以便可以使用tensorflow-serving进行请求
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
values = tf.placeholder(tf.float32, [None, 1])
layer = tf.add(tf.matmul(x, w), b)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=layer))
optimize = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
correct_pred = tf.equal(tf.argmax(layer, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
with tf.Session() as sess:
sess.run(init)
for _ in range(10000):
batch = mnist.train.next_batch(100)
sess.run(accuracy, feed_dict={x:batch[0],y:batch[1]})
!rm -rf "/model"
export_dir = "/model/1"
#Problem here
tf.saved_model.simple_save(
sess,
export_dir=export_dir,
inputs={"x":x},
outputs={"accuracy":accuracy}
)
当我跑步时:
!saved_model_cli show --dir {export_dir} --all
I get:
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['x'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 784)
name: Placeholder:0
The given SavedModel SignatureDef contains the following output(s):
outputs['accuracy'] tensor_info:
dtype: DT_FLOAT
shape: ()
name: Mean_1:0
Method name is: tensorflow/serving/predict
我的输出是shape()而不是(-1,x)或那种格式。 当我发送请求时,没有任何回应。由于准确性是一项操作,所以我没有任何回应。如何将其更改为变量,或者如何在keras中使用{t.name for model.outputs中的t}?
答案 0 :(得分:1)
问题出在代码的最后一行outputs={"accuracy":accuracy}
中。如果将accuracy
替换为'layer',则会解决此问题。因此,代码如下所示:
tf.saved_model.simple_save(sess, export_dir=export_dir, inputs={"x":x},
outputs={"Predicted_Output":layer})
答案 1 :(得分:0)
simple_save中的输出似乎不正确。它应该是分层的,但不是准确性。