fn
log_softmax
→Tensorlog_softmax(x: Tensor, dim: int | None = None)Numerically stable log-softmax along a dimension.
Equivalent to log(softmax(x, dim)) but computed in a way that
avoids overflow in the exponentials and the loss of precision incurred
by taking the logarithm of small softmax probabilities. Almost always
the right thing to use as the input to negative-log-likelihood / NLL
classification losses.
Parameters
xTensorInput logits of any shape.
dimint= NoneDimension along which the log-softmax is computed. Defaults to
the last dimension (
-1).Returns
TensorLog-probabilities of the same shape as x, summing-to-one on
the exponentiated scale along dim.
Notes
The shift inside the exponent is the key numerical trick:
it makes every exponent non-positive so
and the sum is bounded. Pairing log_softmax with
nll_loss is numerically equivalent to — and more stable than —
softmax followed by cross_entropy.
Examples
>>> import lucid
>>> from lucid.nn.functional import log_softmax
>>> logits = lucid.tensor([[1.0, 2.0, 3.0]])
>>> log_softmax(logits, dim=1)
Tensor([[-2.4076, -1.4076, -0.4076]])