【问题标题】:is it possible to implement dynamic class weights in keras?是否可以在 keras 中实现动态类权重?
【发布时间】:2018-04-26 11:15:01
【问题描述】:

keras 支持class_weights 功能,允许为不同的类赋予不同的权重 - 例如当样本数量不平衡时

我想做类似的事情,但要使用动态权重,基于每批中的类不平衡。

这可能吗?

【问题讨论】:

    标签: python machine-learning neural-network deep-learning keras


    【解决方案1】:

    选项 1:

    为epochs和batchs做一个手动循环,使用方法train_on_batch,它也接受class_weight

    for epoch in range(epochs):
        for batchX,batchY in batches: #adapt this loop to your way of creating/getting batches
    
            weights = calculateOrGetTheWeights(batch)
            model.train_on_batch(batchX,batchY,...,class_weight=weights)
    

    选项 2:

    创建自定义损失。可能比较棘手,取决于数据格式、类的数量、损失函数的类型等。

    假设二维数据(样本、类)和一个多类问题:

    import keras.backend as K
    
    def customLoss(yTrue,yPred):
    
        classes = K.argmax(yTrue)
        classCount = K.sum(yTrue,axis=0)
    
        loss = K.some_loss_function(yTrue,yPred)
    
        return loss / K.gather(classCount, classes)
    

    假设使用 1D 或 2D 数据进行二元分类(仅 1 类):

    import keras.backend as K
    
    def binaryCustomLoss(yTrue,yPred):
    
        positives = yTrue
        negatives = 1 - yTrue
    
        positiveRatio = K.mean(positives)
        negativeRatio = 1 - positiveRatio #or K.mean(negatives)
    
        weights = (positives / positiveRatio) + (negatives / negativeRatio)
    
        #you may need K.squeeze(weights) here
    
        return weights * K.some_loss_function(yTrue,yPred)
    

    警告:如果任何类计数为零,则两个损失函数都将返回 Nan(或无穷大)。

    【讨论】:

    • 谢谢,我喜欢自定义损失函数。二进制分类问题会怎样?
    【解决方案2】:

    一种选择是不使用class_weight,而是使用样本权重

    如果您希望样本权重是动态的,则需要使用 fit_generator 而不是 fit,这样您就可以在运行时更改权重

    所以在伪代码中:

    def gen(x, y):
        while True:
            for x_batch, y_batch in make_batches(x, y):
                weights = make_weights(y_batch)
                yield x_batch, y_batch, weights
    model.fit_generator(gen(x_train, y_train))
    

    在这段代码中,make_weights 应该返回一个与y_batch 长度相同的数组。每个元素都是要应用于各个样本的权重

    如果您不确定class_weight 的行为和样本权重是否相同,notice keras 如何标准化类权重。

    所以类权重最终实际上被转换为样本权重:)

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 2020-11-02
      • 2011-09-10
      • 2023-04-02
      • 2011-10-27
      相关资源
      最近更新 更多