Skip to content
fn

softmax

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

Implementing kernel

C++ engine symbols that back this Python API.

Apply the softmax function along a dimension.

Parameters

inputTensor
Input tensor of any shape (the "logits").
dimint or None= None
Dimension along which 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 softmax.

Returns

Tensor

Same-shape tensor whose entries along dim sum to 1.

Notes

softmax(x)i=exi∑jexj\text{softmax}(x)_i = \frac{e^{x_i}}{\sum_j e^{x_j}}

The engine uses the log-sum-exp shift xi↦xi−max⁡jxjx_i \mapsto x_i - \max_j x_j. For loss computation, prefer log_softmax followed by nll_loss.

Examples

>>> import lucid
>>> from lucid.nn.functional import softmax
>>> logits = lucid.tensor([[1.0, 2.0, 3.0]])
>>> p = softmax(logits, dim=1)
>>> p
tensor([[0.09003, 0.2447, 0.6652]])
>>> p.sum(dim=1)
tensor([1.])

Used by 1