Skip to content
fn

entr

→Tensor
entr(input: Tensor)

Element-wise entropy kernel −xlog⁡x-x \log x.

Returns the per-element contribution to Shannon entropy. This is the standard building block for evaluating H(p)=∑ientr(pi)H(p) = \sum_i \mathrm{entr}(p_i) for a discrete distribution, with the limit convention 0log⁡0=00 \log 0 = 0 applied at the boundary.

Parameters

inputTensor
Input tensor. Integer and bool inputs are taken as float32. Values below zero give -inf.

Returns

Tensor

Element-wise −xlog⁡x-x \log x with the limit convention at x = 0; same shape as input.

Notes

Definition:

entr(x)={−xlog⁡x,x>00,x=0−∞,x<0.\mathrm{entr}(x) = \begin{cases} -x \log x, & x > 0 \\[2pt] 0, & x = 0 \\[2pt] -\infty, & x < 0. \end{cases}

The function is concave with maximum 1/e≈0.36791/e \approx 0.3679 at x=1/ex = 1/e. It vanishes at both x = 0 (limit) and x = 1. Related quantities include rel_entr (point-wise relative entropy) and kl_div (KL kernel).

The value is −xlogy⁡(x,x)-\operatorname{xlogy}(x, x) and takes the 0 \log 0 convention from lucid.xlogy. The gradient is −(1+log⁡x)-(1 + \log x) as the reference framework has it: the one-sided +∞+\infty at x = 0 and NaN below zero.

Examples

>>> import lucid
>>> from lucid.special import entr
>>> entr(lucid.tensor([0.0, 0.5, 1.0, 2.0]))
tensor([0., 0.3466, 0., -1.386])