【问题标题】:Cross Entropy Error remains unchanged for various values对于各种值,交叉熵误差保持不变
【发布时间】:2019-10-13 14:10:05
【问题描述】:

我正在使用带有 Softmax 的交叉熵作为我的神经网络的损失函数。 我写的交叉熵函数如下:

def CrossEntropy(calculated,desired):
    sum=0
    n=len(calculated)
    for i in range(0,n):
        sum+=(desired[i] * math.log(calculated[i])) + ((1-desired[i])* math.log(1-calculated[i]))

    crossentropy=(-1)*sum/n
    return crossentropy

现在让我们假设 期望的输出是 [1,0,0,0],我们正在测试它的 两个计算输出,即 a=[0.1,0.9,0.1,0.1 ]b=[0.1,0.1,0.1,0.9]问题在于,对于这两个计算输出,该函数将返回完全相同的交叉熵值。 那么神经网络如何知道哪个输出是正确的呢?

【问题讨论】:

  • 它被反向传播覆盖了。
  • 您的 CE 用于二进制分类,即您有 2 个输出类。对于您的样本,因为您有 4,所以上述 CE 不正确。你应该这样做: - sum(desired * log(calculated))

标签: neural-network classification conv-neural-network softmax cross-entropy


【解决方案1】:

这是意料之中的,因为您在两个 calculated 案例中具有数据对称性

在您的示例中,所需的输出是[1, 0, 0, 0]。因此,真正的阶级是第一阶级。但是,在 ab 中,您对第一类的预测是相同的 (0.1)。同样对于其他类别(真阴性 - 第 2、第 3 和第 4 类),您具有这种数据对称性(第 2 类和第 4 类对于损失计算同样重要)。

 a -> 0.9,0.1,0.1
       ^
       |       |
               V
 b -> 0.1,0.1,0.9

因此,您的损失与预期相同。

如果你移除这种对称性,你会得到不同的交叉熵损失。请参阅以下示例:


# The first two are from your examples.
print CrossEntropy(calculated=[0.1,0.9,0.1,0.1], desired=[1, 0, 0, 0])
print CrossEntropy(calculated=[0.1,0.1,0.1,0.9], desired=[1, 0, 0, 0])

# below we have prediction for the last class as 0.75 thus break the data symmetry.
print CrossEntropy(calculated=[0.1,0.1,0.1,0.75], desired=[1, 0, 0, 0])

# below we have prediction for the true class as 0.45.
print CrossEntropy(calculated=[0.45,0.1,0.1,0.9], desired=[1, 0, 0, 0])


result:
1.20397280433
1.20397280433
0.974900121357
0.827953455132

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 2021-10-02
    • 1970-01-01
    • 2019-03-23
    • 2018-11-27
    • 2016-07-30
    • 2017-12-22
    • 2019-07-13
    相关资源
    最近更新 更多