softmax

paddle.nn.functional. softmax ( x, axis=- 1, dtype=None, name=None ) [源代码]

实现 softmax 层。计算过程如下:

步骤 1:输入 xaxis 维会被置换到最后一维;

步骤 2:将输入 x 在逻辑上变换为二维矩阵。二维矩阵第一维(列长度)是输入除最后一维之外的其他维度值的乘积,第二维(行长度)和输入 axis 维的长度相同;对于矩阵的每一行,softmax 操作对其进行重新缩放,使得该行的每个元素在 \([0, 1]\) 范围内,并且总和为 \(1\)

步骤 3:softmax 操作执行完成后,执行步骤 1 和步骤 2 的逆运算,将二维矩阵恢复至和输入 x 相同的维度。

上述步骤 2 中 softmax 操作计算过程如下:

  • 对于二维矩阵的每一行,计算 K 维向量(K 是输入第 axis 维的长度)中指定位置的指数值和全部位置指数值的和。

  • 指定位置指数值与全部位置指数值之和的比值就是 softmax 操作的输出。

对于二维矩阵中的第 \(i\) 行和第 \(j\) 列有:

\[softmax[i, j] = \frac{\exp(x[i, j])}{\sum_j(exp(x[i, j])}\]
  • 示例 1(矩阵一共有三维。axis = -1,表示沿着最后一维(即第三维)做 softmax 操作)

# input

  x.shape = [2, 3, 4]

  x.data = [[[2.0, 3.0, 4.0, 5.0],
             [3.0, 4.0, 5.0, 6.0],
             [7.0, 8.0, 8.0, 9.0]],
            [[1.0, 2.0, 3.0, 4.0],
             [5.0, 6.0, 7.0, 8.0],
             [6.0, 7.0, 8.0, 9.0]]]

  axis = -1

# output

  out.shape = [2, 3, 4]

  out.data = [[[0.0320586 , 0.08714432, 0.23688282, 0.64391426],
               [0.0320586 , 0.08714432, 0.23688282, 0.64391426],
               [0.07232949, 0.19661193, 0.19661193, 0.53444665]],
              [[0.0320586 , 0.08714432, 0.23688282, 0.64391426],
               [0.0320586 , 0.08714432, 0.23688282, 0.64391426],
               [0.0320586 , 0.08714432, 0.23688282, 0.64391426]]]
  • 示例 2(矩阵一共有三维。axis = 1,表示沿着第二维做 softmax 操作)

# input

  x.shape = [2, 3, 4]

  x.data = [[[2.0, 3.0, 4.0, 5.0],
             [3.0, 4.0, 5.0, 6.0],
             [7.0, 8.0, 8.0, 9.0]],
            [[1.0, 2.0, 3.0, 4.0],
             [5.0, 6.0, 7.0, 8.0],
             [6.0, 7.0, 8.0, 9.0]]]

  axis = 1

# output

  out.shape = [2, 3, 4]

  out.data = [[[0.00657326, 0.00657326, 0.01714783, 0.01714783],
               [0.01786798, 0.01786798, 0.04661262, 0.04661262],
               [0.97555875, 0.97555875, 0.93623955, 0.93623955]],
              [[0.00490169, 0.00490169, 0.00490169, 0.00490169],
               [0.26762315, 0.26762315, 0.26762315, 0.26762315],
               [0.72747516, 0.72747516, 0.72747516, 0.72747516]]]

参数

  • x (Tensor) - 输入的 Tensor,数据类型为 bfloat16 、 float16 、 float32 或 float64。

  • axis (int,可选) - 指定对输入 x 进行运算的轴。axis 的有效范围是 \([-D, D)\)\(D\) 是输入 x 的维度,axis 为负值时与 \(axis + D\) 等价。默认值为 -1。

  • dtype (str,可选) - 输出 Tensor 的数据类型,支持 bfloat16、 float16、 float32、float64。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

Tensor,形状和 x 相同,数据类型为 dtype 或者和 x 相同。

代码示例

>>> import paddle
>>> import paddle.nn.functional as F

>>> x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0],
...                        [3.0, 4.0, 5.0, 6.0],
...                        [7.0, 8.0, 8.0, 9.0]],
...                       [[1.0, 2.0, 3.0, 4.0],
...                        [5.0, 6.0, 7.0, 8.0],
...                        [6.0, 7.0, 8.0, 9.0]]],dtype='float32')
>>> out1 = F.softmax(x)
>>> out2 = F.softmax(x, dtype='float64')
>>> #out1's data type is float32; out2's data type is float64
>>> #out1 and out2's value is as follows:
>>> print(out1)
>>> print(out2)
Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[0.03205860, 0.08714432, 0.23688284, 0.64391428],
  [0.03205860, 0.08714432, 0.23688284, 0.64391428],
  [0.07232949, 0.19661194, 0.19661194, 0.53444666]],
 [[0.03205860, 0.08714432, 0.23688284, 0.64391428],
  [0.03205860, 0.08714432, 0.23688284, 0.64391428],
  [0.03205860, 0.08714432, 0.23688284, 0.64391428]]])
Tensor(shape=[2, 3, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
[[[0.03205860, 0.08714432, 0.23688282, 0.64391426],
  [0.03205860, 0.08714432, 0.23688282, 0.64391426],
  [0.07232949, 0.19661193, 0.19661193, 0.53444665]],
 [[0.03205860, 0.08714432, 0.23688282, 0.64391426],
  [0.03205860, 0.08714432, 0.23688282, 0.64391426],
  [0.03205860, 0.08714432, 0.23688282, 0.64391426]]])