0

I'm now trying to use tf.losses.sigmoid_cross_entropy on an unbalanced dataset. However, I'm a little confused on the parameter weights. Here are the comments in the documentation:

weights: Optional Tensor whose rank is either 0, or the same rank as labels, and must be broadcastable to labels (i.e., all dimensions must be either 1, or the same as the corresponding losses dimension).

I know in tf.losses.softmax_cross_entropy the parameter weights can be a rank 1 tensor with weight for each sample. Why must the weights in tf.losses.sigmoid_cross_entropy have the same rank as labels?

Can anybody answer me? Better with an example.

4

1 回答 1

0

您希望对损失进行加权,因此 tensorflow 期望您为每个标签提供权重。考虑以下示例

Labels: [0, 0, 0, 1, 0]  
possible_weights1: [1]
possible_weights2: [1, 2, 1, 1, 1]
illegal_weights1: [1, 2]
illegal_weights2: [[1], [2]]

在这里,您的标签具有等级 1(只有 1 个维度),因此 tensorflow 期望您为标签中的每个元素提供权重(如 中所示possible_weights2)或为每个维度提供权重(如中所示possible_weights1,广播到[1, 1, 1, 1, 1])。
但是,如果你有illegal_weights2你的权重,那么 tensorflow 不明白它应该如何处理权重中的两个维度,因为标签中只有一个维度?所以你的等级应该总是一样的。
illegal_weights1是秩相同但权重既不与标签长度相同,也不长度为 1(可以广播)但长度为 2,不能广播,因此是非法的情况。

于 2018-02-08T12:16:41.800 回答