Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow:如何在java中使用python训练的语音识别模型_Java_Python_Tensorflow - Fatal编程技术网

Tensorflow:如何在java中使用python训练的语音识别模型

Tensorflow:如何在java中使用python训练的语音识别模型,java,python,tensorflow,Java,Python,Tensorflow,我有一个用python训练的tensorflow模型,在训练后我生成了冻结图。现在我需要使用这个图,并在基于JAVA的应用程序上生成识别。 为此,我看了以下内容。然而,我不明白的是如何收集我的输出。我知道我需要为图表提供3个输入 从官方教程中给出的示例中,我已经阅读了基于python的代码 def run_graph(wav_data, labels, input_layer_name, output_layer_name, num_top_predictions):

我有一个用python训练的tensorflow模型,在训练后我生成了冻结图。现在我需要使用这个图,并在基于JAVA的应用程序上生成识别。 为此,我看了以下内容。然而,我不明白的是如何收集我的输出。我知道我需要为图表提供3个输入

从官方教程中给出的示例中,我已经阅读了基于python的代码

def run_graph(wav_data, labels, input_layer_name, output_layer_name,
              num_top_predictions):
  """Runs the audio data through the graph and prints predictions."""
  with tf.Session() as sess:
    # Feed the audio data as input to the graph.
    #   predictions  will contain a two-dimensional array, where one
    #   dimension represents the input image count, and the other has
    #   predictions per class
    softmax_tensor = sess.graph.get_tensor_by_name(output_layer_name)
    predictions, = sess.run(softmax_tensor, {input_layer_name: wav_data})

    # Sort to show labels in order of confidence
    top_k = predictions.argsort()[-num_top_predictions:][::-1]
    for node_id in top_k:
      human_string = labels[node_id]
      score = predictions[node_id]
      print('%s (score = %.5f)' % (human_string, score))

    return 0

有人能帮我理解tensorflow java api吗?

上面列出的Python代码的直译如下:

public static float[][] getPredictions(Session sess, byte[] wavData, String inputLayerName, String outputLayerName) {
  try (Tensor<String> wavDataTensor = Tensors.create(wavData);
       Tensor<Float> predictionsTensor = sess.runner()
                    .feed(inputLayerName, wavDataTensor)
                    .fetch(outputLayerName)
                    .run()
                    .get(0)
                    .expect(Float.class)) {
    float[][] predictions = new float[(int)predictionsTensor.shape(0)][(int)predictionsTensor.shape(1)];
    predictionsTensor.copyTo(predictions);
    return predictions;
  }
}
希望有帮助

import tensorflow as tf

graph_def = tf.GraphDef()
with open('/tmp/my_frozen_graph.pb', 'rb') as f:
  graph_def.ParseFromString(f.read())

output_layer_name = 'labels_softmax:0'

tf.import_graph_def(graph_def, name='')
print(tf.get_default_graph().get_tensor_by_name(output_layer_name).shape)