chico0913
chico0913

Reputation: 647

how to check whether a certain number is in the Pytorch tensor?

for a Pytorch tensor A:

A = tensor([1,0,0],
           [0,0,0])

is there way I can check whether the number 1 is an element of the tensor A? like is there a pytorch function that returns True is 1 is an element of A, and returns False if 1 is not an element of A?

Thank you,

Upvotes: 6

Views: 14614

Answers (1)

GoodDeeds
GoodDeeds

Reputation: 8507

torch.Tensor implements __contains__. So, you can just use:

1 in A

This returns True if the element 1 is in A, and False otherwise.

Upvotes: 10

Related Questions