You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mindspore.nn.Softmax.rst 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. mindspore.nn.Softmax
  2. ====================
  3. .. py:class:: mindspore.nn.Softmax(axis=-1)
  4. Softmax激活函数。
  5. 计算n维输入Tensor的Softmax函数。
  6. 对输入Tensor在 `axis` 上的元素计算其指数函数值,然后归一化到[0, 1]范围,总和为1。
  7. Softmax定义为:
  8. .. math::
  9. \text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j=0}^{n-1}\exp(x_j)},
  10. 其中, :math:`x_{i}` 是输入Tensor在 `axis` 上的第 :math:`i` 个元素。
  11. **参数:**
  12. **axis** (Union[int, tuple[int]]) - 指定Softmax运算的axis,-1表示最后一个维度。默认值:-1。
  13. **输入:**
  14. **x** (Tensor) - 用于计算Softmax函数的Tensor,数据类型为float16或float32。
  15. **输出:**
  16. Tensor,shape和数据类型与 `x` 相同,取值范围[0,1]。
  17. **异常:**
  18. - **TypeError** - `axis` 既不是int也不是tuple。
  19. - **TypeError** - `x` 的数据类型既不是float16也不是float32。
  20. - **ValueError** - `axis` 是长度小于1的tuple。
  21. - **ValueError** - `axis` 是一个tuple,其元素不都在 `[-x.ndim, x.ndim)` 范围内。
  22. **支持平台:**
  23. ``Ascend`` ``GPU`` ``CPU``
  24. **样例:**
  25. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
  26. >>> softmax = nn.Softmax()
  27. >>> output = softmax(x)
  28. >>> print(output)
  29. [0.03168 0.01166 0.0861 0.636 0.2341 ]