【问题标题】:prediction result - Tensorflow预测结果 - TensorFlow
【发布时间】:2021-08-09 01:09:40
【问题描述】:

除了模型的准确性之外,我还尝试打印预测结果和标签。 我不确定我在这里做错了什么

for mfcc, label in test_data:
 prediction = tflite_inference(mfcc, tflite_path)
 predicted_indices.append(np.squeeze(tf.argmax(prediction, axis=1)))

strlabel="C:/tmp/speech_commands_train/conv_labels.txt"
labels_list= [line.rstrip() for line in tf.io.gfile.GFile(strlabel)]

top_k = prediction.argsort()[-5:][::-1]


for node_id in top_k:
 human_string = labels_list[node_id]
 score = predicted_indices[node_id]
print('%s (score = %.5f)' % (human_string, score))

test_accuracy = calculate_accuracy(predicted_indices, expected_indices)
confusion_matrix = tf.math.confusion_matrix(expected_indices, predicted_indices,
                                        num_classes=model_settings['label_count'])

` 错误信息

human_string = labels_list[node_id] TypeError: only integer scalar arrays can be converted to a scalar index

提前感谢您的帮助。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    编辑的答案(在对问题进行了一些澄清之后): 在这里,我假设 prediction 变量是您的模型对于单个输入的输出。在此假设下,您的 top_k 应包含概率最高的前 5 个索引。 为此,您应该执行以下操作:

    1. 重塑您的 predictions 变量:
    predictions = predictions.reshape(-1) # this will make the predicitions a vector
    
    1. 获取top_k
    # this step is same but this time the output will be a vector instead of a matrix
    top_k = prediction.argsort()[-5:][::-1] 
    
    1. 使用循环
    # This is also same but as the `top_k` is a vector instead of a matrix there 
    # won't be any issues/errors.
    for node_id in top_k:
     human_string = labels_list[node_id]
     score = predicted_indices[node_id]
     print('%s (score = %.5f)' % (human_string, score))
    

    【讨论】:

    • 谢谢,但现在我得到 print('%s (score = %.5f)' % (human_string, score)) TypeError: only size-1 arrays can be convert to Python scalars.
    • 你得到这个是因为你的human_string and score 是数组而不是字符串和整数。我将在我的回答中进行编辑和解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2021-09-11
    • 2018-06-28
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多