Search code examples
luatorch

Bitwise or of 2 tensors torch


How do I perform bitwise and/or operations on two tensors in torch?

Lets us say I have two ByteTensors a and b, I want to compute logical and/or between them. Is it possible to do it using a function?


Solution

  • There is no bitwise and/or operation for tensors in Torch. However, if you could convert each bit as a separate Tensor dimension (e.g. on your data preparation step, or with bitop), you can use Torch.any(x) as element wise or operation.

    Update: it makes more sense to use torch.cmul and torch.add as mattdns suggests

    a = torch.Tensor{0,1,1,0}
    b = torch.Tensor{0,1,0,1}
    
    th> torch.cmul(a,b):eq(1)
     0
     1
     0
     0
    [torch.ByteTensor of size 4]
    
    th> torch.add(a,b):ge(1)
     0
     1
     1
     1
    [torch.ByteTensor of size 4]