Pytorch torch.nn.Softmax中dim参数的用途是什么
我不明白dim参数在torch.nn.Softmax中适用于什么。有一个警告告诉我使用它,我将它设置为1,但我不明白我在设置什么。公式中使用的位置:Pytorch torch.nn.Softmax中dim参数的用途是什么,pytorch,softmax,Pytorch,Softmax,我不明白dim参数在torch.nn.Softmax中适用于什么。有一个警告告诉我使用它,我将它设置为1,但我不明白我在设置什么。公式中使用的位置: Softmax(xi)=exp(xi)/∑jexp(xj) 此处没有dim,因此它适用于什么?torch.nn.Softmax上的Pytorch说明: dim int–沿其计算Softmax的维度,因此沿dim的每个切片总和为1 例如,如果有二维矩阵,则可以选择是将softmax应用于行还是列: import torch import
Softmax(xi)=exp(xi)/∑jexp(xj)
此处没有dim,因此它适用于什么?torch.nn.Softmax上的Pytorch说明:
dim int–沿其计算Softmax的维度,因此沿dim的每个切片总和为1
例如,如果有二维矩阵,则可以选择是将softmax应用于行还是列:
import torch
import numpy as np
softmax0 = torch.nn.Softmax(dim=0) # Applies along columns
softmax1 = torch.nn.Softmax(dim=1) # Applies along rows
v = np.array([[1,2,3],
[4,5,6]])
v = torch.from_numpy(v).float()
softmax0(v)
# Returns
#[[0.0474, 0.0474, 0.0474],
# [0.9526, 0.9526, 0.9526]])
softmax1(v)
# Returns
#[[0.0900, 0.2447, 0.6652],
# [0.0900, 0.2447, 0.6652]]
请注意,对于softmax0,列如何添加到1,对于softmax1,行如何添加到1。torch.nn.Softmax上的Pytorch说明:
dim int–沿其计算Softmax的维度,因此沿dim的每个切片总和为1
例如,如果有二维矩阵,则可以选择是将softmax应用于行还是列:
import torch
import numpy as np
softmax0 = torch.nn.Softmax(dim=0) # Applies along columns
softmax1 = torch.nn.Softmax(dim=1) # Applies along rows
v = np.array([[1,2,3],
[4,5,6]])
v = torch.from_numpy(v).float()
softmax0(v)
# Returns
#[[0.0474, 0.0474, 0.0474],
# [0.9526, 0.9526, 0.9526]])
softmax1(v)
# Returns
#[[0.0900, 0.2447, 0.6652],
# [0.0900, 0.2447, 0.6652]]
请注意,对于softmax0,列如何添加到1,对于softmax1,行如何添加到1