Skip to content
fn

log_softmax

→Tensor
log_softmax(input: Tensor, dim: int | None = None, _stacklevel: int = 3, dtype: DTypeLike | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Numerically stable log-softmax along a dimension.

Parameters

inputTensor
Input logits of any shape.
dimint or None= None
Dimension along which the log-softmax is computed.
_stacklevelint= 3
Internal stack level for the implicit-dimension warning.
dtypeDTypeLike or None= None
Target floating-point dtype to cast input to before log-softmax.

Returns

Tensor

Log-probabilities of the same shape as input.

Notes

log_softmax(x)i=xi−log⁡ ⁣∑jexj=(xi−m)−log⁡ ⁣∑jexj−m,m=max⁡jxj\text{log\_softmax}(x)_i = x_i - \log\!\sum_j e^{x_j} = (x_i - m) - \log\!\sum_j e^{x_j - m}, \quad m = \max_j x_j

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.408, -1.408, -0.4076]])

Used by 1