Top5 vs Top1 accuracy

Get FREE domain for 1st year and build your brand new site

Free book on Graph Algorithms

In this article, we have explored the differences between Top5 and Top1 accuracy measurements. Both are frequently used with Machine Learning models.

Top5 vs Top1 accuracy

Following table summarizes the differences between Top5 and Top1 accuracy:

Top5 vs Top1 accuracy
PointTop5Top1
Higher valueHighLow
# of predictions
considered
51
Strict checkLess strictMore strict

Following is a code snippet in TensorFlow Python which calculates both Top1 and Top5 accuracy which will show that calculating both is similar with minor change:

predictions = sess_graph.run(output_tensor,
                     {input_tensor: np_images})
elapsed_time = time.time() - start_time
accuracy1 = tf.reduce_sum(
  input_tensor=tf.cast(tf.nn.in_top_k(predictions=tf.constant(predictions),
      targets=tf.constant(np_labels), k=1), tf.float32))

accuracy5 = tf.reduce_sum(
  input_tensor=tf.cast(tf.nn.in_top_k(predictions=tf.constant(predictions),
      targets=tf.constant(np_labels), k=5), tf.float32))
np_accuracy1, np_accuracy5 =  sess.run([accuracy1, accuracy5])
total_accuracy1 += np_accuracy1
total_accuracy5 += np_accuracy5
print("Iteration time: %0.4f ms" % elapsed_time)
print("Processed %d images. (Top1 accuracy, Top5 accuracy) = (%0.4f, %0.4f)" \
  % (num_processed_images, total_accuracy1/num_processed_images,
  total_accuracy5/num_processed_images))

With this article at OpenGenus, you must have the complete idea of the differences between Top1 and Top5 accuracy.