fn

logsumexp

Tensor
logsumexp(input: Tensor, dim: int | Sequence[int] | None = ..., keepdim: bool = ...)
source

Compute the numerically stable log-sum-exp along dim.

Combines exp, sum and log in a single fused operation, subtracting the per-slice maximum before exponentiation to avoid overflow. This is the canonical normaliser of categorical log-probabilities and the smooth approximation of max.

Parameters

inputTensor
Input tensor (real-valued).
dimint or sequence of int= ...
Dimension(s) over which to reduce.
keepdimbool= False
Keep reduced dims with size 1 if True.

Returns

Tensor

Reduced tensor.

Notes

The stable formulation is

LSE(x)  =  m+log ⁣iexim,m=maxixi,\mathrm{LSE}(x) \;=\; m + \log\!\sum_i e^{x_i - m}, \qquad m = \max_i x_i ,

which avoids inf even when x has large positive components. LSE is convex, satisfies max(x)LSE(x)max(x)+logn\max(x) \le \mathrm{LSE}(x) \le \max(x) + \log n, and its gradient is the softmax.

Examples

>>> import lucid
>>> x = lucid.tensor([1000.0, 1000.0, 1000.0])
>>> lucid.logsumexp(x, dim=0)
Tensor(1001.0986)