class
LogSoftmax
extends
ModuleLogSoftmax(dim: int | None = None)Log-Softmax activation function.
Applies element-wise:
Combines the softmax normalisation with a logarithm in a single,
numerically stable pass. The output lives in 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= NoneThe 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
dimare 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)Initialise the LogSoftmax module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Apply the activation function element-wise.
Parameters
inputTensorInput tensor of arbitrary shape.
Returns
TensorOutput tensor of the same shape as input.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.