How do use logical not on ByteTensor?

I can use the &, the |, ^ operators on a ByteTensor, but the ~ logical operator doesn’t work. Is there an efficient way of applying it?

We should probably add support for the ~ operator. In the meantime, instead of ~x you can use (1 - x).

4 Likes

I don’t think you can use (1-x) if x is a variable. Negation is not possible on bytetensor variables. You should do something like (1-x.data) which is pretty ugly and perhaps inefficient for something as simple as logical not.

1 Like

Just met with this problem.

Actually we can just achieve ~x by x ^ 1.

1-x may not work because it will return a FloatTensor, which may result in some error.

3 Likes

It’s now 2019: ~ is available for ByteTensors, and it returns (1 - x), but it’s undocumented as far as I can tell. x.neg() does bitwise negation instead.

1 Like

No x.neg() does not do bitwise negation. It computes -x which is the additive inverse of x in the field 2^8 (for ByteTensors). Another way to write that is x.neg() = -x = (256 - x) % 256.

I see, then what’s surprising is that ~ does not do bitwise negation (it’s a bitwise operator in python, but a logical(?) operator in pytorch, but pytorch does not really have booleans).

@bluehood, yes! The problem is that PyTorch does not really have bool Tensors yet. But they’re coming soon.

2 Likes

Ah that’s great to hear, I work with binary data and binary network units a lot :smile:

@colesbury PyTorch 1.2 introduced byte tensors but broke all code relying on the behavior of ~ for byte tensors. It’s now doing bitwise negation instead of logical negation! Was there a deprecation warning I skipped?

No runtime warning. See the release notes for the breaking changes.

We realized after the release that it may have been better to spread the change to byte Tensors over two releases, but by that point it was too late (i.e. we could have deprecated ~ on byte tensors in 1.2 and change it to bitwise inversion for non-bool integer types in 1.3).

I see…I didn’t think you’d introduce major breaking changes without deprecation notice between minor versions. Thanks for the link, super useful!

Cheers,
Enrico