How do use logical not on ByteTensor?

username: truenicoco
cooked: <p>I can use the <code>&amp;</code>, the <code>|</code>, <code>^</code> operators on a ByteTensor, but the <code>~</code> logical operator doesn’t work. Is there an efficient way of applying it?</p>

username: colesbury
cooked: <p>We should probably add support for the ~ operator. In the meantime, instead of ~x you can use (1 - x).</p>

username: Tofigh_Naghibi
cooked: <p>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.</p>

username: AlbertM
cooked: <p>Just met with this problem.</p>
<p>Actually we can just achieve ~x by x ^ 1.</p>
<p>1-x may not work because it will return a FloatTensor, which may result in some error.</p>

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

username: colesbury
cooked: <p>No x.neg() does not do bitwise negation. It computes <code>-x</code> which is the additive inverse of <code>x</code> in the field 2^8 (for ByteTensors). Another way to write that is <code>x.neg() = -x = (256 - x) % 256.</code></p>

username: bluehood
cooked: <p>I see, then what’s surprising is that <code>~</code> does not do bitwise negation (it’s a bitwise operator in python, but a logical(?) operator in pytorch, <em>but</em> pytorch does not really have booleans).</p>

username: colesbury
cooked: <p><a class="mention" href="/u/bluehood">@bluehood</a>, yes! The problem is that PyTorch does not really have bool Tensors <strong>yet</strong>. But they’re coming soon.</p>

username: bluehood
cooked: <p>Ah that’s great to hear, I work with binary data and binary network units a lot <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:"></p>

username: bluehood
cooked: <p><a class="mention" href="/u/colesbury">@colesbury</a> PyTorch 1.2 introduced byte tensors but broke all code relying on the behavior of <code>~</code> for byte tensors. It’s now doing bitwise negation instead of logical negation! Was there a deprecation warning I skipped?</p>

username: colesbury
cooked: <p>No runtime warning. See the release notes for the breaking changes.</p>
<p>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 <code>~</code> on byte tensors in 1.2 and change it to bitwise inversion for non-bool integer types in 1.3).</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/pytorch/pytorch/releases" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/21003710?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/pytorch/pytorch/releases" target="_blank" rel="nofollow noopener">pytorch/pytorch</a></h3>

<p>Tensors and Dynamic neural networks in Python with strong GPU acceleration - pytorch/pytorch</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: bluehood
cooked: <p>I see…I didn’t think you’d introduce major breaking changes without deprecation notice between minor versions. Thanks for the link, super useful!</p>
<p>Cheers,<br>
Enrico</p>