How does torch::where work in libtorch?

Shisho_Sama: <p>Hello everyone,<br>
I’m porting a python module into c++ and I’m stuck here as how to efficiently port :</p>
<pre><code class="lang-python">np.where(np_array &gt; some_float)
</code></pre>
<p>numpy returns two tuples of indexes. I found out torchlib has a <code>where</code> method as well but it returns a <code>at::autograd::variable_list</code> which I guess is simply a <code>std::vector&lt;torch::Variable&gt;</code> .<br>
I couldnt find any related documentation as how to go about it and whether this equivalent to the <code>np.where</code>!<br>
Running a simple test, shows that the return value of the <code>torch::where</code> seems to be having two tensors as the result. and each seem to represent a set of indexes.<br>
However, Its not clear to me, how to interpret these numbers, since there are duplicate numbers and I cant make anything out of it!<br>
Here is the simple demo :</p>
<pre><code class="lang-cpp">#include &lt;iostream&gt;
#include &lt;torch/torch.h&gt;
#include &lt;torch/script.h&gt;

int main()
{
    torch::Tensor preds = torch::randn({ 4,8 });
    torch::Tensor offsets = torch::randn({ 1, 4, 4, 8 });
    auto res = torch::where(preds &gt; 0.9);
    std::cout &lt;&lt; "res0.size(): " &lt;&lt; res[0].sizes()[0] &lt;&lt; std::endl;
    if (res[0].sizes()[0] == 0)
    {
       std::cout &lt;&lt; "nothing is returned!" &lt;&lt; std::endl;
    }
    
    std::cout &lt;&lt; "preds:\n" &lt;&lt; preds &lt;&lt; std::endl;
    std::cout &lt;&lt; "offsets:\n" &lt;&lt; offsets &lt;&lt; std::endl;

    std::cout &lt;&lt; "res.size: " &lt;&lt; res.size() &lt;&lt; std::endl;
    std::cout &lt;&lt; "res0:\n" &lt;&lt; res[0] &lt;&lt; std::endl;
    std::cout &lt;&lt; "res1:\n" &lt;&lt; res[1] &lt;&lt; std::endl;
    std::cout &lt;&lt; "Hello wordl!" &lt;&lt; std::endl;
    std::system("pause");
}
</code></pre>
<p>this produces this output:</p>
<pre><code class="lang-auto">res0.size(): 9
preds:
-0.9659 -0.9782 -1.7985 -1.5461 -0.2913  0.2766 -1.6117 -0.4642
-1.8279  0.9883 -1.0430 -0.0729  1.4847 -0.8073 -1.8110  0.1578
 0.6299  0.6409  1.0483  0.9996 -0.5102 -0.5886  0.9410  0.3633
-0.4674  1.4806  1.0258 -0.5636  0.9242 -0.1481 -1.8079  2.4731
[ CPUFloatType{4,8} ]
offsets:
(1,1,.,.) =
 -0.1614  0.3842 -0.8396 -0.0390 -0.5792  0.1151 -1.4113  0.4774
  0.1490  1.7119 -0.3051 -0.1585 -1.2240 -0.7920 -0.7085 -0.1590
 -0.6625  1.6923 -0.0494 -0.5374 -0.7239 -0.6688 -1.7225  0.3280
  0.8866  2.4275 -0.4948  1.6180 -0.4133 -0.5967  0.3257 -0.7997

(1,2,.,.) =
 -0.5249  1.8908 -0.5924  1.3851  0.8556  0.4914  0.6360  0.8373
  0.2659 -1.3615  0.1923 -0.9496 -1.8631 -0.1948 -0.3652  0.0215
 -0.2501  2.2011  0.8395 -0.4594 -0.2585  0.1908  0.4444  0.2588
  0.5341 -1.6821  0.3597 -0.0394 -0.0743  0.5159 -0.9592  1.0634

(1,3,.,.) =
 -1.3950 -0.7348  1.1979  1.0431  0.4164 -0.1143  0.0959  0.1557
  2.0382  0.0820  0.2825  0.5211  2.0970  0.2202 -1.4359 -1.0877
  0.1951  0.4328 -0.2317 -0.7700 -0.4143 -1.8647 -0.6055  0.8088
  0.7052 -0.1012  0.3797 -1.1983 -1.0531  0.6381 -0.6158 -0.6769

(1,4,.,.) =
 -1.0985 -1.0719 -0.3173 -0.2694  1.4531 -0.7597  0.4511  0.5871
 -1.2546  1.6486 -1.1799  0.7037 -0.6020 -0.8686  1.3177 -1.1100
  0.4443 -0.7602 -1.3926 -0.0029 -0.9942  1.0145 -1.4573 -0.0027
  0.5276  0.5788  0.7841 -0.1466  0.3800 -0.0146 -0.1285 -2.1790
[ CPUFloatType{1,4,4,8} ]
res.size: 2
res0:
 1
 1
 2
 2
 2
 3
 3
 3
 3
[ CPULongType{9} ]
res1:
 1
 4
 2
 3
 6
 1
 2
 4
 7
[ CPULongType{9} ]
Hello wordl!

</code></pre>
<p>So any help is greatly appreciated</p>

Shisho_Sama: <p>OK! I figured it out!<br>
Each number indicates a value in that row that satisfies the condition.<br>
for example  here :</p>
<pre><code class="lang-auto">1
 1
 2
 2
 2
 3
 3
 3
 3
</code></pre>
<p>it means that in row 1, 2 elements satisfy the condition .in row 2, 3 elements satisfy the condition, and likely the row 3, 4 values satisfy the conditions. in the second part (i.e. res1) the actual indexes for  those values are provided that is :</p>
<pre><code class="lang-auto">1
 4
 2
 3
 6
 1
 2
 4
 7
</code></pre>
<p>1 means, the first value in the row 1<br>
4 means, the 4th value in the row 1<br>
2 means the 2nd value in the row 2<br>
3 means the third value in the row 2<br>
and so on…</p>
<p>to visualize  this, simply use <code>torch::ones</code> instead of <code>randn</code> and see the output like  this :</p>
<pre><code class="lang-auto">res0.shape: 32
preds:
 1  1  1  1  1  1  1  1
 1  1  1  1  1  1  1  1
 1  1  1  1  1  1  1  1
 1  1  1  1  1  1  1  1
[ CPUFloatType{4,8} ]

res.size: 2
res0:
 0
 0
 0
 0
 0
 0
 0
 0
 1
 1
 1
 1
 1
 1
 1
 1
 2
 2
 2
 2
 2
 2
 2
 2
 3
 3
 3
 3
 3
 3
 3
 3
[ CPULongType{32} ]
res1:
 0
 1
 2
 3
 4
 5
 6
 7
 0
 1
 2
 3
 4
 5
 6
 7
 0
 1
 2
 3
 4
 5
 6
 7
 0
 1
 2
 3
 4
 5
 6
 7
[ CPULongType{32} ]
</code></pre>
<p>and it works just lik <code>np.where</code></p>