张量流,多标号精度计算

2024-04-26 03:21:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的模型:

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

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


Tags: 图像reducetftrain标签meannumclasses
2条回答

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

pred应为未标度登录(即没有最终乙状结肠)。

在这里,您需要比较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)
# 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

相关问题 更多 >