Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you invert a tensor of boolean values in Pytorch?

With NumPy, you can do it with np.invert(array), but there's no invert function in Pytorch. Let's say I have a 2D tensor of boolean values:

import torch

ts = torch.rand((10, 4)) < .5
tensor([[ True,  True, False,  True],
        [ True,  True,  True,  True],
        [ True, False,  True,  True],
        [False,  True,  True, False],
        [False,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True, False,  True,  True],
        [False,  True, False,  True],
        [ True,  True, False,  True],
        [False, False,  True, False]])

How do I transform the False into True and vice versa?

like image 482
Mr.TripleDouble Avatar asked Dec 03 '19 01:12

Mr.TripleDouble


1 Answers

Literally just use the tilde to transform all True into False and vice versa.

ts = ~ts
like image 160
Nicolas Gervais Avatar answered Sep 27 '22 19:09

Nicolas Gervais