This is a "Bug in pytroch?

AnilNITT: <h1>torch.argsort() function</h1>
<h1>b=torch.tensor([[10,5,78],<br>
[1,108,98],<br>
[90,72,2]])</h1>
<p>b.argsort(axis=0)</p>
<h2>when i m performing this, i have not got correct output:</h2>
<h2>my output is :<br>
[[1, 0, 2],<br>
[0, 2, 0],<br>
[2, 1, 1]]</h2>
<p>But correct output is  after sorting is to this is below, but i haven’t got this output :<br>
[[1, 0, 1],<br>
[0, 2, 2],<br>
[2, 1, 0]]</p>

SimonW: <p>the pytorch output looks correct to me</p>

AnilNITT: <p>axis=0 means for each column wise,<br>
plzz check it again</p>

SimonW: <p>yeah, isn’t 2 &lt; 78 &lt; 98?</p>

tancl: <p>the number tells you which element comes first, or the index of which is smallest.</p>
<p>so the last column, in order of 2-0-1 tells, 2nd element is smallest, followed by e-0 then e-1, which is 2-78-98.</p>
<p>hope this helps.</p>

AnilNITT: <p>but 2-0-1 is not correct accord to all column answers<br>
i think 1-2-0 is correct for this, but those output i have not got there</p>

tancl: <p>201 for last column, pls apply the same concept to the first 2 column</p>
<p>CL</p>

AnilNITT: <p>how u can say that??<br>
10,1,90    i got    [1,0,2]        # indexing in increasing order of no<br>
5,108,72  i tgot   [0,2,1]<br>
78,98,2    i want  [1,2,0]  but i got [2,0,1]    # now u urself calculate what u gor accord to incresing order of no sir.</p>

ptrblck: <p>What am I missing here?</p>
<p>As was said before:</p>
<pre><code class="lang-python">[10, 1, 90] -&gt; index: [1, 0, 2], as this will result in: [1, 10, 90]
[5, 108, 72] -&gt; index: [0, 2, 1], as this will result in: [5, 72, 108]
[78, 98, 2] -&gt; index: [2, 0, 1], as this will result in: [2, 78, 98]
</code></pre>
<p>Why and how should the last index be <code>[1, 2, 0]</code>? This would result in <code>[98, 2, 78]</code>.</p>

AnilNITT: <p>why i got [2,0,1]  for [78,98,2]    ???<br>
this is wrong na?</p>

ptrblck: <p>Why is it wrong? Have a look at my example, as it shows what the sorted result will be.</p>
<p>Let’s maybe talk about what result do you want to get?</p>

AnilNITT: <p>[78,98,2] for this in “argsort” i want [1,2,0]    those i not got<br>
why i got [2,0,1]??  bcoz it wrong for argsort</p>

tancl: <p>may be this explain better.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/0/d0d1498c1f9218cc0058c1a5031f1e69b724e32a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d0d1498c1f9218cc0058c1a5031f1e69b724e32a" title="Untitled"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/0/d0d1498c1f9218cc0058c1a5031f1e69b724e32a_2_690x289.jpeg" alt="Untitled" data-base62-sha1="tNhI0wsa9hrtD7orW7MxP6G4Gz8" width="690" height="289" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/0/d0d1498c1f9218cc0058c1a5031f1e69b724e32a_2_690x289.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/d/0/d0d1498c1f9218cc0058c1a5031f1e69b724e32a.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/0/d0d1498c1f9218cc0058c1a5031f1e69b724e32a.jpeg 2x" data-dominant-color="8A989D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Untitled</span><span class="informations">702×295 31.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

AnilNITT: <p>woooww greatt<br>
u kidding me sir<br>
do u know what ““agrsort”” do actually??<br>
lets take 1 instead o 5 in 2nd column ,it doesnot effect the output 1 is alsi less than =&gt;  108,72<br>
[1,108,72] so according to ur logic it may be   [1,2,0]  but output is  [0,2,1]<br>
means ur logic is failed sir<br>
first undesrstand what “argsort” do then make ur logic sir<br>
thank you</p>

