Tensorflow confusion matrix using one-hot code

前端 未结 1 644
轮回少年
轮回少年 2021-01-05 12:40

I have multi-class classification using RNN and here is my main code for RNN:

def RNN(x, weights, biases):
    x = tf         


        
1条回答
  •  粉色の甜心
    2021-01-05 13:33

    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]]
    

    0 讨论(0)
提交回复
热议问题