How does Keras calculate accuracy from the classwise probabilities? Say, for example we have 100 samples in the test set which can belong to one of two classes. We also have a list of the classwise probabilites. What threshold does Keras use to assign a sample to either of the two classes?
-
$\begingroup$ are you using model.evaluate in keras? $\endgroup$ – Hima Varsha Oct 7 '16 at 8:15
-
$\begingroup$ Yes, I am using model.evaluate. More specifically, model.evaluate_generator. $\endgroup$ – Raghuram Oct 7 '16 at 10:10
-
$\begingroup$ datascience.stackexchange.com/questions/13920/… $\endgroup$ – Hima Varsha Oct 13 '16 at 11:47
-
1$\begingroup$ Possibly related @SO: How does Keras evaluate the accuracy?) $\endgroup$ – desertnaut Jul 3 '18 at 14:52
For binary classification, the code for accuracy metric is:
K.mean(K.equal(y_true, K.round(y_pred)))
which suggests that 0.5 is the threshold to distinguish between classes. y_true should of course be 1-hots in this case.
It's a bit different for categorical classification:
K.mean(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))
which means "how often predictions have maximum in the same spot as true values"
There is also an option for top-k categorical accuracy, which is similar to one above, but calculates how often target class is within the top-k predictions.
-
$\begingroup$ Thank you for the answer. Does that mean even for binary classification, the labels need to be one hot encoded? $\endgroup$ – Raghuram Mar 20 '17 at 5:02
-
$\begingroup$ @Raghuram No, for binary classification you just need 0 or 1 as class, no need to one hot encode them. Since K.mean(K.equal(y_true, K.round(y_pred))) will match 2 float values for each case, so it has to be 0 or 1 and not [0,1],[1,0]. $\endgroup$ – Divyanshu Kalra Jul 4 '17 at 20:13
-
$\begingroup$ For categorical accuracy, use
categorical_accuracy
. $\endgroup$ – Shital Shah Dec 23 '17 at 11:12 -
2$\begingroup$ for a multi-class problem (with more than two classes), is there a difference between using "accuracy" vs "categorical_accuracy" $\endgroup$ – Quetzalcoatl Nov 6 '18 at 20:03
-
2