shlogg · Early preview
Super Kai (Kazuya Ito) @superkai_kazuya

Understanding Argwhere() And Nonzero() In PyTorch

argwhere() and nonzero() explained: get indices of non-zero elements in tensors with torch or PyTorch.

Buy Me a Coffee☕
*Memos:

My post explains where().
My post explains count_nonzero().

argwhere() can get the 2D tensor of the zero or more indices of non-zero elements from the 0D or more D tensor of zero or more elements as shown below:
*Memos:

argwhere() can be used with torch or a tensor.
The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).


import torch
my_tensor = torch.tensor(5)
torch.argwhere(input=my_tensor)
my_tensor.argwhere()
# tensor([], size=(1, 0), dtype=torch.int64)
my_tensor = torch.tensor([5, 0, 4, 0, 3, 1])
torch.argwh...