How to judge a Tensor is in a list?

leftthomas: <p>Here is an example which I want to do:</p>
<pre><code class="lang-auto">import torch
a = torch.randn(3)
b = torch.randn(3)
c = [a]
if a in c:
    .....
......
if b in c:
    .....
</code></pre>
<p>But this code will encounter an issue, which raise</p>
<blockquote>
<p>RuntimeError: bool value of non-empty torch.ByteTensor objects is ambiguous</p>
</blockquote>
<p>So how can I  judge if a Tensor in a list?</p>

jpeg729: <p>Some quick testing on version 0.4.0a0+a3d08de …</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; a = torch.randn(3)
&gt;&gt;&gt; b = torch.randn(3)
&gt;&gt;&gt; a in [a]
True
&gt;&gt;&gt; b in [a]
RuntimeError: bool value of Tensor with more than one value is ambiguous
</code></pre>

ptrblck: <p>You could try the following code:</p>
<pre><code class="lang-auto">a = torch.randn(3)
b = torch.randn(3)
c = [a, b]
d = [torch.randn(3), torch.randn(3)]

if any([(a == c_).all() for c_ in c]):
    print('a in c')
    
if any([(a == d_).all() for d_ in d]):
    print('a in d')
</code></pre>
<p>This code iterates the entries of the lists <code>c</code> and <code>d</code> and compares each entry to the <code>Tensor</code> you would like to check.<br>
Since <code>(a == c_)</code> returns the result for each value, we could call <code>.all</code> on it and finally check if any entry of the list gives a positive result.</p>
<p>EDIT: This approach needs <code>Tensors</code> of the same shape, which might not be useful in some use cases.</p>
<p>EDIT2: This might be a workaround:</p>
<pre><code class="lang-auto">d = [torch.randn(5), torch.randn(3)]
if any([(a == d_).all() for d_ in d if a.shape == d_.shape]):
    print('a in d')
</code></pre>

albanD: <p>Hi,</p>
<p>If you want to find if a tensor with same content is in the list, then <a class="mention" href="/u/ptrblck">@ptrblck</a> looks like the right solution.<br>
If you want to find if the same tensor is in the list, you can use:</p>
<pre><code class="lang-auto">a = torch.randn(5)
d = [a, torch.randn(3)]
if any(a is d_ for d_ in d):
   print('a in d')
# a will be in d here

d = [a.clone(), torch.randn(3)]
if any(a is d_ for d_ in d):
   print('a in d')
# a will NOT be in d here
</code></pre>

leftthomas: <p>Thanks for you reply, I know how to use <code>for loop</code> to solve this problem, I wonder if there are another way which don’t use <code>for loop</code> to solve this, a more efficient way.</p>

jpeg729: <p>While this is an ugly hack, and isn’t guaranteed to work, it might be quite efficient.</p>
<pre><code class="lang-auto">try:
    a in c
    do stuff assuming a is in c
except RuntimeError:
    do stuff assuming a is not in c
</code></pre>
<p>Besides isn’t it a bug that pytorch throws an error only when a is not in c?</p>

Saruar_Alam: <p>How about this way</p>
<pre><code class="lang-auto">setensor = torch.randint(0, 10, size = (5, 20))
selist = [1, 2, 4, 6]
print(setensor)
semasks = torch.any(torch.stack([torch.eq(setensor, aelem).logical_or_(torch.eq(setensor, aelem)) for aelem in selist], dim=0), dim = 0)
print(semasks)

</code></pre>

seyeeet: <p>not sure how did you come up with this but you save me a lot of headache! thanks</p>