简体   繁体   English

Tensorflow:如何创建混淆矩阵

[英]Tensorflow: How to create confusion matrix

I am new to tensorflow, I used this tutorial: 我是tensorflow的新手,我使用了本教程:

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

I have trained the same model on new dataset which contains 3 labels. 我已经在包含3个标签的新数据集上训练了相同的模型。 I am trying to create the confusion matrix. 我正在尝试创建混淆矩阵。

tf.confusion_matrix function is very confusing. tf.confusion_matrix函数非常令人困惑。

Can someone please help using same code example. 有人可以帮忙使用相同的代码示例。

You have 3 labels (say 0,1,2). 您有3个标签(例如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, 假设您有一个大小为10的测试集,并且得到以下张量:真值:[0,0,0,0,1,1,2,2,2,2,2]预测:[2,0,0, 1,1,1,2,1,2,2]然后您可以按照

>>> 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. 请注意以下几点:结果是3x3矩阵。 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. 第一行说正确地预测了标签0的2次,一次被误认为标签1,一次被误认为标签2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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