Gcloud ML Engine 为输入数组添加了额外的维度
Gcloud ML Engine adds extra dimension to input array
正在尝试使用以下代码设置 Gcloud ML 引擎:
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)。但是,将其作为模型的输入时,我遇到了一个错误:
Cannot feed value of shape (1, 2, 3) for Tensor u'x:0', which has
shape '(?, 3)' (Error code: 2)
为了进一步测试这一点,在给出大小为 (1, 3) 的输入时,该模型运行良好。知道如何添加额外维度吗?
编辑:
用于测试的命令:
gcloud ml-engine local predict --model-dir=./test_exports --json-instances inputs.json
ML 引擎预测一批输入,而不仅仅是一个。因此,您应该将服务输入占位符更改为 [ None, 2, 3 ]
顺便说一句,让您的模型批量处理也有利于训练。
为从事相同工作的任何人发布解决方案。
以问题中显示的示例为例,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
进行进一步处理。
正在尝试使用以下代码设置 Gcloud ML 引擎:
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)。但是,将其作为模型的输入时,我遇到了一个错误:
Cannot feed value of shape (1, 2, 3) for Tensor u'x:0', which has shape '(?, 3)' (Error code: 2)
为了进一步测试这一点,在给出大小为 (1, 3) 的输入时,该模型运行良好。知道如何添加额外维度吗?
编辑:
用于测试的命令:
gcloud ml-engine local predict --model-dir=./test_exports --json-instances inputs.json
ML 引擎预测一批输入,而不仅仅是一个。因此,您应该将服务输入占位符更改为 [ None, 2, 3 ]
顺便说一句,让您的模型批量处理也有利于训练。
为从事相同工作的任何人发布解决方案。
以问题中显示的示例为例,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
进行进一步处理。