使用多个输入的Tensorflow不能提供正确的输出

时间:2018-04-27 06:55:06

标签: python python-3.x tensorflow tensorflow-serving

我有两个占位符,必须作为输入提供并处理它

我保存了这个模型。

tf.app.flags.DEFINE_integer('model_version',3, 'version number of the model.')
tf.app.flags.DEFINE_string('work_dir', '', 'Working directory.')
FLAGS = tf.app.flags.FLAGS

sess = tf.InteractiveSession()
# define the tensorflow network and do some trains

x = tf.placeholder("float", name="x")
z = tf.placeholder("float", name="z")
z1 = tf.Variable(1.0, name="bias")

sess.run(tf.global_variables_initializer())

rt = tf.multiply(z, z1,name='rt')
rt = tf.to_float(rt,name = 'rt')

rtt = tf.add(1.0, rt, name='rtt')
rtt=tf.to_float(rtt,name='rtt')

y = tf.multiply(x, rtt,name='y')

export_path_base = FLAGS.work_dir
export_path = os.path.join(tf.compat.as_bytes(export_path_base),
  tf.compat.as_bytes(str(FLAGS.model_version)))
print('Exporting trained model to', export_path)
builder = tf.saved_model.builder.SavedModelBuilder(export_path)


tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
tensor_info_z = tf.saved_model.utils.build_tensor_info(z)
tensor_info_y = tf.saved_model.utils.build_tensor_info(y)


prediction_signature = (
  tf.saved_model.signature_def_utils.build_signature_def(
  inputs={'input': tensor_info_x,
          'inputz': tensor_info_z},
  outputs={'output': tensor_info_y},
  method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

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={
  'prediction':
  prediction_signature,
  },
  legacy_init_op=legacy_init_op)

在客户端,我发送输入tensor_proto以提供给占位符

class Session():

    def __init__(self):
        host, port = FLAGS.server.split(':')
        channel = implementations.insecure_channel(host, int(port))
        self.stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

        self.request = predict_pb2.PredictRequest()
        self.request.model_spec.name = 'New_model'
        self.request.model_spec.signature_name = 'prediction'

    def inference(self, val_x,val_y):
        self.request.inputs['input'].CopyFrom(tf.contrib.util.make_tensor_proto(val_x))
        self.request.inputs['inputz'].CopyFrom(tf.contrib.util.make_tensor_proto(val_y))
        result = self.stub.Predict(self.request, 5.0)
        return result

run = Session()

当我运行此模型时,它不会产生错误但会提供错误的值。例如,如果我发送x = 1000和z = 10,它显示x的输出(即1000)而不是实际输出11000.我不知道我在哪里出错了

0 个答案:

没有答案