每个标签/混淆矩阵的输出错误率 [英] Output error rate per label / confusion matrix
问题描述
我使用Keras训练图像分类器,测试精度高达98%左右.现在,我知道总体准确性为98%,但是我想知道每个不同类别/标签的准确性/错误.
I train an image classifier using Keras up to around 98% test accuracy. Now I know that the overall accuracy is 98%, but i want to know the accuracy/error per distinct class/label.
Keras是否为此提供了内置功能,还是我必须针对每个班级/标签对此进行自我测试?
Has Keras a builtin function for that or would I have to test this myself per class/label?
更新:谢谢@gionni.我不知道实际的用语是混淆矩阵".但这就是我真正想要的.话虽这么说,有一个函数可以生成一个吗?顺便说一下,我必须使用Keras 1.2.2.
Update: Thanks @gionni. I didn't know the actual term was "Confusion Matrix". But that's what I am actually looking for. That being said, is there a function to generate one? I have to use Keras 1.2.2 by the way.
推荐答案
我遇到了类似的问题,因此可以与您共享我的代码.以下函数计算单个类别的准确性:
I had similar issue so I could share my code with you. The following function computes a single class accuracy:
def single_class_accuracy(interesting_class_id):
def fn(y_true, y_pred):
class_id_preds = K.argmax(y_pred, axis=-1)
# Replace class_id_preds with class_id_true for recall here
positive_mask = K.cast(K.equal(class_id_preds, interesting_class_id), 'int32')
true_mask = K.cast(K.equal(y_true, interesting_class_id), 'int32')
acc_mask = K.cast(K.equal(positive_mask, true_mask), 'float32')
class_acc = K.mean(acc_mask)
return class_acc
return fn
现在-如果要获得0
类的准确性,可以在编译模型时将其添加到度量标准:
Now - if you want to get an accuracy for 0
class you could add it to metrics while compiling a model:
model.compile(..., metrics=[..., single_class_accuracy(0)])
如果您想获得所有班级的准确性,可以输入:
If you want to have all classes accuracy you could type:
model.compile(...,
metrics=[...] + [single_class_accuracy(i) for i in range(nb_of_classes)])
这篇关于每个标签/混淆矩阵的输出错误率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!