Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 758
akshata bhat Avatar asked Mar 05 '17 11:03

akshata bhat


People also ask

How is CNN model accuracy calculated?

If the model made a total of 530/550 correct predictions for the Positive class, compared to just 5/50 for the Negative class, then the total accuracy is (530 + 5) / 600 = 0.8917 . This means the model is 89.17% accurate.

How is accuracy calculated TensorFlow?

Class Accuracy Defined in tensorflow/python/keras/metrics.py. Calculates how often predictions matches labels. For example, if y_true is [1, 2, 3, 4] and y_pred is [0, 2, 3, 4] then the accuracy is 3/4 or . 75.

What does accuracy mean in CNN?

Accuracy = Number of correct predictions Total number of predictions.

How accuracy is calculated in keras?

Accuracy calculates the percentage of predicted values (yPred) that match with actual values (yTrue). For a record, if the predicted value is equal to the actual value, it is considered accurate. We then calculate Accuracy by dividing the number of accurately predicted records by the total number of records.


2 Answers

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))
like image 163
Androbin Avatar answered Oct 25 '22 01:10

Androbin


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.

like image 38
Anton Codes Avatar answered Oct 25 '22 00:10

Anton Codes