Python 用于多类分类的张量流混淆矩阵

Python 用于多类分类的张量流混淆矩阵,python,tensorflow,confusion-matrix,multiclass-classification,Python,Tensorflow,Confusion Matrix,Multiclass Classification,谢谢你的帮助。我正在为面部动作(如扬眉、分开的嘴唇)编写一个多类二进制分类器,我想制作一个混淆矩阵。共有6个面部动作和593个样本。我得到了这个错误:我得到了这个错误:“形状(?,2,6)必须有等级2”。从文档中可以看出,tf.conflusion_矩阵采用一维向量,但我认为应该有一种方法来对feed_dict中的输入数据进行整形,使其能够工作,基于。标签和预测如下所示: # Rows are samples, columns are classes, and the classes shows

谢谢你的帮助。我正在为面部动作(如扬眉、分开的嘴唇)编写一个多类二进制分类器,我想制作一个混淆矩阵。共有6个面部动作和593个样本。我得到了这个错误:我得到了这个错误:“形状(?,2,6)必须有等级2”。从文档中可以看出,tf.conflusion_矩阵采用一维向量,但我认为应该有一种方法来对feed_dict中的输入数据进行整形,使其能够工作,基于。标签和预测如下所示:

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