RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead

neeraj1909: <p>I have implemented a function to thin an image using numpy package. Now, I tried to convert that implementation using pytorch.</p>
<p>Numpy Implementation:</p>
<pre><code class="lang-auto"># calculate dx and dy - this is correct as can be seen in a picture
dx = ((orien &lt; ((3/8) * np.pi)).astype(np.float32) - (orien &gt; ((5/8) * np.pi)).astype(np.float32)).astype(bool)
dy = (((orien &gt; ((1/8) * np.pi)) &amp; (orien &lt;= ((1/2) * np.pi))).astype(np.float32) - ((orien &gt; ((1/2) * np.pi)) &amp; (orien &lt; ((7/8) * np.pi))).astype(np.float32)).astype(bool)
                
# normally the same pixels would be checked if (dy and dx &gt; 0) and (dy and dx &lt; 0). because
# different pixels should be checked with a gabor-orientation of 45 and 135 this difference should
# be checked
                
if (dy &lt; 0) &amp; (dx &lt; 0):
    result[i,j] = np.multiply(((matrixB[i,j] &gt;= matrixB[i+dy, j+dx]) &amp; (matrixB[i,j] &gt;= matrixB[i-dy, j-dx])), matrixB[i,j])
else:
    result[i,j] = np.multiply(((matrixB[i,j] &gt;= matrixB[i-dy, j+dx]) &amp; (matrixB[i,j] &gt;= matrixB[i+dy, j-dx])), matrixB[i,j])
</code></pre>
<p>Corresponding torch code:</p>
<pre><code class="lang-auto">dx = ((orien &lt; ((3/8) * torch.pi)).to(torch.float32) - (orien &gt; ((5/8) * torch.pi)).to(torch.float32)).to(torch.bool)
dy = (((orien &gt; ((1/8) * torch.pi)) &amp; (orien &lt;= ((1/2) * torch.pi))).to(torch.float32) - ((orien &gt; ((1/2) * torch.pi)) &amp; (orien &lt; ((7/8) * torch.pi))).to(torch.float32)).to(torch.bool)


if (dy &lt; 0) &amp; (dx &lt; 0):
    result[i,j] = torch.mul(((matrixB[i,j] &gt;= matrixB[i+dy, j+dx]) &amp; (matrixB[i,j] &gt;= matrixB[i-dy, j-dx])), matrixB[i,j])
else:
    result[i,j] = torch.mul(((matrixB[i,j] &gt;= matrixB[i-dy, j+dx]) &amp; (matrixB[i,j] &gt;= matrixB[i+dy, j-dx])), matrixB[i,j])
</code></pre>
<p>I am getting the following error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[18], line 29
     26 plt.imshow(output_image, cmap='gray')
     27 plt.show()
---&gt; 29 thinning_output = thinning(output_image, oriens_matrix, 1)
     30 plt.imshow(thinning_output, cmap='gray')
     31 plt.show()

File ~/Code/corf_dst/push_pull_corf/thin_image.py:43, in thinning(matrix, oriens_matrix, method)
     40                 result[i,j] = torch.mul(((matrixB[i,j] &gt;= matrixB[i+dy, j+dx]) &amp; ~(matrixB[i,j] &gt;= matrixB[i-dy, j-dx])), matrixB[i,j])
     41                 # result[i,j] = torch.mul(((matrixB[i,j] &gt;= matrixB[i+dy, j+dx]) &amp; torch.logical_not((matrixB[i,j] &gt;= matrixB[i-dy, j-dx]))), matrixB[i,j])
     42             else:
---&gt; 43                 result[i,j] = torch.mul(((matrixB[i,j] &gt;= matrixB[i-dy, j+dx]) &amp; ~(matrixB[i,j] &gt;= matrixB[i+dy, j-dx])), matrixB[i,j])
     44                 # result[i,j] = torch.mul(((matrixB[i,j] &gt;= matrixB[i-dy, j+dx]) &amp; torch.logical_not((matrixB[i,j] &gt;= matrixB[i+dy, j-dx]))), matrixB[i,j])
     45 
     46 
     47 else: # linear thinning
     48     for i in range(1, h+1): # for rows

File /home/anaconda3/envs/neeraj/lib/python3.9/site-packages/torch/_tensor.py:39, in _handle_torch_function_and_wrap_type_error_to_not_implemented.&lt;locals&gt;.wrapped(*args, **kwargs)
     37     if has_torch_function(args):
     38         return handle_torch_function(wrapped, args, *args, **kwargs)
---&gt; 39     return f(*args, **kwargs)
     40 except TypeError:
     41     return NotImplemented

File /home/anaconda3/envs/neeraj/lib/python3.9/site-packages/torch/_tensor.py:834, in Tensor.__rsub__(self, other)
    832 @_handle_torch_function_and_wrap_type_error_to_not_implemented
    833 def __rsub__(self, other):
--&gt; 834     return _C._VariableFunctions.rsub(self, other)

RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.
</code></pre>
<p>Can anyone please help me out?</p>

ptrblck: <p>Based on the error message and the code it seems you are trying to subtract a <code>bool</code> tensor from an index value <code>i</code> and <code>j</code>.<br>
In this case, transform the <code>bool</code> to <code>long</code> and the subtraction as well as the indexing should work: <code>i - dy.long()</code>.</p>