megengine.functional.nn.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

实际案例

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