Skip to content
class

LogSoftmax

extendsModule
LogSoftmax(dim: int | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Log-Softmax activation function.

Applies element-wise:

LogSoftmax(xi)=ln⁡ ⁣(exi∑jexj)=xi−ln⁡ ⁣∑jexj\text{LogSoftmax}(x_i) = \ln\!\left(\frac{e^{x_i}}{\sum_j e^{x_j}}\right) = x_i - \ln\!\sum_j e^{x_j}

Combines the softmax normalisation with a logarithm in a single, numerically stable pass. The output lives in (−∞,0](-\infty, 0] and is intended to be paired with lucid.nn.NLLLoss for multi-class classification, or equivalently used directly with lucid.nn.functional.cross_entropy.

Parameters

dimint or None= None
The dimension along which log-softmax is computed. Must be specified explicitly for most use cases. Default: None.

Notes

  • Input: (∗)(*) — any shape.
  • Output: (∗)(*) — same shape as input; values along dim are non-positive and represent log-probabilities.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.LogSoftmax(dim=-1)
>>> x = lucid.tensor([[1.0, 2.0, 3.0]])
>>> m(x)
tensor([[-2.408, -1.408, -0.4076]])
>>> # Classifier output layer — pair with NLLLoss
>>> logits = lucid.randn(32, 10)
>>> log_probs = nn.LogSoftmax(dim=-1)(logits)
>>> log_probs.shape
(32, 10)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(dim: int | None = None)

Initialise the LogSoftmax module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.