MaM*_*eak 18 python tensorflow
我正在研究多标签问题,我正在尝试确定模型的准确性.
我的模特:
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 )
Run Code Online (Sandbox Code Playgroud)
我想以两种不同的方式计算准确度
- 正确预测的所有标签的百分比 - 正确预测所有标签的图像的百分比
不幸的是,我只能计算出正确预测的所有标签的百分比.
我认为这段代码会计算正确预测所有标签的图像百分比
correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Run Code Online (Sandbox Code Playgroud)
并且这个代码是正确预测的所有标签的百分比
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 ) )
Run Code Online (Sandbox Code Playgroud)
某种程度上属于一个图像的标签的一致性丢失了,我不知道为什么.
Oli*_*rot 27
我相信你的代码中的错误是:correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) ).
pred应该是未缩放的logits(即没有最终的sigmoid).
在这里,你要比较的输出sigmoid(pred)和y_(无论是在间隔[0, 1]),所以你必须写:
correct_prediction = tf.equal(tf.round(tf.nn.sigmoid(pred)), tf.round(y_))
Run Code Online (Sandbox Code Playgroud)
然后计算:
accuracy1 = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Run Code Online (Sandbox Code Playgroud)
all_labels_true = tf.reduce_min(tf.cast(correct_prediction), tf.float32), 1)
accuracy2 = tf.reduce_mean(all_labels_true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10703 次 |
| 最近记录: |