如何在 Keras 中输出 per-class 的准确率? [英] How to output per-class accuracy in Keras?
问题描述
Caffe 不仅可以打印整体准确度,还可以打印每类准确度.
Caffe can not only print overall accuracy, but also per-class accuracy.
在 Keras 日志中,只有整体准确性.我很难计算单独的类别准确率.
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
有人知道如何在 keras 中输出每类的准确率吗?
Anybody who knows how to output per-class accuracy in keras?
推荐答案
Precision &召回是多类分类更有用的度量(参见定义).遵循 Keras MNIST CNN 示例(10 级分类),你可以使用 classification_report
从 sklearn.metrics:
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))
结果如下:
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
这篇关于如何在 Keras 中输出 per-class 的准确率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!