LogSoftmax

class paddle.nn. LogSoftmax ( axis=- 1, name=None ) [源代码]

LogSoftmax 激活层,计算公式如下:

\[\begin{split}\begin{aligned} Out[i, j] &= log(softmax(x)) \\ &= log(\frac{\exp(X[i, j])}{\sum_j(\exp(X[i, j])}) \end{aligned}\end{split}\]

参数

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

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

形状

  • input :任意形状的 Tensor。

  • output :和 input 具有相同形状的 Tensor。

代码示例

>>> import paddle

>>> x = [[[-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]]]
>>> m = paddle.nn.LogSoftmax()
>>> x = paddle.to_tensor(x)
>>> out = m(x)
>>> print(out)
Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[-7.12783957 , -2.12783957 , -9.12783909 , -0.12783945 ],
  [-2.12705135 , -9.12705135 , -0.12705141 , -11.12705135],
  [-16.31326103, -17.31326103, -1.31326187 , -0.31326184 ]],
 [[-3.05181193 , -6.05181217 , -7.05181217 , -0.05181199 ],
  [-12.31326675, -1.31326652 , -0.31326646 , -15.31326675],
  [-3.44018984 , -2.44018984 , -1.44018972 , -0.44018975 ]]])