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.argwhere() Method in PyTorch

Home torch.argwhere() Method in PyTorch
PyTorch torch.argwhere() with various code examples
  • Written by krunallathiya21
  • May 14, 2025
  • 0 Com
PyTorch

The torch.argwhere() method returns the indices of non-zero elements, providing a convenient way to locate elements that satisfy a condition.

torch.argwhere() method

In short, it returns a 2D tensor of shape (N, D), where,

  1. N is the number of non-zero elements.
  2. D is the number of dimensions in the input tensor.

Each row contains the indices of a non-zero element.

The torch.argwhere() method differs from torch.where(), which returns indices for elements satisfying a condition but is typically used with a condition tensor and can return multiple tensors.

Syntax

torch.argwhere(input)

Parameters

Argument Description
input (Tensor) It is an input tensor of any shape in which the method identifies where elements are non-zero (or True for boolean tensors).

Finding Non-Zero elements in a Numerical Tensor

Let’s identify the indices of non-zero elements in a 2D tensor.

You can locate any value in the 2D tensor or matrix by its rows and columns, which start with the 0th index.

import torch

tensor = torch.tensor([[0, 1],
                       [2, 0]])

indices = torch.argwhere(tensor)

print(indices)

# Output:
# tensor([[0, 1],
#         [1, 0]])
Row Column Value Is Non-Zero?
0 0 0 No
0 1 1 Yes
1 0 2 Yes
1 1 0 No
So, the non-zero elements are:
  1. At position [0, 1] → value 1
  2. At position [1, 0] → value 2

Hence, torch.argwhere(tensor) returns: tensor[[0, 1], [1, 0]].

Working with Boolean tensors

argwhere() with boolean tensor

Locate True values in a Boolean tensor.
import torch

bool_tensor = torch.tensor([[False, True, False],
                           [True, False, True]])

indices = torch.argwhere(bool_tensor)

print(indices)

# Output:
# tensor([[0, 1],
#         [1, 0],
#         [1, 2]])

At position [0, 1], there is a True value. At position [1, 0], there is a True value; at position [1, 2], there is a True value.

Higher-Dimensional Tensors

Let’s locate the non-zero elements in a 3D tensor.

import torch

tensor_3d = torch.tensor([[[0, 1], [0, 0]],
                          [[21, 0], [13, 41]]])
indices = torch.argwhere(tensor_3d)

print(indices)

# Output:
# tensor([[0, 0, 1],
#         [1, 0, 0],
#         [1, 1, 0],
#         [1, 1, 1]])

Empty Tensor

Let’s check out the scenario where the tensor has no non-zero elements.

For example, if we created an input tensor using the torch.zeros() method, it would have no non-zero elements.

So, this method will return an empty tensor due to no non-zero elements.

import torch

zero_tensor = torch.zeros(2, 3)

indices = torch.argwhere(zero_tensor)

print(indices)

# Output:

# tensor([], size=(0, 2), dtype=torch.int64)

Using with Conditions

One advantage of this method is that it can be used with conditions to find indices that meet specific criteria.

import torch

tensor = torch.tensor([[11, 2, 3],
                       [4, 5, 61]])

indices = torch.argwhere(tensor > 10)

print(indices)

# Output:
# tensor([[0, 0],
#         [1, 2]])

In the above code, the condition tensor > 10 creates a boolean tensor, and argwhere returns indices where the condition is True.

GPU Compatibility

You can use this method on a GPU without any errors.

import torch

tensor = torch.tensor([[0, 1, 0],
                       [2, 0, 3]], device='cuda')

indices = torch.argwhere(tensor)

print(indices)
  Running torch.argwhere() method on the GPU That’s all!
Post Views: 7
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