您提出的代码对我来说似乎是错误的。我同意,损失应乘以重量。
但是,如果将logit乘以类权重,则结尾为:
weights[class] * -x[class] + log( \sum_j exp(x[j] * weights[class]) )
第二项不等于:
weights[class] * log(\sum_j exp(x[j]))
为了说明这一点,我们可以将后者重写为:
log( (\sum_j exp(x[j]) ^ weights[class] )
所以这是我提议的代码:
ratio = 31.0 / (500.0 + 31.0)
class_weight = tf.constant([[ratio, 1.0 - ratio]])
logits = ... # shape [batch_size, 2]
weight_per_label = tf.transpose( tf.matmul(labels
, tf.transpose(class_weight)) ) #shape [1, batch_size]
# this is the weight for each datapoint, depending on its label
xent = tf.mul(weight_per_label
, tf.nn.softmax_cross_entropy_with_logits(logits, labels, name="xent_raw") #shape [1, batch_size]
loss = tf.reduce_mean(xent) #shape 1
0
我正在尝试将深度学习应用于目标类别(500k,31k)之间类别高度不平衡的二进制分类问题。我想编写一个自定义损失函数,该函数应类似于:minimal(100-((predicted_smallerclass)/(total_smallerclass))* 100)
感谢有关如何构建此逻辑的任何指示。