PyTorch torch.nonzero function
PyTorch torch reference manual
torch.nonzero is a function in PyTorch that returns the indices of nonzero elements.
Function definition
torch.nonzero(input)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
[
0
,
1
]
,
[
2
,
0
]
,
[
0
,
3
]
]
)
Return the indices of the nonzero elements
indices
=
torch.
nonzero
(
x
)
(
"Nonzero element indices:"
)
(
indices
)
The output is:
非零元素索引:
tensor([[0, 1],
[1, 0],
[2, 1]])