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



感谢您的帮助。我正在为面部动作(例如扬眉毛、分开的嘴唇(编写一个多类二元分类器,我想制作一个混淆矩阵。有6个面部动作和593个样本。我收到此错误:我收到此错误:"形状 (?, 2, 6( 必须具有等级 2"。从文档中看,tf.confusion_matrix采用一维向量,但我认为应该有一种方法可以塑造来自feed_dict的输入数据,以便它基于TensorBoard中的Tensorflow Confusion Matrix工作。标签和预测如下所示:

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

并查看此详细说明: 使用一热代码的张量流混淆矩阵

相关内容

  • 没有找到相关文章

最新更新