Check if at least one element in tensor is nonzero

Samue1: <p>I want to check if a tensor contains nonzero elements. It does not matter to me how many nonzero elements there are. I currently do the following which is very inefficient for large tensors:</p>
<p><code>T.abs().sum().item() == 0</code></p>
<p>Is there a better way to do this in PyTorch? Like a function that stops iterating through the tensor as soon as it finds the first nonzero element?</p>

Epoching: <p>Not too sure on that, but I do believe that this:</p>
<p><code>T.abs().sum().item() == 0</code></p>
<p>will be much faster than</p>
<blockquote>
<p>a function that stops iterating through the tensor as soon as it finds the first nonzero element?</p>
</blockquote>
<p>as loops/iterators are very very slow <img src="https://discuss.pytorch.org/images/emoji/apple/open_mouth.png?v=12" title=":open_mouth:" class="emoji" alt=":open_mouth:" loading="lazy" width="20" height="20"></p>

Epoching: <p>Also this might be of interest: <code>.nonzero()</code></p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/0/309ced4354af80e5b4ff01550572f48c5ac04e80.png" alt="image" data-base62-sha1="6W38eawukFpTFWcn1HKoZkp2YlW" width="572" height="469"></p>
<p>You can check <code>some_tensor.nonzero()</code> and check for empty results (i.e. <code>tensor([])</code></p>

rmahfuz: <p>tensorName.type(t.uint8).any()</p>

Samue1: <p>I guess <code>tensor_name.any()</code> should work fine. Why the <code>type()</code> call?</p>

rmahfuz: <p>Without typecasting, I get this error:<br>
RuntimeError: all only supports torch.uint8 and torch.bool dtypes<br>
At least with pytorch version 1.7.1</p>

Cmbrsnatch: <p><strong>Using <code>type(torch.uint8)</code> is extremely dangerous.</strong></p>
<p>Let’s say you have a tensor of floats in the range <code>-0.1</code> to <code>0.1</code>.</p>
<p>Casting to uint8 will convert every value to <code>0</code>. Which <code>any()</code> will see as FALSE (“there’s no values”).</p>
<p>Here’s an example of the bug your code causes, believing the tensor is totally empty because it was cast to uint8 zeroes:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; foo = torch.tensor([-0.1, 0.05, 0.3])
&gt;&gt;&gt; foo.type(torch.uint8)
tensor([0, 0, 0], dtype=torch.uint8)
&gt;&gt;&gt; foo.type(torch.uint8).any()
tensor(0, dtype=torch.uint8)
&gt;&gt;&gt; bool(foo.type(torch.uint8).any())
False
</code></pre>
<p>By the way, I think the data type conversion totally defeats the purposes of “any()” to quickly check for any non-zero values, because it would waste time converting every value first. So in that case, it would be more efficient to just use <code>your_tensor.count_nonzero() &gt; 0</code>, which does a slow count of every non-zero value but at least it’s faster and uses less memory than converting <em>everything!</em></p>
<p>Anyway, it’s 2024 and I use Torch “2.3.1+cu118”.</p>
<p><strong>It is totally capable of calling “.any()” on float tensors without any issues. NOTHING needs to be converted. Keep the native types!</strong></p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; x = torch.tensor([0.0, -0.5, 1.2, 50.3])
&gt;&gt;&gt; y = torch.tensor([0.0, 0.0, 0.0, 0.0])
&gt;&gt;&gt; z = torch.tensor([999.0, -0.5, 1.2, 50.3])
&gt;&gt;&gt; x.any()
tensor(True)
&gt;&gt;&gt; y.any()
tensor(False)
&gt;&gt;&gt; z.any()
tensor(True)
&gt;&gt;&gt; x.all()
tensor(False)
&gt;&gt;&gt; y.all()
tensor(False)
&gt;&gt;&gt; z.all()
tensor(True)
</code></pre>
<p><a href="https://pytorch.org/docs/stable/generated/torch.any.html#torch.any" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.any.html#torch.any</a></p>
<p>The code is available here for anyone curious what <code>any()</code> does for different backends:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/search?q=repo%3Apytorch%2Fpytorch%2B%22def%2Bany%28%22&amp;type=code">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/search?q=repo%3Apytorch%2Fpytorch%2B%22def%2Bany%28%22&amp;type=code" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <img width="500" height="500" src="https://github.githubassets.com/assets/github-logo-55c5b9a1fe52.png" class="thumbnail onebox-avatar">

<h3><a href="https://github.com/search?q=repo%3Apytorch%2Fpytorch%2B%22def%2Bany%28%22&amp;type=code" target="_blank" rel="noopener nofollow ugc">Build software better, together</a></h3>

  <p>GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 420 million projects.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


Cmbrsnatch: <p>The link to the PyTorch <code>any()</code> backend code was broken by this forum, so here’s a raw link as text instead:</p>
<pre><code class="lang-auto">https://github.com/search?q=repo%3Apytorch%2Fpytorch+%22def+any%28%22&amp;type=code
</code></pre>