1

Given a pytorch tensor in dtype=int8:

tensor([[[-3, -6, -1],
         [-6, -10, -1, 
         [9, 9, 6],
         [[-4, -7, -3], 
          [-4, -6, -1], 
          [14, 16, 8]],
         [[-4, -6, -2],
          [-6, -9, -2],
          [9, 10, 5]]], device='cuda:0', dtype=torch.int8)

How do I convert the above tensor into its binary representation? I tried to convert to numpy to use np.unpackbits function but it only takes un-sign integer 8 as input.

3 Answers 3

2

Adapted from my answer for Convert integer to pytorch tensor of binary bits, here's something more concise than the repo from your answer:

a = torch.tensor([[[-3,  -6, -1],
                   [-6, -10, -1], 
                   [ 9,   9,  6]],
                  [[-4,  -7, -3], 
                   [-4,  -6, -1], 
                   [14,  16,  8]],
                  [[-4,  -6, -2],
                   [-6,  -9, -2],
                   [ 9,  10,  5]]], dtype=torch.int8)

def int_to_bits(x, bits=None, dtype=torch.uint8):
    assert not(x.is_floating_point() or x.is_complex()), "x isn't an integer type"
    if bits is None: bits = x.element_size() * 8
    mask = 2**torch.arange(bits-1,-1,-1).to(x.device, x.dtype)
    return x.unsqueeze(-1).bitwise_and(mask).ne(0).to(dtype=dtype)

int_to_bits(a, dtype=torch.float32)

This returns:

tensor([[[[1., 1., 1., 1., 1., 1., 0., 1.],
          [1., 1., 1., 1., 1., 0., 1., 0.],
          [1., 1., 1., 1., 1., 1., 1., 1.]],

         [[1., 1., 1., 1., 1., 0., 1., 0.],
          [1., 1., 1., 1., 0., 1., 1., 0.],
          [1., 1., 1., 1., 1., 1., 1., 1.]],

         [[0., 0., 0., 0., 1., 0., 0., 1.],
          [0., 0., 0., 0., 1., 0., 0., 1.],
          [0., 0., 0., 0., 0., 1., 1., 0.]]],


        [[[1., 1., 1., 1., 1., 1., 0., 0.],
          [1., 1., 1., 1., 1., 0., 0., 1.],
          [1., 1., 1., 1., 1., 1., 0., 1.]],

         [[1., 1., 1., 1., 1., 1., 0., 0.],
          [1., 1., 1., 1., 1., 0., 1., 0.],
          [1., 1., 1., 1., 1., 1., 1., 1.]],

         [[0., 0., 0., 0., 1., 1., 1., 0.],
          [0., 0., 0., 1., 0., 0., 0., 0.],
          [0., 0., 0., 0., 1., 0., 0., 0.]]],


        [[[1., 1., 1., 1., 1., 1., 0., 0.],
          [1., 1., 1., 1., 1., 0., 1., 0.],
          [1., 1., 1., 1., 1., 1., 1., 0.]],

         [[1., 1., 1., 1., 1., 0., 1., 0.],
          [1., 1., 1., 1., 0., 1., 1., 1.],
          [1., 1., 1., 1., 1., 1., 1., 0.]],

         [[0., 0., 0., 0., 1., 0., 0., 1.],
          [0., 0., 0., 0., 1., 0., 1., 0.],
          [0., 0., 0., 0., 0., 1., 0., 1.]]]])
Sign up to request clarification or add additional context in comments.

Comments

0

Change the dtype from torch.int8 to torch.uint8.

Comments

0

This is the required output that I want:

tensor([[[[1., 1., 1., 1., 1., 1., 0., 1.],                                                                                                                                                                        
         [1., 1., 1., 1., 1., 0., 1., 0.],                                                                                                                                                                                    
          [1., 1., 1., 1., 1., 1., 1., 1.]],                                                                                                                                                                                                                                                                                                                                                                                                       
           [[1., 1., 1., 1., 1., 0., 1., 0.],                                                                                                                                                                                    
            [1., 1., 1., 1., 0., 1., 1., 0.],                                                                                                                                                                                    
            [1., 1., 1., 1., 1., 1., 1., 1.]],                                                                                                                                                                                                                                                                                                                                                                                                       
            [[0., 0., 0., 0., 1., 0., 0., 1.],                                                                                                                                                                                    
            [0., 0., 0., 0., 1., 0., 0., 1.],                                                                                                                                                                                    
            [0., 0., 0., 0., 0., 1., 1., 0.]]],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
             [[[1., 1., 1., 1., 1., 1., 0., 0.],                                                                                                                                                                                    
              [1., 1., 1., 1., 1., 0., 0., 1.],                                                                                                                                                                                    
              [1., 1., 1., 1., 1., 1., 0., 1.]],                                                                                                                                                                                                                                                                                                                                                                                                       
              [[1., 1., 1., 1., 1., 1., 0., 0.],                                                                                                                                                                                    
              [1., 1., 1., 1., 1., 0., 1., 0.],                                                                                                                                                                                    
              [1., 1., 1., 1., 1., 1., 1., 1.]],                                                                                                                                                                                                                                                                                                                                                                                                       
              [[0., 0., 0., 0., 1., 1., 1., 0.],                                                                                                                                                                                    
              [0., 0., 0., 1., 0., 0., 0., 0.],                                                                                                                                                                                    
              [0., 0., 0., 0., 1., 0., 0., 0.]]],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
             [[[1., 1., 1., 1., 1., 1., 0., 0.],                                                                                                                                                                                    
               [1., 1., 1., 1., 1., 0., 1., 0.],                                                                                                                                                                                    
               [1., 1., 1., 1., 1., 1., 1., 0.]],                                                                                                                                                                                                                                                                                                                                                                                                       
               [[1., 1., 1., 1., 1., 0., 1., 0.],                                                                                                                                                                                    
               [1., 1., 1., 1., 0., 1., 1., 1.],                                                                                                                                                                                    
               [1., 1., 1., 1., 1., 1., 0., 0.]],                                                                                                                                                                                                                                                                                                                                                                                                       
               [[0., 0., 0., 0., 1., 0., 0., 1.],                                                                                                                                                                                    
               [0., 0., 0., 0., 1., 0., 1., 0.],                                                                                                                                                                                    
               [0., 0., 0., 0., 0., 1., 0., 1.]]]], device='cuda:0',                                                                                                                                                             
               grad_fn=<RemainderBackward0>) 

I found a helpful repo that has a function which converts int8 into binary like above https://github.com/KarenUllrich/pytorch-binary-converter.git

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.