简体   繁体   中英

Tensorflow: How to create confusion matrix

I am new to tensorflow, I used this tutorial:

https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/ .

I have trained the same model on new dataset which contains 3 labels. I am trying to create the confusion matrix.

tf.confusion_matrix function is very confusing.

Can someone please help using same code example.

You have 3 labels (say 0,1,2). Let's assume that you have a test set of size 10 and you get the following tensors: truth: [0,0,0,0,1,1,2,2,2,2] prediction: [2,0,0,1,1,1,2,1,2,2] Then you can do as,

>>> import tensorflow as tf
>>> truth = [0,0,0,0,1,1,2,2,2,2]
>>> prediction = [2,0,0,1,1,1,2,1,2,2]
>>> cm = tf.contrib.metrics.confusion_matrix(truth, prediction)
>>> with tf.Session() as sess:
...     sess.run(cm)
... 
array([[2, 1, 1],
       [0, 2, 0],
       [0, 1, 3]], dtype=int32)

Note the following: The result is a 3x3 matrix. The first row says that 2 times label 0 was predicted correctly, once it was mistaken as label 1 and once it was mistaken as label 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM