fn
is_nonzero
→boolis_nonzero(x: Tensor)Predicate: is a single-element tensor non-zero?
Reduces a scalar (one-element) tensor to a Python bool. Raises if
the tensor has more than one element so that the caller is forced to
pick an explicit reduction (e.g. .any() / .all()) for
multi-element inputs.
Parameters
xTensorSingle-element tensor (i.e.
x.numel() == 1).Returns
boolTrue iff the (sole) element is non-zero.
Raises
RuntimeErrorIf
x.numel() != 1.Notes
Equivalent to:
where is the single scalar value extracted via
Tensor.item. The strict shape check prevents the silent
bug where one would accidentally call bool(tensor) on an
unintentionally multi-element tensor.
Examples
>>> import lucid
>>> lucid.is_nonzero(lucid.tensor(0.0))
False
>>> lucid.is_nonzero(lucid.tensor(3.5))
True