Java 如何在TensorFlowEnferenceInterface中使用feed和fetch函数?

Java 如何在TensorFlowEnferenceInterface中使用feed和fetch函数?,java,android,tensorflow,java-native-interface,Java,Android,Tensorflow,Java Native Interface,虽然我想在TensorFlowEnferenceInterface中使用feed和fetch函数,但我无法理解feed和fetch参数。 public void feed(String inputName, float[] src, long... dims) public void fetch(String outputName, float[] dst) 这里是TensorFlowEnferenceInterface。↓ 现在,我使用Android Studio并希望使用MNIST导


虽然我想在TensorFlowEnferenceInterface中使用feed和fetch函数,但我无法理解feed和fetch参数。

public void feed(String inputName, float[] src, long... dims) 
public void fetch(String outputName, float[] dst) 
这里是TensorFlowEnferenceInterface。↓

现在,我使用Android Studio并希望使用MNIST导入程序。
下面是制作协议缓冲区的程序。

import tensorflow as tf
import shutil
import os.path

if os.path.exists("./tmp/beginner-export"):
    shutil.rmtree("./tmp/beginner-export")

# Import data
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("./tmp/data/", one_hot=True)

g = tf.Graph()

with g.as_default():
    # Create the model
    x = tf.placeholder("float", [None, 784])
    W = tf.Variable(tf.zeros([784, 10]), name="vaiable_W")
    b = tf.Variable(tf.zeros([10]), name="variable_b")
    y = tf.nn.softmax(tf.matmul(x, W) + b)

    # Define loss and optimizer
    y_ = tf.placeholder("float", [None, 10])
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

    sess = tf.Session()

    # Train
    init = tf.initialize_all_variables()
    sess.run(init)

    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        train_step.run({x: batch_xs, y_: batch_ys}, sess)

    # Test trained model
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

    print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}, sess))

# Store variable
_W = W.eval(sess)
_b = b.eval(sess)

sess.close()

# Create new graph for exporting
g_2 = tf.Graph()
with g_2.as_default():
    # Reconstruct graph
    x_2 = tf.placeholder("float", [None, 784], name="input")
    W_2 = tf.constant(_W, name="constant_W")
    b_2 = tf.constant(_b, name="constant_b")
    y_2 = tf.nn.softmax(tf.matmul(x_2, W_2) + b_2, name="output")

    sess_2 = tf.Session()

    init_2 = tf.initialize_all_variables();
    sess_2.run(init_2)

    graph_def = g_2.as_graph_def()

    tf.train.write_graph(graph_def, './tmp/beginner-export',
                         'beginner-graph.pb', as_text=False)

    # Test trained model
    y__2 = tf.placeholder("float", [None, 10])
    correct_prediction_2 = tf.equal(tf.argmax(y_2, 1), tf.argmax(y__2, 1))
    accuracy_2 = tf.reduce_mean(tf.cast(correct_prediction_2, "float"))
    print(accuracy_2.eval({x_2: mnist.test.images, y__2: mnist.test.labels}, sess_2))
输入的占位符名称为“输入”。
输出的占位符名称为“output”。


请告诉我feed和fetch的用法。

我已经给出了一个带有注释的示例代码。希望你能理解

    private static final String INPUT_NODE = "input:0"; // input tensor name
    private static final String OUTPUT_NODE = "output:0"; // output tensor name
    private static final String[] OUTPUT_NODES = {"output:0"}; 
    private static final int OUTPUT_SIZE = 10; // number of classes
    private static final int INPUT_SIZE = 784; // size of the input
    INPUT_IMAGE //MNIST Image
    float[] result = new float[OUTPUT_SIZE]; // get the output probabilities for each class

    inferenceInterface.feed(INPUT_NODE, INPUT_IMAGE, 1, INPUT_SIZE); //1-D input (1,INPUT_SIZE)
    inferenceInterface.run(OUTPUT_NODES);
    inferenceInterface.fetch(OUTPUT_NODE, result);
对于我正在使用的Android Tensorflow库版本,我需要提供一个一维输入。因此,Tensorflow代码需要据此进行修改

x_2 = tf.placeholder("float", [None, 1, 784], name="input") //1-D input
x_2 = tf.reshape(x_2,[-1, 784]) // reshape according to the model requirements

希望这有帮助。

谢谢你教我!但是,我还有一个问题。虽然我理解提要函数args中的inputName和dim,但我不能理解src。我是否应该将带有784个DIM(例如[1,33,34,2,84,…])的展平数字输入src?src是示例代码中的输入图像。因此,根据您的模型,src应该是输入图像。此外,它应该是一个一维数组(在您的例子中是float[]src)。