我正在使用Cross Entropy和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,并且我们正在针对两个计算输出进行测试,即和a=0.1,0.9,0.1,0.1问题是,对于这两个计算出来的输出,函数会返回完全相同的交叉熵的值,,,那么,神经网络如何知道哪个输出是正确的呢?
发布于 2019-05-30 09:05:34
这是意料之中的,因为您的两个calculated
案例中都有一个数据对称。
在您的示例中,所需的输出为[1, 0, 0, 0]
。因此,真正的类是第一个类。然而,在a
和b
中,您对第一类的预测是相同的(0.1)。对于其他类别(真负片-第二类、第三类和第四类),您也具有这种数据对称性(第二类和第四类在损失计算方面同样重要)。
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
https://stackoverflow.com/questions/56335568
复制相似问题