尝试使用以下代码设置 Gcloud ML Engine:
import numpy as np
import tensorflow as tf
x = tf.placeholder('float', shape=[None, 3], name='x')
w = tf.Variable(tf.zeros([3, 2]))
y = tf.nn.softmax(tf.matmul(x, w), name='y')
sess = tf.Session()
sess.run(tf.global_variables_initializer())
tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
tensor_info_y = tf.saved_model.utils.build_tensor_info(y)
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'inputs': tensor_info_x},
outputs={'scores': tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
)
export_path = './test_exports'
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_images':
prediction_signature
},
legacy_init_op=legacy_init_op)
builder.save()
我用于预测的示例输入 JSON 文件是:
{ "inputs" : [[ 0.32439028, 0.07830289, 0.30881251], [ 0.32439028, 0.07830289, 0.30881251]] }
如您所见,输入数组的形状是 (2, 3)。但是,当将此作为模型的输入时,我遇到了一个错误:
无法为 Tensor u'x:0' 提供形状 (1, 2, 3) 的值,其形状为“(?, 3)”(错误代码:2)
为了进一步测试,在给出大小为 (1, 3) 的输入时,该模型运行良好。知道如何添加额外维度吗?
编辑 :
用于测试的命令:
gcloud ml-engine local predict --model-dir=./test_exports --json-instances inputs.json
为任何从事相同工作的人发布解决方案。
以问题中显示的示例为例,ml 引擎给模型的输入的形状为 [1, N, m],其中 N 是输入的数量,m 是特征大小。因此,要将输入转换为这种格式,请tf.squeeze()
按如下方式使用:
x = tf.placeholder('float', shape=[1, None, 3], name='x')
x_exp = tf.squeeze(x, axis=0)
现在您可以x_exp
用于进一步处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句