神经网络 – Tensorflow:具有交叉熵的缩放logits

在Tensorflow中,我有一个分类器网络和不平衡的培训课程.由于各种原因,我不能使用重新采样来补偿不平衡的数据.因此,我不得不通过其他方式补偿失衡,特别是根据每个类中的示例数量将logits乘以权重.我知道这不是首选方法,但重新采样不是一种选择.我的训练损失op是tf.nn.softmax_cross_entropy_with_logits(我也可以尝试tf.nn.sparse_softmax_cross_entropy_with_logits). Tensorflow文档在这些操作的描述中包括以下内容:

WARNING: This op expects unscaled logits, since it performs a softmax
on logits internally for efficiency. Do not call this op with the
output of softmax, as it will produce incorrect results.

我的问题:上面的警告是指仅由softmax完成的缩放,还是意味着禁止任何类型的任何logit缩放?如果是后者,那么我的类重新平衡logit缩放会导致错误的结果吗?

谢谢,

罗恩

最佳答案 警告只是通知您tf.nn.softmax_cross_entropy_with_logits将在输入logits上应用softmax,然后再计算交叉熵.这个警告似乎真的避免两次应用softmax,因为交叉熵结果会非常不同.

以下是相关source code中关于实现tf.nn.softmax_cross_entropy_with_logits的函数的注释:

// NOTE(touts): This duplicates some of the computations in softmax_op
// because we need the intermediate (logits -max(logits)) values to
// avoid a log(exp()) in the computation of the loss.

正如警告所述,此实现是为了提高性能,但需要注意的是,您不应将自己的softmax图层作为输入(在实践中这有点方便).

如果强制softmax阻碍了您的计算,也许另一个API可能会有所帮助:tf.nn.sigmoid_cross_entropy_with_logitstf.nn.weighted_cross_entropy_with_logits.

但是,实现似乎并未表明任何扩展都会影响结果.我想线性缩放函数应该没问题,只要它保留原始logits重新分区即可.但无论在输入logits上应用什么,tf.nn.softmax_cross_entropy_with_logits都会在计算交叉熵之前应用softmax.

点赞