Skip to content
fn

xlog1py

→Tensor
xlog1py(input: TensorOrScalar, other: TensorOrScalar)

Safe product xlog⁡(1+y)x \log(1 + y) with limit-convention zero handling.

Computes xlog⁡(1+y)x \log(1 + y) element-wise but takes the product as 0 wherever x=0x = 0, including where log⁡(1+y)\log(1 + y) is infinite (y = -1). Mirrors lucid.xlogy but uses log⁡(1+y)\log(1 + y) instead of log⁡y\log y, which is the right primitive for log-densities of distributions expressed in terms of small offsets (Negative Binomial log-likelihood, Beta survival functions, etc.).

Parameters

inputTensor | Scalar
Multiplier. Either operand may be a Python number.
otherTensor | Scalar
Argument of log1p; broadcast-compatible with input.

Returns

Tensor

xlog⁡(1+y)x \log(1 + y) element-wise, broadcast to the common shape of input and other.

Notes

Mathematical definition with the limit convention:

xlog1py(x,y)={NaN,y=NaN0,x=0xlog⁡(1+y),otherwise.\text{xlog1py}(x, y) = \begin{cases} \mathrm{NaN}, & y = \mathrm{NaN} \\[2pt] 0, & x = 0 \\[2pt] x \log(1 + y), & \text{otherwise}. \end{cases}

The convention covers only x == 0: with x != 0 the plain definition applies, so xlog1py(2, -1) is -inf. Using log⁡(1+y)\log(1 + y) avoids precision loss for small y where 1 + y would round to 1.0.

Gradients are ∂/∂x=log⁡(1+y)\partial/\partial x = \log(1 + y) and ∂/∂y=x/(1+y)\partial/\partial y = x / (1 + y), except where x == 0 and log⁡(1+y)\log(1 + y) is not finite (y <= -1 or y infinite): there both are 0, so the backward never forms 0 \cdot \infty. This is the convention of lucid.xlogy, and the two functions share one implementation. Two cells differ from the reference framework on purpose: at (0, -1) its y gradient is 0 / 0 = NaN and Lucid's is 0, and at (0, +\infty) its x gradient is +∞+\infty and Lucid's is 0.

Examples

>>> import lucid
>>> from lucid.special import xlog1py
>>> x = lucid.tensor([0.0, 1.0, 2.0])
>>> y = lucid.tensor([0.0, 1.0, 3.0])
>>> xlog1py(x, y)
tensor([0., 0.6931, 2.773])
>>> xlog1py(lucid.tensor([0.0, 2.0]), lucid.tensor([-1.0, -1.0]))
tensor([0., -inf])

Used by 4