The logical "or/and/not" function about pytorch

weixia-cs: <p>Compute the truth value of x1 OR/AND/NOT x2 element-wise in <strong>Numpy</strong> is <a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.logical_or.html" rel="nofollow noopener">numpy.logical_or</a>  , <a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.logical_and.html#numpy.logical_and" rel="nofollow noopener">numpy.logica_and</a> , <a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.logical_not.html#numpy.logical_not" rel="nofollow noopener">numpy.logical_not</a></p>
<p>The same function of <strong>TensorFlow</strong> is <a href="https://www.tensorflow.org/api_docs/python/tf/math/logical_or" rel="nofollow noopener">tf.logical_or</a> , <a href="https://www.tensorflow.org/api_docs/python/tf/math/logical_and" rel="nofollow noopener">tf.logical_and</a>, <a href="https://www.tensorflow.org/api_docs/python/tf/math/logical_not" rel="nofollow noopener">tf.logical_not</a></p>
<p>and i do not  found in PyTorch docs, Is there some equivalent in PyTorch and if not, would it be possible to add these functions?</p>

albanD: <p>On a byte tensorz you can use the python syntax for them or directly <code>__or__</code>, <code>__and__</code> etc</p>

drevicko: <p>Awesome. That means the python bitwise operators work like elementwise logical operators. For unit8 tensors <code>a</code> and <code>b</code> and numpy bool array equivalents <code>A</code> and <code>B</code>:</p>
<pre><code class="lang-auto">a | b ~= np.logical_or (A, B)
a &amp; b ~= np.logical_and (A, B)
a ^ b ~= np.logical_xor (A, B)
</code></pre>

Deepak: <p>Hi <a class="mention" href="/u/alband">@albanD</a>,</p>
<p>Kindly help me with two concerns, regarding the logical xor operation between two tensors:</p>
<ol>
<li>Would the python syntax like <code>^</code> (xor operation) be consistent with tensor.backwards() gradient flow?</li>
<li>The PyTorch version that my docker image has; it supports torch.bool type but not torch.logical_xor() operation. So, I used <code>bool_tensor_1 ^ bool_tensor_1</code> . How is the efficiency of <code>^</code> compared to <code>torch.logical_xor</code> ?</li>
</ol>

albanD: <p>Hi,</p>
<ol>
<li>The inputs to xor are binary. So gradients don’t really exist. We can only provide gradients for contiguous number spaces (float/double).</li>
<li>I am not sure about the difference, but they should be routing to the same implementation under the hood.</li>
</ol>

Deepak: <p>Does that mean that:</p>
<ol>
<li>In the loss function calculation, there should be no boolean data/number from starting to end?</li>
<li>Also, there should be no boolean operator from starting to end (in loss function calculation)?</li>
<li>If I do <code>float_tensor ^ float_tensor</code>, it’s gradient would not be a valid thing?</li>
</ol>
<p>Why my program didn’t throw any error ? How has it interpreted the differentiation across xor function, and differentiation of boolean numbers ?</p>

albanD: <ol>
<li>If you want to be able to use automatic differentiation, yes. Note that in classical ML, this is also why when your original loss has non-continuous values, you introduce a surrogate loss to be able to optimize something.</li>
<li>Same as 1</li>
<li>If you try to run this code with float tensors that require gradients, you will get the following error: “arguments don’t support automatic differentiation”. So if your code ran fine, it was because the inputs did not require gradients.</li>
</ol>

drevicko: <p>These also work for <code>torch.bool</code> tensors (introduced in pytorch 1.2).</p>
<p>There’s also an element-wise not operation:</p>
<pre><code class="lang-auto">~a  == np.logical_not (A)  
</code></pre>
<p>In pytorch 1.4+, this works for both <code>ByteTensor</code>s and <code>BoolTensor</code>s, however in pytorch 1.2 (1.3 also?) it only worked for <code>ByteTensor</code>s (ie: uint8).</p>