Java 为什么我在这个演示的TensorFlowEnferenceInterface中获取错误的结果?

Java 为什么我在这个演示的TensorFlowEnferenceInterface中获取错误的结果?,java,android,tensorflow,Java,Android,Tensorflow,下面是制作协议缓冲区的程序 # -*- coding:utf-8 -*- import tensorflow as tf session = tf.Session() matrix1 = tf.constant([[1., 3.]], name='input1') matrix2 = tf.constant([[2., 2.]], name='input2') mat_add = tf.add(matrix1, matrix2, name='output1') mat_sub = tf.su

下面是制作协议缓冲区的程序

# -*- coding:utf-8 -*-
import tensorflow as tf

session = tf.Session()

matrix1 = tf.constant([[1., 3.]], name='input1')
matrix2 = tf.constant([[2., 2.]], name='input2')
mat_add = tf.add(matrix1, matrix2, name='output1')
mat_sub = tf.subtract(matrix1, matrix2, name='output2')

session.run(mat_add)
session.run(mat_sub)


tf.train.write_graph(session.graph.as_graph_def(), "./models/", "simple.pb", as_text=False)

session.close()
这里是java代码的主要部分,它与TensorFlowEnferenceInterface接口

inferenceInterface = new TensorFlowInferenceInterface(getAssets(),MODEL_FILE);

input1[0] = (float) 5.0; input1[1] = (float) 6.0;
input2[0] = (float) 2.0; input2[1] = (float) 3.0;


inferenceInterface.feed("input1", input1, new long[]{1,2});
inferenceInterface.feed("input2", input2, new long[]{1,2});

inferenceInterface.run(new String[]{"output1","output2"});

inferenceInterface.fetch("output1", output1);
inferenceInterface.fetch("output2", output2);


for(float f : output1)
    Log.i(TAG, "output1: " + f);
for(float f : output2)
    Log.i(TAG, "output2: " + f);
这是

加法的结果总是正确的,而减法的结果总是[1.0,1.0],这就是为什么我不明白,加法和减法的运算几乎是一样的,而减法的运算总是错误的,是一个错误 固定值。 任何意见都会有帮助!请告诉我原因。
提前谢谢

谢谢你,阿什!通过更改代码,问题得到了解决

matrix1 = tf.constant([[1., 3.]], name='input1')
matrix2 = tf.constant([[2., 2.]], name='input2')


您使用的是哪个版本的TensorFlow?我似乎无法在更新的版本(如1.4、1.5或1.6)中重现您的问题。使用更新的版本有帮助吗?此外,也就是说,您的图形是用常量作为输入定义的,这意味着图形优化可能会起作用(比如常量折叠)。如果您真的想“输入”输入,那么在图形中使用
tf.placeholder
而不是
tf.constant
来定义它们,看看这是否有帮助。希望能有帮助。非常感谢!我使用的是Tensorflow 1.2,当我使用
tf.placeholder
而不是
tf.constant
时,结果变得正确。再次感谢你的帮助!
matrix1 = tf.placeholder(dtype=tf.float32, shape=(1,2), name='input1')
matrix2 = tf.placeholder(dtype=tf.float32, shape=(1,2), name='input2')