Skip to content

Commit

Permalink
[Term Entry] PyTorch Tensors .argwhere() (#5742)
Browse files Browse the repository at this point in the history
Co-authored-by: Daksha Deep <[email protected]>
  • Loading branch information
mamtawardhani and dakshdeepHERE authored Dec 19, 2024
1 parent 0792659 commit 1484279
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions content/pytorch/concepts/tensors/terms/argwhere/argwhere.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
Title: '.argwhere()'
Description: 'Returns the indices of elements in a tensor that satisfy a specified condition, arranged in a 2D tensor.'
Subjects:
- 'AI'
- 'Data Science'
Tags:
- 'AI'
- 'Deep Learning'
- 'Functions'
- 'Machine Learning'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'py-torch-for-classification'
---

In PyTorch, **`.argwhere()`** returns the indices of elements in a tensor that satisfy a specified condition. It is useful for finding the positions of elements in a tensor that meet specific conditions, such as values greater than a threshold.

## Syntax

```pseudo
torch.argwhere(input)
```

- `input`: A tensor containing the elements to be checked. The condition will be applied to this tensor.

It returns a 2D tensor containing the indices of the elements in the input tensor that satisfy the specified condition. Each row in the resulting tensor represents the indices of an element that meets the condition.

## Example

In this example, `.argwhere()` is used to find the indices of elements in the tensor that are greater than _0_, equal to _0_, and less than _2_:

```py
import torch

# Define a tensor
tensor = torch.tensor([[0, 1], [2, 0], [-1, 3]])

# Case 1: Use argwhere to find indices of elements greater than 0
indices_case_1 = torch.argwhere(tensor > 0)

# Case 2: Use argwhere to find indices of elements equal to 0
indices_case_2 = torch.argwhere(tensor == 0)

# Case 3: Use argwhere to find indices of elements less than 2
indices_case_3 = torch.argwhere(tensor < 2)

print("Case 1 (elements > 0):", indices_case_1)
print("Case 2 (elements == 0):", indices_case_2)
print("Case 3 (elements < 2):", indices_case_3)
```

Here is the output for the above example:

```shell
Case 1 (elements > 0): tensor([[0, 1],
[1, 0],
[2, 1]])
Case 2 (elements == 0): tensor([[0, 0],
[1, 1]])
Case 3 (elements < 2): tensor([[0, 0],
[0, 1],
[1, 1],
[2, 0]])
```

0 comments on commit 1484279

Please sign in to comment.