Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.is_nonzero() Method

Home torch.is_nonzero() Method
torch.is_nonzero() Method in PyTorch
  • Written by krunallathiya21
  • July 15, 2025
  • 0 Com
PyTorch

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).

torch.is_nonzero() Method

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

torch.is_nonzero() with different data types Let’s try different data types, such as integer, float, and boolean, using a one-element tensor.
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: True
If 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 ambiguous
That’s all!
Post Views: 1
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS