java - 为什么我在这个演示中在 TensorFlowInferenceInterface 中获取了错误的结果?

标签 java android tensorflow

这里是制作protocol buffer的程序。

# -*- 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()

这里是与 TensorFlowInferenceInterface 交互的 java 代码的主要部分。

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);

这是 result

加法结果总是正确的,但减法结果总是[1.0,1.0],这就是我想不通的原因,加减运算几乎相同而减法总是错误的A 定值。 任何意见都会有所帮助!请告诉我原因。 提前致谢!

最佳答案

谢谢灰!问题已通过修改代码解决

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

matrix1 = tf.placeholder(dtype=tf.float32, shape=(1,2), name='input1')
matrix2 = tf.placeholder(dtype=tf.float32, shape=(1,2), name='input2')

关于java - 为什么我在这个演示中在 TensorFlowInferenceInterface 中获取了错误的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49207984/

相关文章:

java - 为什么动态编程和朴素解决方案需要相同的执行时间?

java - Canvas 上的透视图

android - 使自定义 Android SurfaceView 透明

python - 'RefVariable' 对象没有属性 '_id'

python - 为什么我不能通过 pip 将 Tensorflow 从 2.0 版更新到 2.1 版?

java - Method.invoke java 上的监听器

JAVA 根据分隔符在字符串中添加单引号

android - Google Play 音乐应用的歌曲轨道信息

android - 在没有付费应用的国家/地区显示带有应用内结算功能的应用

tensorflow - 如何获取重新训练的 inception-v3 模型的 'checkpoint'?