Tensor miss a dimension with advanced indexing

username: dio_din
cooked: <p>Hi, guys,</p>
<p>I met a tensor shape issue when doing indexing.<br>
Suppose I have a tensor: <code>a = torch.randn(2, 3, 4)</code><br>
<code>a[[0,]]</code>'s shape is (1, 3, 4)<br>
<code>a[:, [0,]]</code>'s shape is (2, 1, 4)<br>
then why <code>a[[0,], [0,]]</code>'s shape is (1, 4) instead of (1, 1, 4)?</p>

username: Brock_Brown
cooked: <p><sub>Edit: misread, ignore this.</sub><br>
<code>a[0, 0]</code> is just short for <code>a[0][0]</code>. The “<code>:</code>” you’ve got there is a slice operator, which will give you the every index instead of just one index. It’s not entirely unlike string indexing, imagine instead of just getting one letter you get a substring.</p>

username: KFrank
cooked: <p>Hi Dio!</p>
<aside class="quote no-group" data-username="dio_din" data-post="1" data-topic="206015" data-full="true">
<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/dio_din/48/21505_2.png" class="avatar"> dio_din:</div>
<blockquote>
<p>then why <code>a[[0,], [0,]]</code>'s shape is (1, 4) instead of (1, 1, 4)?</p>
</blockquote>
</aside>
<p>Two pieces of behavior combine together to produce the results you<br>
are seeing.</p>
<p>First, the trailing dimensions of <code>a</code> that you are not indexing are simply<br>
carried along unchanged.  Another way of saying this is that the trailing<br>
dimensions are implicitly sliced with full slices.</p>
<p>The dimensions that you index with advanced indexing get replaced<br>
with the shape of the index tensors (or in your example, index lists).<br>
The index tensors used for advanced indexing must have matching<br>
(or broadcastable) shapes.</p>
<p>So for <code>a[[0], [0]]</code> you keep the implicitly-full-sliced third dimension<br>
of size <code>4</code>, and because your two advanced-indexing lists have matching<br>
shape <code>[1]</code>, the first two dimensions are replaced with that shape of <code>[1]</code>.<br>
Hence<code> a[[0], [0]].shape</code> becomes <code>[1, 4]</code>.</p>
<p>Consider:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'2.3.1'
&gt;&gt;&gt; a = torch.ones (2, 3, 4)
&gt;&gt;&gt; # a[&lt;index&gt;, &lt;implicit slice&gt;, &lt;implicit slice&gt;]
&gt;&gt;&gt; a[[0]].shape
torch.Size([1, 3, 4])
&gt;&gt;&gt; a[[0], :, :].shape
torch.Size([1, 3, 4])
&gt;&gt;&gt; # a[&lt;explicit slice&gt;, &lt;index&gt;, &lt;implicit slice&gt;]
&gt;&gt;&gt; a[:, [0]].shape
torch.Size([2, 1, 4])
&gt;&gt;&gt; a[:, [0], :].shape
torch.Size([2, 1, 4])
&gt;&gt;&gt; # a[&lt;index&gt;, &lt;index&gt;, &lt;implicit slice&gt;]
&gt;&gt;&gt; a[[0], [0]].shape
torch.Size([1, 4])
&gt;&gt;&gt; a[[0], [0], :].shape
torch.Size([1, 4])
&gt;&gt;&gt; # shape of advanced indices determines shape of result
&gt;&gt;&gt; a[[0, 1, 0], [0, 1, 2]].shape
torch.Size([3, 4])
&gt;&gt;&gt; a[[[0, 1, 0], [1, 0, 1], [0, 0, 1], [0, 1, 1]], [[0, 1, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]].shape
torch.Size([4, 3, 4])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

username: dio_din
cooked: <p>I’m using <code>a[[0], [0]]</code> rather than <code>a[0, 0]</code>.</p>

username: dio_din
cooked: <p>Thanks, that solved my question.</p>

username: Narayanan_R
cooked: <p>but using a[0, 0] is still gonna give you a shape of (2, 3, 4) where second element of the first dimension will be the copy of the first element , if you want to get a shape of (1, 1, 4) then use a[:1, [n]]</p>