Updating pytorch tensor by matching multiple boolean conditions on the original values
I currently am updating values in a Pytorch tensor using multiple OR conditions:
&gt;&gt;&gt; import torch

&gt;&gt;&gt; my_tensor = torch.tensor([0, 1, 2, 3, 4, 5])

&gt;&gt;&gt; condition = ((my_tensor==1) | (my_tensor==4) | (my_tensor==5))
&gt;&gt;&gt; my_tensor[condition] = 0

&gt;&gt;&gt; my_tensor
[0, 0, 2, 3, 0, 0]

My list of conditions is much longer than the toy example above. Can the condition operator match a list? If not, what is the best solution?

How about using a combination of torch.where and  torch.isin like below:
&gt;&gt;&gt; torch.where(torch.isin(my_tensor, torch.tensor([1,4,5])), 0, my_tensor)
tensor([0, 0, 2, 3, 0, 0])

Update, Second approach: We can use torch.reshape.
(torch.isin not exist in pytorch==1.9.1 as you say in comment)
&gt;&gt;&gt; mask = (my_tensor == torch.reshape(torch.tensor([1,4,5]), (-1,1))).any(0)
&gt;&gt;&gt; torch.where(mask, 0, my_tensor)

