TensorPlay
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-15

tensorplay.functional.is_nonzero

tensorplay.functional.is_nonzero(input)[source]

Returns True if the input is a single element tensor which is not equal to zero after type conversions. i.e. not equal to tensorplay.tensor([0.]) or tensorplay.tensor([0]) or tensorplay.tensor([False]). Throws a RuntimeError if tensorplay.numel() != 1 (even in case of sparse tensors).

Parameters:

input (Tensor) – the input tensor.

Examples:

>>> tensorplay.is_nonzero(tensorplay.tensor([0.]))
False
>>> tensorplay.is_nonzero(tensorplay.tensor([1.5]))
True
>>> tensorplay.is_nonzero(tensorplay.tensor([False]))
False
>>> tensorplay.is_nonzero(tensorplay.tensor([3]))
True
>>> tensorplay.is_nonzero(tensorplay.tensor([1, 3, 5]))
Traceback (most recent call last):
...
RuntimeError: Boolean value of Tensor with more than one value is ambiguous
>>> tensorplay.is_nonzero(tensorplay.tensor([]))
Traceback (most recent call last):
...
RuntimeError: Boolean value of Tensor with no values is ambiguous

On this page

Ask DeepWiki