Computing accuracy

We calculate the accuracy of our model as follows:

  • The y_hat parameter denotes the predicted probability for each class of our model. Since we have 10 classes, we will have 10 probabilities. If the probability is high at position 7, then it means that our network predicts the input image as digit 7 with high probability. The tf.argmax() function returns the index of the largest value. Thus, tf.argmax(y_hat,1) gives the index where the probability is high. Thus, if the probability is high at index 7, then it returns 7.
  • The Y parameter denotes the actual labels, and they are the one-hot encoded values. That is, it consists of zeros everywhere except at the position of the actual image, where it consists of 1. For instance, if the input image is 7, then Y has 0 at all indices except at index 7, where it has 1. Thus, tf.argmax(Y,1) returns 7 because that is where we have a high value, 1.

Thus, tf.argmax(y_hat,1) gives the predicted digit, and tf.argmax(Y,1) gives us the actual digit.

The tf.equal(x, y) function takes x and y as inputs and returns the truth value of (x == y) element-wise. Thus, correct_pred = tf.equal(predicted_digit,actual_digit) consists of True where the actual and predicted digits are the same, and False where the actual and predicted digits are not the same. We convert the Boolean values in correct_pred into float values using TensorFlow's cast operation, tf.cast(correct_pred, tf.float32). After converting them into float values, we take the average using tf.reduce_mean().

Thus, tf.reduce_mean(tf.cast(correct_pred, tf.float32)) gives us the average correct predictions:

with tf.name_scope('Accuracy'):

predicted_digit = tf.argmax(y_hat, 1)
actual_digit = tf.argmax(Y, 1)

correct_pred = tf.equal(predicted_digit,actual_digit)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset