簡體   English   中英

用於多類分類的Tensorflow混淆矩陣

[英]Tensorflow confusion matrix for multiclass classification

謝謝你的幫助。 我正在為面部動作(例如眉毛升高,嘴唇張開)編碼一個多類二進制分類器,並且我想創建一個混淆矩陣。 有6個面部動作和593個樣本。 我收到此錯誤:我收到此錯誤:“形狀(?,2,6)必須具有等級2”。 從文檔來看,tf.confusion_matrix采用一維矢量,但是我認為應該有一種方法可以基於TensorBoard中的Tensorflow混淆矩陣來調整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)

並查看此詳細說明: 使用一鍵編碼的Tensorflow混淆矩陣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM