Safe product with limit-convention zero handling.
Computes element-wise but takes the product as
0 wherever , including where is
infinite (y = -1). Mirrors lucid.xlogy but uses
instead of , 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
Returns
Tensor element-wise, broadcast to the common
shape of x and y.
Notes
Mathematical definition with the limit convention:
The convention covers only x == 0: with x != 0 the plain
definition applies, so xlog1py(2, -1) is -inf. Using
avoids precision loss for small y where
1 + y would round to 1.0.
Gradients are and
, except where x == 0
and 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 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])