mindspore.ops.softmax

View Source On Gitee
mindspore.ops.softmax(input, axis=- 1, *, dtype=None)[source]

Applies the Softmax operation to the input tensor on the specified axis. Suppose a slice in the given axis \(axis\), then for each element \(input_i\), the Softmax function is shown as follows:

\[\text{output}(input_i) = \frac{\exp(input_i)}{\sum_{j = 0}^{N-1}\exp(input_j)},\]

where \(N\) is the length of the tensor.

Parameters
  • input (Tensor) – Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

  • axis (int, optional) – The axis to perform the Softmax operation. Default: -1 .

Keyword Arguments

dtype (mindspore.dtype, optional) – When set, input will be converted to the specified type, dtype, before execution, and dtype of returned Tensor will also be dtype. Default: None .

Returns

Tensor, with the same type and shape as the input.

Raises

TypeError – If axis is not an int.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
>>> output = ops.softmax(input)
>>> print(output)
[0.01165623 0.03168492 0.08612854 0.23412167 0.6364086 ]