How to do masks on multiple conditions?

I have a torch.autograd.variable.Variable ‘target’ with data of type torch.cuda.LongTensor. I want to run this:

target = target[mask]

This works:
mask = target >=0

This:

mask = target >= 0 and target < 128

doesn’t work:

RuntimeError: bool value of Variable objects containing non-empty torch.cuda.ByteTensor is ambiguous.

What am I doing wrong?

2 Likes

Since the condition returns ones and zeros, a bitwise and (&) should work:

target = Variable(torch.LongTensor(10).random_(256))
mask = (target >= 0) & (target < 128)
target[mask]
9 Likes