Home > Mobile >  Compute accuracy with tensorflow 1
Compute accuracy with tensorflow 1

Time:11-09

Below you can see a code to build a network. With probs = tf.nn.softmax(logits), I am getting probabilities:

def build_network_test(input_images, labels, num_classes):
    logits = embedding_model(input_images, train_phase=True)
    logits = fully_connected(logits, num_classes, activation_fn=None,
                             scope='tmp')

    with tf.variable_scope('loss') as scope:
        with tf.name_scope('soft_loss'):
            softmax = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels))
            probs = tf.nn.softmax(logits)
        scope.reuse_variables()
    with tf.name_scope('acc'):
        accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), labels), tf.float32))

    with tf.name_scope('loss/'):
        tf.summary.scalar('TotalLoss', softmax)

    return logits, softmax, accuracy,probs  # returns total loss

In addition, I am computing accuracy and loss with following code snippet:

for idx in range(num_of_batches):
    batch_images, batch_labels = get_batch(idx, FLAGS.batch_size, mm_labels, mm_data)
    _, summary_str, train_batch_acc, train_batch_loss, probabilities_1 = sess.run(
        [train_op, summary_op, accuracy, total_loss, probs],
        feed_dict={
            input_images: batch_images - mean_data_img_train,
            labels: batch_labels,
        })

    train_acc  = train_batch_acc
    train_loss  = train_batch_loss

train_acc /= num_of_batches
train_acc = train_acc * 100

My question:

I am getting probabilities with two feature values. Afterwards, I am averaging these probabilities with following code

mvalue = np.mean(np.array([probabilities_1, probabilities_2]), axis=0)

Now, I want to compute accuracy on mvalue. Can someone give me pointers on how to do it?

What I had done so far

tmp = tf.argmax(input=mvalue, axis=1)
an_array = tmp.eval(session=tf.compat.v1.Session())

It gives me predicated labels however, I want to have an accuracy value.

CodePudding user response:

Please use accuracy_score from sklearn. Below you can find the code snippet.

    mvalue = np.mean(np.array([probabilities_face, probabilities_voice]), axis=0)
    tmp = tf.argmax(input=mvalue, axis=1)

    an_array = tmp.eval(session=tf.compat.v1.Session())
    bmulti_accuracy = accuracy_score(batch_labels_test, an_array)
  • Related