【问题标题】:Tensorflow, multi label accuracy calculationTensorflow,多标签准确率计算
【发布时间】:2016-06-10 11:06:38
【问题描述】:

我正在研究一个多标签问题,我正在尝试确定我的模型的准确性。

我的模特:

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 )

我想用两种不同的方式计算准确度
- 正确预测的所有标签的百分比 - 正确预测所有标签的图像百分比

不幸的是,我只能计算正确预测的所有标签的百分比。

我认为这段代码会计算所有标签都被正确预测的图像的百分比

correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

这个代码占所有标签正确预测的百分比

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

不知何故,属于一个图像的标签的一致性丢失了,我不知道为什么。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    我相信您代码中的错误在:correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )

    pred 应该是unscaled logits(即没有最终的 sigmoid)。

    这里你想比较sigmoid(pred)y_的输出(都在区间[0, 1])所以你必须写:

    correct_prediction = tf.equal(tf.round(tf.nn.sigmoid(pred)), tf.round(y_))
    

    然后计算:

    • 所有标签的平均准确度:
    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)
    

    【讨论】:

    • 感谢就像一个魅力,我是否正确理解 reduce_min 将一张图像的所有标签打包在一起?
    • 对于批次的每个元素将采用正确预测的最小值。如果所有元素为 1(即所有预测都正确),则此最小值为 1;如果至少一个元素为 False(且等于 0),则此最小值为 0
    • 我尝试了这种方法,但我得到的每个时期的准确度值都相同:Train accuracy: 0.984375 Test accuracy: 0.984375。知道为什么会这样吗? stackoverflow.com/questions/49210520/…
    • 很好的答案,一个困惑多标签分类的成本/损失函数是什么?如果可以,请在答案中添加。
    • @AyodhyankitPaul,MaMiFrea 的成本函数适用于多标签分类:cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( pred, y_) )
    【解决方案2】:
    # to get the mean accuracy over all labels, prediction_tensor are scaled logits (i.e. with final sigmoid layer)
    correct_prediction = tf.equal( tf.round( prediction_tensor ), tf.round( ground_truth_tensor ) )
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    # to get the mean accuracy where all labels need to be correct
    all_labels_true = tf.reduce_min(tf.cast(correct_prediction, tf.float32), 1)
    accuracy2 = tf.reduce_mean(all_labels_true)
    

    参考:https://gist.github.com/sbrodehl/2120a95d57963a289cc23bcfb24bee1b

    【讨论】:

      猜你喜欢
      • 2019-03-23
      • 2018-11-14
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      相关资源
      最近更新 更多