【问题标题】:How to create weighted cross entropy loss?如何创建加权交叉熵损失?
【发布时间】:2021-02-03 16:38:32
【问题描述】:

我必须处理高度不平衡的数据。据我了解,我需要使用加权交叉熵损失。

我试过了:

import tensorflow as tf

weights = np.array([<values>])
def loss(y_true, y_pred):
    # weights.shape = (63,)
    # y_true.shape = (64, 63)
    # y_pred.shape = (64, 63)
    return tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(y_true, y_pred, weights))

model.compile('adam', loss=loss, metrics=['acc'])

但是有一个错误:

ValueError: Creating variables on a non-first call to a function decorated with tf.function

我怎样才能造成这种损失?

【问题讨论】:

    标签: python tensorflow keras loss-function cross-entropy


    【解决方案1】:

    我建议首先使用 Keras 的 class_weight

    class_weight
    

    是带有{label:weight}的字典

    例如,如果标签 1 中的示例比标签 0 中的示例多 20 倍,那么您可以编写

    # Assign 20 times more weight to label 0
    model.fit(..., class_weight = {0:20, 1:0})
    

    通过这种方式,您无需担心自己实施加权 CCE。

    附加说明:在您的model.compile() 中不要忘记使用weighted_metrics=['accuracy'] 以反映您的准确性。

    model.fit(..., class_weight = {0:20, 1:0}, weighted_metrics = ['accuracy'])
    

    【讨论】:

    • 我猜,如果类 1 出现的次数多 20 次,你应该使用{0: 20, 1: 1}
    • 是的,我这边的错字
    • 感谢您的关注
    【解决方案2】:

    类权重是一个字典,用于补偿数据集中的不平衡。例如,如果您有一个包含 1000 个狗图像和 100 个猫图像的数据集,那么您的分类器将偏向于狗类。如果它每次都预测狗,那么 90% 的时间都是正确的。为了弥补不平衡,class_weights 字典使您在计算损失时可以将猫的样本加权比狗的样本高 10 倍。一种方法是使用 sklearn 中的 class_weight 方法,如下所示

    from sklearn.utils import class_weight
    import numpy as np
    
    class_weights = class_weight.compute_class_weight(
                   'balanced',
                    np.unique(train_generator.classes), 
                    train_generator.classes) 
    

    【讨论】:

      【解决方案3】:

      如果您使用不平衡类,则应使用类权重。例如,如果您有两个类,其中 0 类的数据是 1 类的两倍:

      class_weight = {0 :1, 1: 2}
      

      编译时,请使用 weighted_metrics 而不仅仅是指标,否则模型在计算准确度时不会考虑类权重,并且会高得不切实际。

      model.compile(loss="binary_crossentropy",optimizer='adam', weighted_metrics=['accuracy'])
      
      hist = model.fit_generator(train,validation_split=0.2,epochs=20,class_weight=class_weight)
      

      【讨论】:

        猜你喜欢
        • 2018-05-31
        • 2019-06-19
        • 2019-08-08
        • 2017-11-11
        • 2021-08-25
        • 2019-07-28
        • 2018-09-03
        • 2016-08-01
        • 2017-12-26
        相关资源
        最近更新 更多