【发布时间】:2018-11-06 16:07:54
【问题描述】:
我试图弄清楚如何使用神经网络为多标签分类任务生成混淆矩阵。我之前设法使用函数“intersection”计算精度,因为为此我不关心任何排序。
intersection = tf.sets.set_intersection(predictions, labels)
但是,为了计算混淆矩阵,我确实关心预测/标签的索引顺序。而且由于标签始终具有相同的值(1,1 或 0.5,0.5),因此不可能根据更高/更低的值进行排序。
我想知道:
1) 是否可以为多标签分类任务计算混淆矩阵?
2) 如何实施?
3) 你如何处理预测两个标签都失败的情况?因为不可能知道哪个混淆属于哪个预测。
4) 函数tf.nn.top_k()的排序背后的逻辑是什么
下面是我尝试使用的代码示例。
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
Z = np.array([[7.0, 3.0, 5.0, 1.0, 0.0, 6.0],[2.0, 3.0, 4.0, 1.0, 3.25, 2.2], [2.0 , 5.0, 1.0, 7.0, 0.0, 8.0]])
Y = np.array([[0.5, 0, 0, 0.0, 0, 0.5],[0, 0.0, 0.5, 0, 0.5, 0], [0,0,0,0.5,0,0.5]])
_, predicted_softmax = tf.nn.top_k(tf.nn.softmax(Z), k = 2, sorted = False)
_ , labels = tf.nn.top_k(Y, k = 2, sorted = False)
with tf.Session() as sess:
# reshape to (6,1) because there is 2 correct values per sample(2*3)
print(predicted_softmax.eval().reshape(6,1))
print(labels.eval().reshape(6,1))
predicted = predicted_softmax.eval().reshape(6,1)
labels_idx = labels.eval().reshape(6,1)
class_labels = np.arange(6)
cnf_matrix_train = confusion_matrix(labels_idx, predicted, labels = class_labels)
print(cnf_matrix_train)
我真的不明白为什么 predict_softmax 的输出是:
[[5] [0] [4] [2] [3] [5]] ,
我期待最后两个学期的 [5] [3]。此输出没有任何逻辑。在documentation 中,在sorted = False 认为的情况下,他们没有指定任何关于排序的内容,但我期待一些一致的行为。
感谢您的帮助!
【问题讨论】:
标签: python tensorflow neural-network confusion-matrix multilabel-classification