Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.nonzero function

PyTorch torch.nonzero function


PyTorch torch reference manual 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

)

print

(

"Nonzero element indices:"

)

print

(

indices

)

The output is:

非零元素索引:

tensor([[0, 1],

        [1, 0],

        [2, 1]])


PyTorch torch reference manual PyTorch torch reference manual