Search code examples
machine-learningtensorflowdeep-learningmetrics

TensorFlow: Is there a metric to calculate and update top k accuracy?


The current tf.contrib.metrics.streaming_accuracy is only able to calculate the top 1 accuracy, and not the top k. As a workaround, this is what I've been using:

tf.reduce_mean(tf.cast(tf.nn.in_top_k(predictions=predictions, targets=labels, k=5), tf.float32))

However, this does not give me a way to calculate the streaming accuracies averaged across each batch, which would be useful in getting a stable evaluation accuracy. I am currently manually calculating this streaming top 5 accuracy through using its numpy output, but this means I won't be able to visualize this metric on tensorboard.

Is there a way to have a simpler implementation by creating an accuracy_update function, or is there an existing function that already does this?

Thank you.


Solution

  • You could replace your use of tf.contrib.metrics.streaming_accuracy by the lower-level tf.metrics.mean, which is by the way ultimately used by streaming_accuracy -- you will find a similarity in their respective documentations.

    E.g. (not tested)

    tf.metrics.mean(tf.nn.in_top_k(predictions=predictions, targets=labels, k=5))