【问题标题】:how to create confusion matrix for classification in tensorflow如何在张量流中为分类创建混淆矩阵
【发布时间】:2017-08-20 11:20:12
【问题描述】:

我有 CNN 模型,它有 4 个输出节点,我正在尝试计算混淆矩阵,以便我可以知道各个类的准确度。我能够计算整体准确性。 在链接here 中,Igor Valantic 给出了一个可以计算混淆矩阵变量的函数。 它在correct_prediction = tf.nn.in_top_k(logits, labels, 1, name="correct_answers") 给我一个错误,错误是TypeError: DataType float32 for attr 'T' not in list of allowed values: int32, int64

我已经尝试将 logits 类型转换为 int32 内部提到的函数 def evaluation(logits, labels),它在计算 correct_prediction = ... 时给出了另一个错误 TypeError:Input 'predictions' of 'InTopK' Op has type int32 that does not match expected type of float32

如何计算这个混淆矩阵?

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})

这是我的代码 sn-p

【问题讨论】:

标签: python tensorflow confusion-matrix


【解决方案1】:

您可以简单地使用 Tensorflow 的 confusion matrix。我假设y 是您的预测,您可能有也可能没有num_classes(这是可选的)

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)

如果你print(confusion),你会得到

  [[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 会话的名称。

【讨论】:

  • 对于 Tensorflow
  • 它不会为我打印混淆矩阵 - 以下import tensorflow as tf y_ = [1, 2, 4] y = [2, 2, 4] num_classes = 3 cm = tf.confusion_matrix(labels=y_, predictions=y, num_classes=num_classes) print(cm) 会打印出Tensor("confusion_matrix_5/SparseTensorDenseAdd:0", shape=(3, 3), dtype=int32)
  • 使用sess = tf.Session() with sess.as_default(): print(cm.eval())正确打印。
  • 张量流 2.2.0:tf.math.confusion_matrix()
  • 谢谢我不知名的恩人! (在我的情况下,它没有打印矩阵。)
【解决方案2】:
import tensorflow as tf     
y = [1, 2, 4]
y_ = [2, 2, 4]

con = tf.confusion_matrix(labels=y_, predictions=y )
sess = tf.Session()
with sess.as_default():
        print(sess.run(con))

输出是:

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

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 2018-03-18
    • 2021-03-02
    • 2018-03-01
    • 1970-01-01
    • 2017-02-01
    • 2021-10-30
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多