如何处理不平衡数据集进行多标签分类



我想知道如何在处理一个真正不平衡的数据集时如何惩罚其他类别的较少的班级(大约有20000个样本的10个类,但以下是每个类的情况的数量:[[[10868 26 4797 26 8320 26 5278 9412 4485 16172](。

我阅读了有关TensorFlow函数的了解:加权_CROSS_ENTROPY_WITH_LOGITS(https://wwwww.tensorflow.org/api_docs/python/python/tf/tf/nn/weighted_cross_cross_entropy_with_with_logits(,但我不确定我是否可以将其用于多i lab for for Multi for。>

我找到了一个帖子,可以完美地总结我的问题(用于多级多级多标签分类的神经网络(,并且提出了一个想法,但没有答案,我认为这个想法可能很好:(

谢谢您的想法和答案!

首先,我的建议您可以修改以多标签方式使用的成本函数。有代码显示如何使用TensorFlow中的SoftMax横熵进行多标签图像任务。

使用该代码,您可以在每行损失计算中进行多个权重。这是示例代码,以防您执行多标签任务:(即,每个图像都有两个标签(

logits_split  = tf.split( axis=1, num_or_size_splits=2, value= logits  ) 
labels_split  = tf.split( axis=1, num_or_size_splits=2, value= labels  )
weights_split = tf.split( axis=1, num_or_size_splits=2, value= weights )
total         = 0.0
for i in range ( len(logits_split) ):  
    temp   = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( logits=logits_split[i] , labels=labels_split[i] )) 
    total += temp * tf.reshape(weights_split[i],[-1])  

我认为您可以将tf.nn.weighted_cross_entropy_with_logits用于多类分类。

例如,对于4个类,其中最多的成员的比率为[0.8, 0.5, 0.6, 1],您只需以下面的方式给它一个权重向量:

cross_entropy = tf.nn.weighted_cross_entropy_with_logits(
        targets=ground_truth_input, logits=logits, 
        pos_weight = tf.constant([0.8,0.5,0.6,1]))

,所以我不确定考虑到您所写的内容,我会理解您的问题。您链接到的帖子写了有关多标签和多类的文章,但考虑到那里写的内容,这确实没有意义。因此,我将作为一个多级问题将其处理为每个样本,您都有一个标签。

为了惩罚类,我根据当前批次中的标签实现了一个重量张量。对于三级问题,您可以。将权重定义为类的反频率,以便分别为1、2和3的比例[0.1、0.7、0.2],则权重为[10,1.43,5]。然后根据当前批次定义重量张量,然后是

weight_per_class = tf.constant([10, 1.43, 5]) # shape (, num_classes)
onehot_labels = tf.one_hot(labels, depth=3) # shape (batch_size, num_classes)
weights = tf.reduce_sum(
    tf.multiply(onehot_labels, weight_per_class), axis=1) # shape (batch_size, num_classes)
reduction = tf.losses.Reduction.MEAN # this ensures that we get a weighted mean
loss = tf.losses.softmax_cross_entropy(
        onehot_labels=onehot_labels, logits=logits, weights=weights, reduction=reduction)

使用SoftMax确保分类问题不是3个独立的分类。

最新更新