Python Tensorflow,多标签精度计算

Python Tensorflow,多标签精度计算,python,tensorflow,Python,Tensorflow,我正在处理一个多标签问题,并试图确定我的模型的准确性 我的模型: NUM_CLASSES=361 x=tf.placeholder(tf.float32,[无,图像像素]) y=tf.placeholder(tf.float32,[None,NUM\u类]) #创建网络 pred=转换网络(x) #损失 成本=tf.reduce\u均值(tf.nn.sigmoid\u交叉\u熵\u与logits(pred,y)) #火车站 训练步骤=tf.train.AdamOptimizer()。最小化(成本

我正在处理一个多标签问题,并试图确定我的模型的准确性

我的模型:

NUM_CLASSES=361
x=tf.placeholder(tf.float32,[无,图像像素])
y=tf.placeholder(tf.float32,[None,NUM\u类])
#创建网络
pred=转换网络(x)
#损失
成本=tf.reduce\u均值(tf.nn.sigmoid\u交叉\u熵\u与logits(pred,y))
#火车站
训练步骤=tf.train.AdamOptimizer()。最小化(成本)
我想用两种不同的方法计算精度
-正确预测的所有标签的百分比 -正确预测所有标签的图像百分比

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

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

correct_prediction=tf.equal(tf.round(pred),tf.round(y)))
准确度=tf.reduce_平均值(tf.cast(正确的预测,tf.float32))
此代码占正确预测的所有标签的%

pred_restrape=tf.restrape(pred,[BATCH_SIZE*NUM_CLASSES,1])
y_整形=tf.reforme(y_,[BATCH_SIZE*NUM_CLASSES,1])
正确的预测值=tf.equal(tf.round(pred_整形)、tf.round(y_整形))
准确度=tf.reduce平均值(tf.cast(correct\u prediction\u all,tf.float32))

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

我相信代码中的错误在于:
correct\u prediction=tf.equal(tf.round(pred),tf.round(y))

pred
应该是无标度logits(即没有最终的乙状结肠)

在这里,您要比较
sigmoid(pred)
y
(两者都在
[0,1]
的间隔内)的输出,因此您必须编写:

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

然后计算:

  • 所有标签的平均精度:
accuracy1=tf.reduce\u平均值(tf.cast(correct\u prediction,tf.float32))
  • 所有标签需要正确的准确度:
all\u labels\u true=tf.reduce\u min(tf.cast(correct\u prediction),tf.float32),1)
精度2=tf.减少平均值(所有标签均为真)

参考资料:

Thank的工作原理很有魅力,我是否正确理解reduce\u min将一张图像的所有标签打包在一起?它将对批次中的每个元素进行最低限度的正确预测。如果所有元素均为1(即所有预测均正确),则最小值为1;如果至少有一个元素为假(且等于0),则最小值为0。我尝试了这种方法,但每个历元的精度值相同:
列车精度:0.984375测试精度:0.984375
。知道为什么会这样吗?回答得很好,有一点困惑:多标签分类的成本/损失函数是什么?如果可以的话,请在回答中加上它。@AyodhyankitPaul,MaMiFrea的成本函数适用于多标签分类:成本=tf.reduce\u均值(tf.nn.sigmoid\u cross\u entropy\u with_logits(pred,y))
# 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)