Torch.unique not returning expecting values

haltaha: <p>I have a tensor of size (2,3), after running it through <code>torch.unique</code>, I get a tensor size of (2,3) for the unique but only get a tensor size of (1,3) for the counts when <code>dim=1</code>. I was expecting that I get (2,3)</p>
<p>Test code:</p>
<pre><code class="lang-auto">x = torch.tensor([[2, 2, 1], [0, 1, 2]])
u, c = torch.unique(x, dim=1, return_counts=True)
</code></pre>
<p>Is there a problem with the implementation or have I misunderstood the documentation.</p>

ptrblck: <p>The returned counts tensor <code>c</code> should have the shape <code>[3]</code> (not <code>[1, 3]</code>), which is also what I get.<br>
This is the expected shape, since the counts tensor will have the shape <code>output.size(dim)</code>, if <code>dim</code> was specified. From the docs:</p>
<blockquote>
<ul>
<li><strong>counts</strong> (<em>Tensor</em>): (optional) if <code>return_counts</code> is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of ocurrences          for each unique value or tensor.</li>
</ul>
</blockquote>
<p>Note, that you will get the unique “columns” in your example.<br>
This code snippet might give you a better idea, as it contains duplicated columns:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.tensor([[2, 2, 1], [0, 0, 2]])
u, c = torch.unique(x, dim=1, return_counts=True)
print(u)
&gt; tensor([[1, 2],
          [2, 0]])
print(c)
&gt; tensor([1, 2])
</code></pre>
<p>Here you can see, that two unique columns were found, where <code>[[2], [0]]</code> is duplicated and thus has a count of 2.</p>

JosueCom: <p>For the following code:</p>
<pre><code class="lang-python">votes = tensor([[2, 2, 1],
                [1, 1, 2]])

uni, count = th.unique(votes, dim=1, return_counts=True)

### output
uni = tensor([[1, 2],
              [2, 1]])
count = tensor([1, 2])
</code></pre>
<p>When I run it, <code>count</code> returns a shape of (2) instead of (2, 2) unlike <code>uni</code>. I understand your explanation above but how can one get the count for each row instead?</p>

ptrblck: <p>To get the count for each row, you would have to use <code>dim=0</code> in the <code>torch.unique</code> call.<br>
However, neither <code>dim=0</code> would return a count in the shape <code>[2, 2]</code>, as you are then only counting unique rows.</p>

JosueCom: <p>Makes sense</p>
<p>How can one get the count for each unique element in each row of the <code>votes</code> tensor? Resulting in a 2d array e.i. running unique on each row with count</p>

ptrblck: <aside class="quote no-group" data-username="JosueCom" data-post="5" data-topic="91979">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/josuecom/48/27043_2.png" class="avatar"> JosueCom:</div>
<blockquote>
<p>Resulting in a 2d array e.i. running unique on each row with count</p>
</blockquote>
</aside>
<p>This wouldn’t necessary result in a 2-dimensional tensor, since each row might contain a different number of unique elements and I think you would need to iterate the rows:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.tensor([[0, 0, 0],
                  [1, 1, 2]])

for x_ in x.split(1, 0):
    print(torch.unique(x_, return_counts=True))

&gt; (tensor([0]), tensor([3]))
  (tensor([1, 2]), tensor([2, 1]))
</code></pre>

JosueCom: <p>Thanks. I think I will do that.</p>
<p>I was trying to avoid doing it since it is running in the gpu but I guess it is fine.</p>
<p>My goal is to implement knn and this section of the code is meant to count the votes of the labels</p>