Keras: weighted binary crossentropy

后端 未结 6 577
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 08:39

I tried to implement a weighted binary crossentropy with Keras, but I am not sure if the code is correct. The training output seems to be a bit confusing. After a few epochs I j

6条回答
  •  死守一世寂寞
    2021-01-31 09:37

    You can calc the weights like this and have the binary cross entropy like this which will programmatically put one_weight to 0.11 and one to 0.89:

    one_weight = (1-num_of_ones)/(num_of_ones + num_of_zeros)
    zero_weight = (1-num_of_zeros)/(num_of_ones + num_of_zeros)
    
    def weighted_binary_crossentropy(zero_weight, one_weight):
    
        def weighted_binary_crossentropy(y_true, y_pred):
    
            b_ce = K.binary_crossentropy(y_true, y_pred)
    
            # weighted calc
            weight_vector = y_true * one_weight + (1 - y_true) * zero_weight
            weighted_b_ce = weight_vector * b_ce
    
            return K.mean(weighted_b_ce)
    
        return weighted_binary_crossentropy
    

提交回复
热议问题