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

2024-04-19 22:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

{/strong>在生成一个基于python的应用程序后,需要一个经过训练的应用程序生成的图形。 为此,我查看了下面的example。但是我不明白的是如何收集我的输出。我知道我需要为图形提供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

有人能帮我理解TensorFlowJavaAPI吗?在


Tags: thenamelayeridnode应用程序inputdata
1条回答
网友
1楼 · 发布于 2024-04-19 22:38:55

上面列出的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;
  }
}

返回的predictions数组将具有每个预测的“置信度”值,并且您必须运行逻辑来计算它的“top K”,类似于Python代码如何使用numpy(.argsort())来计算sess.run()返回的内容。在

从教程页面和代码的粗略阅读来看,predictions将有1行12列(每个hotword对应一个)。我从下面的Python代码中得到这个:

^{pr2}$

希望有帮助。在

相关问题 更多 >