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

49 lines
1.7 KiB
ReStructuredText
Raw Normal View History

2021-12-07 12:17:56 +08:00
mindspore.nn.Softmax
====================
.. py:class:: mindspore.nn.Softmax(axis=-1)
Softmax函数它是二分类函数 :class:`mindspore.nn.Sigmoid` 在多分类上的推广,目的是将多分类的结果以概率的形式展现出来。
2021-12-07 12:17:56 +08:00
对输入Tensor在轴 `axis` 上的元素计算其指数函数值,然后归一化到[0, 1]范围总和为1。
2021-12-07 12:17:56 +08:00
Softmax定义为
.. math::
\text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j=0}^{n-1}\exp(x_j)},
其中, :math:`x_{i}` 是输入Tensor在轴 `axis` 上的第 :math:`i` 个元素。
2021-12-07 12:17:56 +08:00
**参数:**
**axis** (Union[int, tuple[int]]) - 指定Softmax运算的轴axis假设输入 `x` 的维度为x.ndim则axis的范围为 `[-x.ndim, x.ndim)` -1表示最后一个维度。默认值-1。
2021-12-07 12:17:56 +08:00
**输入:**
**x** (Tensor) - 用于计算Softmax函数的Tensor数据类型为float16或float32。
**输出:**
Tensor数据类型和shape与 `x` 相同,取值范围为[0, 1]。
2021-12-07 12:17:56 +08:00
**异常:**
- **TypeError** - `axis` 既不是int也不是tuple。
- **TypeError** - `x` 的数据类型既不是float16也不是float32。
- **ValueError** - `axis` 是长度小于1的tuple。
- **ValueError** - `axis` 是一个tuple其元素不都在 `[-x.ndim, x.ndim)` 范围内。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
>>> # axis = -1(default), and the sum of return value is 1.0.
2021-12-07 12:17:56 +08:00
>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> softmax = nn.Softmax()
>>> output = softmax(x)
>>> print(output)
[0.03168 0.01166 0.0861 0.636 0.2341 ]
>>> assert(1.0 == output.sum())
2021-12-07 12:17:56 +08:00