【问题标题】:Viewing wrong predictions in tensorflow在张量流中查看错误的预测
【发布时间】:2020-11-21 07:22:43
【问题描述】:

我有四个类(标记为 0、1、2、3)的问题。我有一个训练和评估的模型。我什至有一个混淆矩阵。但是我想看看被错误分类的图像样本,主要是FP和FN,这样我就知道哪个样本模型在分类上有问题。

任何建议都会有很大帮助。

【问题讨论】:

    标签: python tensorflow keras deep-learning conv-neural-network


    【解决方案1】:

    In this stackoverflow answer 我展示了一种处理二进制大小写的方法。我对您的问题的想法是找到每个班级各自的 FP 和 FN。所以我想做的是

    1. 对于每个类 cls
      1. 将所有 cls 的标签设置为 1
      2. 将所有非cls标签设置为0
      3. 现在我们已将问题转化为对应于 cls 的“二元分类”
        • 获取 FP 和 FN(参见链接的 SO 答案)

    代码:

    import numpy as np 
    
    real = np.array([1,1,1,2,2,2,0,0,0,3,3,3])
    predicted = np.array([1,1,0,2,2,1,0,0,2,3,3,1])
    
    classes = np.array([0,1,2,3])
    
    def get_dummy_values(a: np.ndarray, cls: int):
        temp = a.copy()
        mask = (temp == cls)
        temp[mask] = 1
        temp[~mask] = 0
        return temp
    
    diffs = np.empty((len(classes), len(real)), dtype=np.int)
    for i, cls in enumerate(classes):
        dum_real = get_dummy_values(real, cls)
        dum_pred = get_dummy_values(predicted, cls)
        
        diffs[i] = dum_real - dum_pred
    
    print(f'{f" Diffs ":=^40}')
    print(diffs, end='\n\n')
    
    for diff, cls in zip(diffs, classes):
        print(f'{f" For class {cls} ":=^40}')
        print('False positives: ', np.where(diff == -1)[0])
        print('False negatives: ', np.where(diff == 1)[0], end='\n\n')
    

    输出:

    ================ Diffs =================
    [[ 0  0 -1  0  0  0  0  0  1  0  0  0]
     [ 0  0  1  0  0 -1  0  0  0  0  0 -1]
     [ 0  0  0  0  0  1  0  0 -1  0  0  0]
     [ 0  0  0  0  0  0  0  0  0  0  0  1]]
    
    ============= For class 0 ==============
    False positives:  [2]
    False negatives:  [8]
    
    ============= For class 1 ==============
    False positives:  [ 5 11]
    False negatives:  [2]
    
    ============= For class 2 ==============
    False positives:  [8]
    False negatives:  [5]
    
    ============= For class 3 ==============
    False positives:  []
    False negatives:  [11]
    

    注意:假设您的模型预测“大部分正确”,则 diffs 矩阵非常稀疏,因此最好为该任务使用内存效率更高的数据结构(以防您有很多标签)。

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2017-05-15
      • 1970-01-01
      • 2017-11-22
      • 2017-04-28
      • 2018-01-10
      • 1970-01-01
      • 2017-08-16
      • 2018-06-21
      相关资源
      最近更新 更多