Tensorflow,多标签精度计算 [英] Tensorflow, multi label accuracy calculation
问题描述
我正在处理一个多标签问题,我正在尝试确定我的模型的准确性.
I am working on a multi label problem and i am trying to determine the accuracy of my model.
我的模型:
NUM_CLASSES = 361
x = tf.placeholder(tf.float32, [None, IMAGE_PIXELS])
y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES])
# create the network
pred = conv_net( x )
# loss
cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( pred, y_) )
# train step
train_step = tf.train.AdamOptimizer().minimize( cost )
我想以两种不同的方式计算准确度
- 正确预测的所有标签的百分比- 正确预测所有标签的图像百分比
i want to calculate the accuracy in two different ways
- % of all labels that are predicted correctly
- % of images where ALL labels are predicted correctly
不幸的是,我只能计算正确预测的所有标签的百分比.
unfortunately i am only able to calculate the % of all labels that are predicted correctly.
我认为这段代码会计算正确预测所有标签的图像百分比
I thought this code would calculate % of images where ALL labels are predicted correctly
correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
以及此代码占正确预测的所有标签的百分比
and this code % of all labels that are predicted correctly
pred_reshape = tf.reshape( pred, [ BATCH_SIZE * NUM_CLASSES, 1 ] )
y_reshape = tf.reshape( y_, [ BATCH_SIZE * NUM_CLASSES, 1 ] )
correct_prediction_all = tf.equal( tf.round( pred_reshape ), tf.round( y_reshape ) )
accuracy_all = tf.reduce_mean( tf.cast(correct_prediction_all, tf.float32 ) )
不知何故,属于一张图像的标签的一致性丢失了,我不知道为什么.
somehow the coherency of the labels belonging to one image is lost and i am not sure why.
推荐答案
我相信您代码中的错误在于:correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )代码>.
I believe the bug in your code is in: correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )
.
pred
应该是未缩放的 logits(即没有最终的 sigmoid).
pred
should be unscaled logits (i.e. without a final sigmoid).
这里你想比较 sigmoid(pred)
和 y_
的输出(都在区间 [0, 1]
中)所以你必须写:
Here you want to compare the output of sigmoid(pred)
and y_
(both in the interval [0, 1]
) so you have to write:
correct_prediction = tf.equal(tf.round(tf.nn.sigmoid(pred)), tf.round(y_))
<小时>
然后计算:
Then to compute:
- 所有标签的平均准确率:
accuracy1 = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
- 所有标签都需要正确的准确性:
all_labels_true = tf.reduce_min(tf.cast(correct_prediction), tf.float32), 1)
accuracy2 = tf.reduce_mean(all_labels_true)
这篇关于Tensorflow,多标签精度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!