fn

logsigmoid

Tensor
logsigmoid(x: Tensor)
source

Numerically stable logσ(x)\log \sigma(x).

Direct evaluation of log(1/(1+ex))\log(1 / (1 + e^{-x})) underflows once σ(x)\sigma(x) is below machine epsilon (around x37x \lesssim -37 for float32). Rewriting as softplus(x)-\operatorname{softplus}(-x) matches the same value while staying finite for arbitrarily negative inputs.

Parameters

xTensor
Input logits.

Returns

Tensor

logσ(x)\log \sigma(x) with the same shape as x, values in (,0](-\infty, 0].

Notes

logσ(x)=log(1+ex)=softplus(x)\log\sigma(x) = -\log(1 + e^{-x}) = -\operatorname{softplus}(-x)

Frequently the building block of binary cross-entropy with logits (BCE-with-logits) and of importance-weighting computations in RL.

Examples

>>> import lucid
>>> from lucid.nn.functional import logsigmoid
>>> x = lucid.tensor([-10.0, 0.0, 10.0])
>>> logsigmoid(x)
Tensor([-10.0000,  -0.6931,  -0.0000])