更新:在 Pytorch 1.2 中,PyTorch 引入了torch.bool 数据类型,可以使用torch.BoolTensor:
>>> a = torch.BoolTensor([False, True, True, False]) # or pass [0, 1, 1, 0]
>>> b = torch.BoolTensor([True, True, False, False])
>>> a & b # logical and
tensor([False, True, False, False])
PyTorch 支持对 ByteTensor 的逻辑操作。您可以使用&、|、^、~ 运算符进行逻辑运算,如下所示:
>>> a = torch.ByteTensor([0, 1, 1, 0])
>>> b = torch.ByteTensor([1, 1, 0, 0])
>>> a & b # logical and
tensor([0, 1, 0, 0], dtype=torch.uint8)
>>> a | b # logical or
tensor([1, 1, 1, 0], dtype=torch.uint8)
>>> a ^ b # logical xor
tensor([1, 0, 1, 0], dtype=torch.uint8)
>>> ~a # logical not
tensor([1, 0, 0, 1], dtype=torch.uint8)