oneflow.nn.LogSoftmax

class oneflow.nn.LogSoftmax(dim: Optional[int] = None)

Applies the LogSoftmax function to an n-dimensional input Tensor. The LogSoftmax formulation can be simplified as:

\[\text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right) = x_i - \log({ \sum_j \exp(x_j)})\]
Parameters

dim (int) – A dimension along which LogSoftmax will be computed.

Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Output: \((N, *)\), same shape as the input

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> m = flow.nn.LogSoftmax(dim=1)
>>> x = flow.Tensor(
...    np.array(
...        [[ 0.4296, -1.1957,  2.5463],
...        [ 1.2552, -1.5747,  0.6923]]
...    )
... )
>>> out = m(x)
>>> out
tensor([[-2.2513, -3.8766, -0.1346],
        [-0.4877, -3.3176, -1.0506]], dtype=oneflow.float32)