log_softmax

paddle. log_softmax ( input: Tensor, dim: int | None = None, dtype: DTypeLike | None = None, *, out: Tensor | None = None ) → Tensor [source]

This operator implements PyTorch compatible log_softmax. The calculation process is as follows:

\[\begin{split}\begin{aligned} log\_softmax[i, j] &= log(softmax(input)) \\ &= log\left(\frac{\exp(input[i, j])}{\sum_j \exp(input[i, j])}\right) \end{aligned}\end{split}\]
Parameters
  • input (Tensor) – The input Tensor with data type float32, float64.

  • dim (int, optional) – The dim along which to perform log_softmax calculations. It should be in range [-D, D), where D is the rank of input. If dim < 0, it works the same way as \(dim + D\). If dim is None, it defaults to 0 for 0-D, 1-D, and 3-D tensors, and 1 for 2-D tensors (same as PyTorch behavior). Default is None.

  • dtype (str|np.dtype|core.VarDesc.VarType|core.DataType, optional) – The desired data type of the output tensor. If dtype is specified, input is cast to dtype before the operation is performed. Supported dtype: float32, float64. If dtype is None, the output Tensor has the same dtype as input. Default is None.

  • out (Tensor, optional) – The output Tensor.

Returns

A Tensor with the same shape and data type (use dtype if it is specified) as input.

Examples

>>> import paddle

>>> 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 = paddle.compat.nn.functional.log_softmax(x, -1)
>>> out2 = paddle.compat.nn.functional.log_softmax(x, -1, dtype='float64')
>>> # out1's data type is float32; out2's data type is float64
>>> print(out1)
>>> print(out2)