megengine.functional.softmax

softmax(inp, axis=None)[源代码]

使用 \(\text{softmax}(x)\) 函数。\(\text{softmax}(x)\) 被定义为:

\[\text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]

应用softmax于一个n维输入张量,并重新放缩张量中的元素值,使得n维输出张量中所有元素的取值范围为 [0,1] 并且加和为1。

更多信息参见 Softmax

参数
  • inp (Tensor) – 输入张量。

  • axis (Optional[int]) – 在该轴上使用 \(\text{softmax}(x)\) 方法。默认情况下,\(\text{softmax}(x)\) 将在序号最大的轴上使用。

返回类型

megengine.tensor.Tensor

例如:

import numpy as np
from megengine import tensor
import megengine.functional as F

x = tensor(np.arange(-5, 5, dtype=np.float32)).reshape(2,5)
out = F.softmax(x)
print(out.numpy().round(decimals=4))

输出:

[[0.0117 0.0317 0.0861 0.2341 0.6364]
 [0.0117 0.0317 0.0861 0.2341 0.6364]]
返回类型

Tensor

参数
  • inp (megengine.tensor.Tensor) –

  • axis (Optional[int]) –