最近用到 softmax
函数,但是发现 softmax
的写法五花八门,记录如下
# torch._C._VariableFunctions
torch.softmax(x, dim=-1)
# class
softmax = torch.nn.Softmax(dim=-1)
x=softmax(x)
# function
x = torch.nn.functional.softmax(x, dim=-1)
简单测试了一下,用 torch.nn.Softmax
类是最慢的,另外两个差不多
torch.nn.Softmax
源码如下,可以看到这是个类,而他这里的 return F.softmax(input, self.dim, _stacklevel=5)
调用的是 torch.nn.functional.softmax
class Softmax(Module):
r"""Applies the Softmax function to an n-dimensional input Tensor
rescaling them so that the elements of the n-dimensional output Tensor
lie in the range [0,1] and sum to 1.
Softmax is defined as:
.. math::
\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}
When the input Tensor is a sparse tensor then the unspecifed
values are treated as ``-inf`&