fn

xlogy

Tensor
xlogy(x: Tensor | Scalar, y: Tensor | Scalar)
source

Compute xlogyx \log y with the convention 0log0=00 \cdot \log 0 = 0.

Frequently used to compute cross-entropy losses where the limit limx0+xlogx=0\lim_{x \to 0^{+}} x \log x = 0 must be honored to avoid NaN contamination from probability values that are exactly zero.

Parameters

xTensor | Scalar
Left operand (multiplier).
yTensor | Scalar
Argument of the logarithm. Must be non-negative for a real result.

Returns

Tensor

Element-wise xlogyx \log y with the zero-times-zero convention applied.

Notes

Mathematical definition:

xlogy(x,y)={0,x=0,xlogy,x0.\operatorname{xlogy}(x, y) = \begin{cases} 0, & x = 0, \\ x \cdot \log y, & x \neq 0. \end{cases}

Gradient with respect to y is x/yx / y; with respect to x is logy\log y. Both are masked to zero wherever x == 0.

Examples

>>> import lucid
>>> x = lucid.tensor([0.0, 1.0, 2.0])
>>> y = lucid.tensor([0.0, 2.0, 3.0])
>>> lucid.xlogy(x, y)
Tensor([0.    , 0.6931, 2.1972])