class

LogSoftmax

extendsModule
LogSoftmax(dim: int | None = None)
source

Log-Softmax activation function.

Applies element-wise:

LogSoftmax(xi)=ln ⁣(exijexj)=xiln ⁣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.4076, -1.4076, -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)

Methods (3)

dunder

__init__

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

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

fn

forward

Tensor
forward(x: Tensor)
source

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.