Tensorflow,多标签混淆矩阵

时间:2018-05-28 06:02:07

标签: python tensorflow neural-network multilabel-classification

我试图弄清楚如何使用神经网络为多标签分类任务生成混淆矩阵。我之前设法使用函数“十字路口”计算精度,因为我不关心任何排序。

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想到的情况下指定有关排序的任何内容,但我期待一些一致的行为。

感谢您的帮助!

0 个答案:

没有答案
相关问题