Prashant_Kalikotay: <p><code>torch.</code> <code>argsort</code> ( <em>input</em> , <em>dim=-1</em> , <em>descending=False</em> , <em>out=None</em> ) → LongTensor</p>
<p>Returns the indices that sort a tensor along a given dimension in ascending order by value.</p>
<p>This is the second value returned by <a href="https://pytorch.org/docs/stable/torch.html#torch.sort" rel="noopener nofollow ugc"> <code>torch.sort()</code> </a>. See its documentation for the exact semantics of this method.</p>
<p>Parameters</p>
<ul>
<li><strong>input</strong> (<a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" rel="noopener nofollow ugc"> <em>Tensor</em> </a>) – the input tensor.</li>
<li><strong>dim</strong> (<a href="https://docs.python.org/3/library/functions.html#int" rel="noopener nofollow ugc"> <em>int</em> </a> <em>,</em> <em>optional</em> ) – the dimension to sort along</li>
<li><strong>descending</strong> (<a href="https://docs.python.org/3/library/functions.html#bool" rel="noopener nofollow ugc"> <em>bool</em> </a> <em>,</em> <em>optional</em> ) – controls the sorting order (ascending or descending)</li>
</ul>
<p>Example:</p>
<blockquote>
<blockquote>
<blockquote>
<p>a = torch.randn(4, 4)<br>
a<br>
tensor([[ 0.0785, 1.5267, -0.8521, 0.4065],<br>
[ 0.1598, 0.0788, -0.0745, -1.2700],<br>
[ 1.2208, 1.0722, -0.7064, 1.2564],<br>
[ 0.0669, -0.2318, -0.8229, -0.9280]])<br>
torch.argsort(a, dim=1)<br>
tensor([[2, 0, 3, 1],<br>
[3, 2, 1, 0],<br>
[2, 1, 0, 3],<br>
[3, 2, 1, 0]])</p>
</blockquote>
</blockquote>
</blockquote>
<p>``<br>
understood and pasted from <a href="https://pytorch.org/docs/stable/torch.html#torch.argsort" rel="noopener nofollow ugc">torch.argsort</a></p>
<p>Dear brother <a class="mention" href="/u/anilnitt">@AnilNITT</a> ,<br>
if u would have known what argsort does you would have understood, after these many explanations atleast. Your mindset is wrong.</p>

AnilNITT: <p>torch.argsort(a, dim=1)<br>
tensor([[2, 0, 3, 1],<br>
[3, 2, 1, 0],<br>
[2, 1, 0, 3],<br>
[3, 2, 1, 0]])</p>
<p>dim=1 means row-wise argument sort<br>
can u explain me how u got [2,0,3,1] for 1st row [ 0.0785, 1.5267, -0.8521, 0.4065] ???<br>
bcoz we given index according to increasing order value in all other rows.<br>
if u explain it i got my question answer too bro <a class="mention" href="/u/prashant_kalikotay">@Prashant_Kalikotay</a><br>
<a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/tancl">@tancl</a> <a class="mention" href="/u/simonw">@SimonW</a></p>

ptrblck: <p>I’m not sure at this moment, if you are trolling or why the ascending ordering of these numbers is not clear enough.</p>
<p>Let’s take the example you are mentioning:</p>
<pre><code class="lang-python">x = [ 0.0785, 1.5267, -0.8521, 0.4065] 
</code></pre>
<p>The output (as given by <a class="mention" href="/u/prashant_kalikotay">@Prashant_Kalikotay</a>) is</p>
<pre><code class="lang-python">[2,0,3,1]
</code></pre>
<p>which gives the ordering:</p>
<pre><code class="lang-python">[x[2], x[0], x[3], x[1]] = [-0.8521, 0.0785, 0.4065, 1.5267]
</code></pre>
<p>since</p>
<pre><code class="lang-python">-0.8521 &lt; 0.0785 &lt; 0.4065 &lt; 1.5267
</code></pre>
<p>Which part is confusing you?</p>

tom: <p>Like <a class="mention" href="/u/ptrblck">@ptrblck</a>, I think taking a step back and noticing that no less than four kind people have been going out of their way trying to help might be useful. Keeping things friendly seems not only nice but also essential to your cause and the forums in general.</p>
<p>My impression is that the confusion is between the permutation mapping unordered to ordered (returned by argsort) and the inverse, mapping ordered to unordered.</p>
<p>In the 2d case, you can invert the permutation using scatter (in 1d, indexed assignment <code>c[b] = torch.arange(...)</code> is quicker and more obvious) if you need the inverse.</p>
<pre><code class="lang-python">a = torch.randn(4,4)
b = a.argsort(dim=1)

c = torch.scatter(torch.zeros_like(b), 1, b, torch.arange(b.size(1))[None, :].expand_as(b))

# check that c is the inverse of b
print(b.gather(1, c))
# c, the inverse permutation of what argsort returns,  probably is what you are looking for
print(c)

# or you can get the sorted values and rearrange to match the original using the inverse permutation
print((a.gather(1, b)==a.sort(1).values).all().item())
print(((a.sort(1).values).gather(1, c)==a).all().item())
</code></pre>
<p>Best regards</p>
<p>Thomas</p>