Estoy trabajando en un problema de tags múltiples y estoy tratando de determinar la precisión de mi modelo.
Mi modelo:
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 )
Quiero calcular la precisión de dos maneras diferentes.
-% de todas las tags que se pronostican correctamente -% de imágenes donde TODAS las tags se pronostican correctamente
desafortunadamente solo puedo calcular el% de todas las tags que se pronostican correctamente.
Pensé que este código calcularía el% de imágenes donde TODAS las tags se pronostican correctamente
correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) ) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
y este código% de todas las tags que se pronostican correctamente
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 ) )
de alguna manera se pierde la coherencia de las tags que pertenecen a una imagen y no estoy seguro de por qué.
Creo que el error en su código está en: correct_prediction = tf.equal( tf.round( pred ), tf.round( y_ ) )
.
pred
debe ser logits sin escala (es decir, sin un sigmoide final).
Aquí desea comparar la salida de sigmoid(pred)
y y_
(ambos en el intervalo [0, 1]
), por lo que debe escribir:
correct_prediction = tf.equal(tf.round(tf.nn.sigmoid(pred)), tf.round(y_))
Luego, para calcular:
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 (ie 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)
referencia: https://gist.github.com/sbrodehl/2120a95d57963a289cc23bcfb24bee1b