鉴于:
x_batch = torch.tensor([[-0.3, -0.7], [0.3, 0.7], [1.1, -0.7], [-1.1, 0.7]])
然后申请 torch.sigmoid(x_batch)
:tensor([[0.4256, 0.3318],
[0.5744, 0.6682],
[0.7503, 0.3318],
[0.2497, 0.6682]])
给出了与 torch.softmax(x_batch,dim=1)
完全不同的结果:tensor([[0.5987, 0.4013],
[0.4013, 0.5987],
[0.8581, 0.1419],
[0.1419, 0.8581]])
根据我的理解,softmax 是不是与二进制情况下的 sigmoid 完全相同?
最佳答案
你被误导了。 Sigmoid 和 softmax 不相等,即使对于 2 元素情况也是如此。
考虑 x = [x1, x2]
.
sigmoid(x1) = 1 / (1 + exp(-x1))
但softmax(x1) = exp(x1) / (exp(x1) + exp(x2))
= 1 / (1 + exp(-x1)/exp(-x2))
= 1 / (1 + exp(-(x1 - x2))
= sigmoid(x1 - x2)
从代数我们可以看到一个等价的关系是softmax(x, dim=1) = sigmoid(x - fliplr(x))
或在pytorchx_softmax = torch.sigmoid(x_batch - torch.flip(x_batch, dims=(1,))
关于python - torch.softmax 和 torch.sigmoid 在二进制情况下不等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58539767/