akshata bhat
akshata bhat

Reputation: 1103

How to compute accuracy of CNN in TensorFlow

I am new to TensorFlow. I am doing a binary classification with my own dataset. However I do not know how to compute the accuracy. Can anyone please help me with to do this?

My classifier has 5 convolutional layers followed by 2 fully connected layers. The final FC layer has an output dimension of 2 for which I have used:

prob = tf.nn.softmax(classification_features, name="output")

Upvotes: 10

Views: 16254

Answers (2)

Anton Panchishin
Anton Panchishin

Reputation: 3763

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.

Upvotes: 1

Robin Richtsfeld
Robin Richtsfeld

Reputation: 1036

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))

Upvotes: 20

Related Questions