How to compute accuracy of CNN in TensorFlow

UPDATE 2020-11-23 Keras in Tensorflow

Now you can just specify you want it in the metrics parameter in model.compile.

This post is from 3.6 years ago when tensorflow was still in version 1. Now that Tensorflow.org suggests using the Keras calls you can specify you want accuracy like so:

model.compile(loss='mse',optimizer='sgd',metrics=['accuracy'])
model.fit(x,y)

BOOM! You've got accuracy in your report when you run "model.fit".

If you are using an older version of tensorflow or just writing it from scratch, @Androbin explains it well.


Just calculate the percentage of correct predictions:

prediction = tf.math.argmax(prob, axis=1)
equality = tf.math.equal(prediction, correct_answer)
accuracy = tf.math.reduce_mean(tf.cast(equality, tf.float32))