mindspore.nn.LogSoftmax

class mindspore.nn.LogSoftmax(axis=- 1)[源代码]

按元素计算Log Softmax激活函数。

输入经Softmax函数、Log函数转换后,值的范围在[-inf,0)。

Log Softmax定义如下:

\[\text{logsoftmax}(x_i) = \log \left(\frac{\exp(x_i)}{\sum_{j=0}^{n-1} \exp(x_j)}\right),\]

其中,\(x_i\) 是输入Tensor的一个元素。

参数:
  • axis (int) - Log Softmax运算的axis,-1表示最后一个维度。默认值:-1。

输入:
  • x (Tensor) - Log Softmax的输入,数据类型为float16或float32。

输出:

Tensor,数据类型和shape与 x 相同,输出值的范围在[-inf,0)。

异常:
  • TypeError - axis 不是int。

  • TypeError - x 的数据类型既不是float16也不是float32。

  • ValueError - axis 不在[-len(x), len(x))范围中。

支持平台:

Ascend GPU CPU

样例:

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> log_softmax = nn.LogSoftmax()
>>> output = log_softmax(x)
>>> print(output)
[[-5.00672150e+00 -6.72150636e-03 -1.20067215e+01]
 [-7.00091219e+00 -1.40009127e+01 -9.12250078e-04]]