如何在张量流中创建用于分类的混淆矩阵 [英] how to create confusion matrix for classification in tensorflow

查看:40
本文介绍了如何在张量流中创建用于分类的混淆矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有 4 个输出节点的 CNN 模型,我正在尝试计算混淆矩阵,以便我可以知道单个类的准确性.我能够计算整体精度.在链接 这里,Igor Valantic 给出了一个计算混淆矩阵变量的函数.它给了我一个错误 correct_prediction = tf.nn.in_top_k(logits, labels, 1, name="correct_answers") 并且错误是 TypeError: DataType float32 for attr 'T' not在允许值列表中:int32、int64

I have CNN model which has 4 output nodes, and I am trying to compute the confusion matrix so that i can know the individual class accuracy. I am able to compute the overall accuracy. In the link here, Igor Valantic gave a function which can compute the confusion matrix variables. it gives me an error at correct_prediction = tf.nn.in_top_k(logits, labels, 1, name="correct_answers") and the error is TypeError: DataType float32 for attr 'T' not in list of allowed values: int32, int64

我尝试将 logits 类型转换到 int32 中提到的 def 评估(logits,labels) 函数中,它在将 correct_prediction = ... 计算为 时给出了另一个错误类型错误:InTopK"操作的输入预测"的类型为 int32,与预期的 float32 类型不匹配

I have tried typecasting logits to int32 inside function mentioned def evaluation(logits, labels), it gives another error at computing correct_prediction = ... as TypeError:Input 'predictions' of 'InTopK' Op has type int32 that does not match expected type of float32

如何计算这个混淆矩阵?

how to calculate this confusion matrix ?

sess = tf.Session()
model = dimensions() # CNN input weights are calculated 
data_train, data_test, label_train, label_test =  load_data(files_test2,folder)
data_train, data_test, = reshapedata(data_train, data_test, model)
# input output placeholders
x  = tf.placeholder(tf.float32, [model.BATCH_SIZE, model.input_width,model.input_height,model.input_depth]) # last column = 1 
y_ = tf.placeholder(tf.float32, [model.BATCH_SIZE, model.No_Classes])
p_keep_conv = tf.placeholder("float")
# 
y  = mycnn(x,model, p_keep_conv)
# loss
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
# train step
train_step = tf.train.AdamOptimizer(1e-3).minimize(cost)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
true_positives, false_positives, true_negatives, false_negatives = evaluation(y,y_)
lossfun = np.zeros(STEPS)
sess.run(tf.global_variables_initializer())

for i in range(STEPS):
    image_batch, label_batch = batchdata(data_train, label_train, model.BATCH_SIZE)
    epoch_loss = 0
    for j in range(model.BATCH_SIZE):
        sess.run(train_step, feed_dict={x: image_batch, y_: label_batch, p_keep_conv:1.0})
        c = sess.run( cost, feed_dict={x: image_batch, y_: label_batch, p_keep_conv: 1.0})
        epoch_loss += c
    lossfun[i] = epoch_loss
    print('Epoch',i,'completed out of',STEPS,'loss:',epoch_loss )
 TP,FP,TN,FN = sess.run([true_positives, false_positives, true_negatives,  false_negatives], feed_dict={x: image_batch, y_: label_batch, p_keep_conv:1.0})

这是我的代码片段

推荐答案

你可以简单地使用 Tensorflow 的 混淆矩阵.我假设 y 是你的预测,你可能有也可能没有 num_classes(这是可选的)

You can simply use Tensorflow's confusion matrix. I assume y are your predictions, and you may or may not have num_classes (which is optional)

y_ = placeholder_for_labels # for eg: [1, 2, 4]
y = mycnn(...) # for eg: [2, 2, 4]

confusion = tf.confusion_matrix(labels=y_, predictions=y, num_classes=num_classes)

如果你打印(混淆),你得到

  [[0 0 0 0 0]
   [0 0 1 0 0]
   [0 0 1 0 0]
   [0 0 0 0 0]
   [0 0 0 0 1]]

如果print(confusion) 没有打印混淆矩阵,则使用print(confusion.eval(session=sess)).这里 sess 是您的 TensorFlow 会话的名称.

If print(confusion) is not printing the confusion matrix, then use print(confusion.eval(session=sess)). Here sess is the name of your TensorFlow session.

这篇关于如何在张量流中创建用于分类的混淆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