mindspore/docs/api/api_python/nn/mindspore.nn.LogSoftmax.rst

50 lines
1.3 KiB
ReStructuredText
Raw Normal View History

2021-12-08 15:50:20 +08:00
mindspore.nn.LogSoftmax
=============================
.. py:class:: mindspore.nn.LogSoftmax(axis=-1)
Log Softmax激活函数。
按元素计算Log Softmax激活函数。
输入经Softmax函数、Log函数转换后值的范围在[-inf,0)。
Log Softmax定义如下
.. math::
\text{logsoftmax}(x_i) = \log \left(\frac{\exp(x_i)}{\sum_{j=0}^{n-1} \exp(x_j)}\right),
其中,:math:`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]]