gpt4 book ai didi

python - torch softmax : What dimension to use?

转载 作者:太空狗 更新时间:2023-10-29 17:42:09 25 4
gpt4 key购买 nike

torch.nn.functional.softmax 函数有两个参数:inputdim。根据其文档,softmax 操作应用于沿指定 dim 的所有 input 切片,并将重新缩放它们,以便元素位于范围 ( 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 这个词的使用让我感到困惑。

我不是很清楚“along”是什么意思,所以我举个例子来说明一下。假设我们有一个大小为 (s1, s2, s3, s4) 的张量,我希望它发生

最佳答案

Steven's answer是不正确的。请参阅下面的快照。实际上是相反的方式。

enter image description here

图像转录为代码:

>>> 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]])

关于python - torch softmax : What dimension to use?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49036993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com