gpt4 book ai didi

python - 如何创建 tensorflow 分类的混淆矩阵

转载 作者:行者123 更新时间:2023-12-02 16:10:33 24 4
gpt4 key购买 nike

我有 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在允许值列表中:int32、int64

我尝试在提到的函数内将 logits 类型转换为 int32 def evaluation(logits, labels),它在计算 Correct_prediction = ... 时出现另一个错误 TypeError:“InTopK”Op 的输入“预测”的类型为 int32,与 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})

这是我的代码片段

最佳答案

您可以简单地使用 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 session 的名称。

关于python - 如何创建 tensorflow 分类的混淆矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43051687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com