Please explain the working of tf.nn.in_top_k() in this.
with tf.name_scope(“eval”):
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
THANKYOU.
Please explain the working of tf.nn.in_top_k() in this.
with tf.name_scope(“eval”):
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
THANKYOU.
Topk method is used for checking how well a multiclass classification model is performing.
Here is the documentation of this method in TensorFlow 1.x.
tf.nn.in_top_k(
predictions_probabilities,
targets_indexes,
k,
name=None
)
The TensorFlow 2 also has a similar function:
tf.math.in_top_k(
targets, predictions, k, name=None
)
To understand it, tet us take some examples.
Say, there is a neural network model that takes two instances and gives the probabilities of 3 classes i.e. it is multiclass classification. Also such a neural network would be having three neurons in last layer
If our model is predicting probabilities as [[0.5, 0.1, 0.4], [0.1, 0.2, 0.7]], it means it is predicting that the first instance is in class 0 and second instance is in class 2. If our target is [0, 2], it would give true in both cases.
import tensorflow as tf
with tf.Session() as sess:
predicted_probabilities = [[0.5, 0.1, 0.4], [0.1, 0.2, 0.7]]
target_classes_index = [0, 2]
print(sess.run(tf.nn.in_top_k(
predicted_probabilities,
target_classes_index,
1)))
[ True True]
If our target was [1, 2], the result would be [false, true]
import tensorflow as tf
with tf.Session() as sess:
predicted_probabilities = [[0.5, 0.1, 0.4], [0.1, 0.2, 0.7]]
target_classes_index = [1, 2]
print(sess.run(tf.nn.in_top_k(
predicted_probabilities,
target_classes_index,
1)))
[False True]
Now, coming to k
- the third argument of the method. It means that in how many top probabilities to check our target class. So, far we checked if our target existed in top 1.
Say, our predicted probabilities are [[0.3, 0.1, 0.1, 0.19, 0.31]], the top two predicted classes are 0 and 4. So, if our target is 0. the in_top_k would return true for k = 2 and it would return false for k = 1
import tensorflow as tf
with tf.Session() as sess:
predicted_probabilities = [[0.3, 0.1, 0.1, 0.19, 0.31]]
target_classes_index = [0]
print(sess.run(tf.nn.in_top_k(
predicted_probabilities,
target_classes_index,
2)))
[ True]
import tensorflow as tf
with tf.Session() as sess:
predicted_probabilities = [[0.3, 0.1, 0.1, 0.19, 0.31]]
target_classes_index = [0]
print(sess.run(tf.nn.in_top_k(
predicted_probabilities,
target_classes_index,
1)))
[False]