How to do not equal to

a.eq(b) is used to do element wise comparison, where it returns True if the values are equal. How to do not equal to? where True is returned in places where elements are not equal

The ~ operator negate the bool tensor:

import torch
a = torch.tensor([1, 2])
b = torch.tensor([1, 0])
print(a.eq(b))
print(~a.eq(b))
tensor([ True, False])
tensor([False,  True])
1 Like