Tensorflow confusion matrix using one-hot code

廉价感情. 提交于 2019-12-01 10:41:38
user1190882

You cannot generate confusion matrix using one-hot vectors as input parameters of labels and predictions. You will have to supply it a 1D tensor containing your labels directly.

To convert your one hot vector to normal label, make use of argmax function:

label = tf.argmax(one_hot_tensor, axis = 1)

After that you can print your confusion_matrix like this:

import tensorflow as tf

num_classes = 2
prediction_arr = tf.constant([1,  1, 1, 1,  0, 0, 0, 0,  1, 1])
labels_arr     = tf.constant([0,  1, 1, 1,  1, 1, 1, 1,  0, 0])

confusion_matrix = tf.confusion_matrix(labels_arr, prediction_arr, num_classes)
with tf.Session() as sess:
    print(confusion_matrix.eval())

Output:

[[0 3]
 [4 3]]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!