用于多类分类的 Tensorflow 混淆矩阵

Tensorflow confusion matrix for multiclass classification

感谢您的帮助。我正在为面部动作(例如扬起的眉毛、分开的嘴唇)编写一个多类二元分类器,我想制作一个混淆矩阵。有6个面部动作和593个样本。我收到此错误: 我收到此错误:"Shape (?, 2, 6) must have rank 2"。从文档中,tf.confusion_matrix 采用一维向量,但我认为应该有一种方法可以根据 feed_dict 对输入数据进行整形,使其工作,基于 Tensorflow Confusion Matrix in TensorBoard。标签和预测如下所示:

# Rows are samples, columns are classes, and the classes shows a facial
# action which is either 1 for detection or 0 for no detection. 
[[0, 0, 1, 0, 1, 0],
[1, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 1],...]

我正在使用前馈 MLP,变量 'pred' 是预测,阈值强制选择 0 或 1。我尝试将预测和标签乘以 np.arange( 1,7) 使正值与索引匹配,但我被困在参数的形状上。

还有更多代码,但我展示的是我认为相关的内容。

sess = tf.Session()

x = tf.placeholder(tf.float32, [None, n_input], name = "x")
y = tf.placeholder(tf.float32, [None, n_output], name = "labels")

#2 fully connected layers
fc1 = fc_layer(x, n_input, n_hidden_1, "fc1")
relu = tf.nn.relu(fc1)
tf.summary.histogram("fc1/relu", relu)
logits = fc_layer(relu, n_hidden_1, n_output, "fc2")

# Calculate loss function
with tf.name_scope("xent"):
    xent = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(
            logits=logits, labels=y, name="xent"))

with tf.name_scope("train"):
    train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)


# Choose between 0 and 1
onesMat = tf.ones_like(logits)
zerosMat = tf.zeros_like(logits)   
pred = tf.cast(tf.where(logits>=zero,onesMat,zerosMat),dtype=tf.float32, name = "op_to_restore")

# Problem occurs when I add this line. 
confusion = tf.confusion_matrix(predictions = pred*np.arange(1,7), labels = y*np.arange(1,7), num_classes = n_output, name = "confusion")

# Save and visualize results
saver = tf.train.Saver()
init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init)

writer = tf.summary.FileWriter(LOGDIR + hparam + '/train')
writer.add_graph(sess.graph)


# Train
for i in range(2001):
    if i % 5 == 0:
      [train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: train_x, y: train_y})
      writer.add_summary(s, i)
    if i % 50 == 0:
      [acc,s] = sess.run([accuracy, summ],feed_dict={x: test_x, y: test_y})
    sess.run(train_step, feed_dict={x: train_x, y: train_y})

谢谢!

我遇到了和你一样的问题。我使用 argmax 函数 解决了我的问题。

尝试这段代码(或类似代码):

cm = tf.confusion_matrix(labels=tf.argmax(y*np.arange(1,7), 1), predictions=tf.argmax(pred*np.arange(1,7)))

#then check the result:
with tf.Session() as sess:
    cm_reachable = cm.eval()
    print(cm_reachable)

并查看此详细说明: Tensorflow confusion matrix using one-hot code