Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if tensor is in list

I can do the following with a single int to retrieve a bool tensor:

import torch
a = torch.tensor([1,2,3])
a != 2
#tensor([ True, False,  True])

Can I do the same with a list in plain pytorch? I.e.:

import torch
a = torch.tensor([1,2,3])
a not in [2,3]
#tensor([ True, False,  False])

Thanks a lot for your time!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you want torch.isin

out = ~torch.isin(a, torch.tensor([2, 3]))
# or
out = torch.isin(a, torch.tensor([2, 3]), invert=True)
print(out)

tensor([ True, False, False])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading