我正在尝试打印预测结果和标签,以及模型的准确性。我不确定我在这里做错了什么
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
提前感谢您的帮助。
编辑后的回答(在澄清了问题之后):这里我假设prediction
变量是单个输入的模型输出。有了这个假设,你的top_k
应该以最高的概率包含前5个指数。要做到这一点,你应该这样做:
- 重塑
predictions
变量:
predictions = predictions.reshape(-1) # this will make the predicitions a vector
- 获取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]
- 使用循环
# 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))