The torch.is_nonzero() method returns True if the input tensor contains exactly one element and that element is not zero, and False otherwise. It works exclusively with tensors containing exactly one element (0-dimensional or single-element tensors).

There is also another method called .item() to extract the tensor’s value, which you can use to achieve similar functionality by comparing it to 0. However, torch.is_nonzero() is more concise and avoids explicit value extraction.
Syntax
torch.is_nonzero(input)
Parameters
Argument | Description |
input (Tensor) | It represents a PyTorch tensor that contains a single element. It can be of any numeric dtype (int, float, complex, bool). |
Valid single-element tensors
Let’s define two single-element tensors, in which one is a zero-element tensor and the other is a non-zero element.
import torch # Single element tensors tensor_zero = torch.tensor([0]) tensor_nonzero = torch.tensor([5]) print(torch.is_nonzero(tensor_zero)) # Output: False print(torch.is_nonzero(tensor_nonzero)) # Output: True
You can see that only tensors with one element and a nonzero element return True, otherwise False.
Different Data Types

import torch # Integer tensors int_zero = torch.tensor(0) int_nonzero = torch.tensor(-3) print(torch.is_nonzero(int_zero)) # Output: False print(torch.is_nonzero(int_nonzero)) # Output: True # Float tensors float_zero = torch.tensor(0.0) float_nonzero = torch.tensor(0.001) print(torch.is_nonzero(float_zero)) # Output: False print(torch.is_nonzero(float_nonzero)) # Output: True # Boolean tensors bool_false = torch.tensor(False) bool_true = torch.tensor(True) print(torch.is_nonzero(bool_false)) # Output: False print(torch.is_nonzero(bool_true)) # Output: True
Different Shapes (Single Element)
It does not matter if the input vector is 1D, 2D, or 3D, as long as it has only one element and not multiple elements.
import torch scalar = torch.tensor(21) vector = torch.tensor([21]) matrix = torch.tensor([[21]]) tensor_3d = torch.tensor([[[21]]]) print(torch.is_nonzero(scalar)) # Output: True print(torch.is_nonzero(vector)) # Output: True print(torch.is_nonzero(matrix)) # Output: True print(torch.is_nonzero(tensor_3d)) # Output: True
Multi-element tensor → ERROR
What if the input tensor has multiple elements and not a single element? Well, it throws a RuntimeError.import torch # Multi element tensors multi_tensor = torch.tensor([1, 2]) print(torch.is_nonzero(multi_tensor)) # RuntimeError: Boolean value of Tensor with more than one value is ambiguous
You can see that it threw RuntimeError: Boolean value of Tensor with more than one value is ambiguous. To avoid this type of error, make sure your input tensor has only a single element.
Edge cases
NaN
Let’s take a tensor containing only one NaN value.import torch nan_tensor = torch.tensor(float('nan')) output = torch.is_nonzero(nan_tensor) print(output) # Output: TrueIf the input is a NaN value, it will still return True because NaN is a non-zero value.
Infinity
What if the tensor only contains one element, and that is infinity? It will also return True for that.import torch inf_tensor = torch.tensor(float('inf')) output = torch.is_nonzero(inf_tensor) print(output) # Output: True
Empty tensor
Attempt to use torch.is_nonzero() method on an empty tensor returns an error.
import torch empty_tensor = torch.tensor([]) try: output = torch.is_nonzero(empty_tensor) except RuntimeError as e: print(e) # Output: Boolean value of Tensor with no values is ambiguousThat’s all!