Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output per-class accuracy in Keras?

Tags:

Caffe can not only print overall accuracy, but also per-class accuracy.

In Keras log, there's only overall accuracy. It's hard for me to calculate the separate class accuracy.

Epoch 168/200  0s - loss: 0.0495 - acc: 0.9818 - val_loss: 0.0519 - val_acc: 0.9796  Epoch 169/200  0s - loss: 0.0519 - acc: 0.9796 - val_loss: 0.0496 - val_acc: 0.9815  Epoch 170/200  0s - loss: 0.0496 - acc: 0.9815 - val_loss: 0.0514 - val_acc: 0.9801 

Anybody who knows how to output per-class accuracy in keras?

like image 819
spider Avatar asked Aug 29 '17 04:08

spider


People also ask

How is accuracy defined in keras?

Accuracy(name="accuracy", dtype=None) Calculates how often predictions equal labels. This metric creates two local variables, total and count that are used to compute the frequency with which y_pred matches y_true .

How do you check accuracy in Tensorflow?

metrics. accuracy and pass it to eval_metric_ops that will be returned by the function. Then the output of estimator. evaluate() will contain an accuracy key that will hold the accuracy computed on the validation set.

What is the accuracy metric?

Accuracy is a metric that generally describes how the model performs across all classes. It is useful when all classes are of equal importance. It is calculated as the ratio between the number of correct predictions to the total number of predictions.


1 Answers

Precision & recall are more useful measures for multi-class classification (see definitions). Following the Keras MNIST CNN example (10-class classification), you can get the per-class measures using classification_report from sklearn.metrics:

from sklearn.metrics import classification_report import numpy as np  Y_test = np.argmax(y_test, axis=1) # Convert one-hot to index y_pred = model.predict_classes(x_test) print(classification_report(Y_test, y_pred)) 

Here is the result:

         precision    recall  f1-score   support        0       0.99      1.00      1.00       980       1       0.99      0.99      0.99      1135       2       1.00      0.99      0.99      1032       3       0.99      0.99      0.99      1010       4       0.98      1.00      0.99       982       5       0.99      0.99      0.99       892       6       1.00      0.99      0.99       958       7       0.97      1.00      0.99      1028       8       0.99      0.99      0.99       974       9       0.99      0.98      0.99      1009  avg / total   0.99      0.99      0.99     10000 
like image 107
desertnaut Avatar answered Dec 06 '22 19:12

desertnaut