Pytorch softmax:使用什么维度?

2024-01-25

功能torch.nn.functional.softmax有两个参数:input and dim。根据其文档,softmax 操作应用于所有切片input沿着指定的dim,并将重新调整它们以使元素位于范围内(0, 1)并求和为 1。

设输入为:

input = torch.randn((3, 4, 5, 6))

假设我想要以下内容,以便该数组中的每个条目都是 1:

sum = torch.sum(input, dim = 3) # sum's size is (3, 4, 5, 1)

我应该如何应用softmax?

softmax(input, dim = 0) # Way Number 0
softmax(input, dim = 1) # Way Number 1
softmax(input, dim = 2) # Way Number 2
softmax(input, dim = 3) # Way Number 3

我的直觉告诉我那是最后一个,但我不确定。英语不是我的母语并且这个词的使用along因此我觉得很困惑。

我不太清楚“沿着”是什么意思,所以我将使用一个可以澄清问题的例子。假设我们有一个大小为 (s1, s2, s3, s4) 的张量,我希望这种情况发生


史蒂文的回答 https://stackoverflow.com/a/49039412/是不正确的。请参阅下面的快照。实际上是相反的方式。

图片转录为代码:

>>> x = torch.tensor([[1,2],[3,4]],dtype=torch.float)
>>> F.softmax(x,dim=0)
tensor([[0.1192, 0.1192],
        [0.8808, 0.8808]])
>>> F.softmax(x,dim=1)
tensor([[0.2689, 0.7311],
        [0.2689, 0.7311]])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pytorch softmax:使用什么维度? 的相关文章

随机推荐