Shape of tensor changes after slicing. RuntimeError: stack expects each tensor to be equal size, but got [32, 1] at entry 0 and [32, 0] at entry 1

Umair_Javaid: <p>I have a very large tensor of shape <code>(512,3,224,224)</code>. I input it to model in batches of 32 and I then save the scores corresponding to the target label which is <code>2</code>. in each iteration, after every slice, the shape of <code>scores</code> changes. Which leads to the following error. What am I doing wrong and how to fix it.<br>
<code>label = torch.ones(1)*2</code></p>
<pre><code class="lang-auto">def sub_forward(self, x):
    x = self.vgg16(x)
    x = self.bn1(x)
    x = self.linear1(x)
    x = self.linear2(x)
    return x
</code></pre>
<pre><code class="lang-auto">
def get_scores(self, imgs, targets):
    b, _, _, _ = imgs.shape
    batch_size = 32
    total_scores = []
    for i in range(0, b, batch_size):
      scores = self.sub_forward(imgs[i:i+batch_size,:,:,:])
      scores = F.softmax(scores)
      labels = targets[i:i+batch_size]
      labels = labels.long()
      scores = scores[:,labels]
      print(i," scores: ", scores)
      total_scores.append(scores)
      print(i," total_socres: ", total_scores)
    total_scores = torch.stack(total_scores)
    return scores
</code></pre>
<pre><code class="lang-auto">0  scores:  tensor([[0.0811],
        [0.0918],
        [0.0716],
        [0.1680],
        [0.1689],
        [0.1319],
        [0.1556],
        [0.2966],
        [0.0913],
        [0.1238],
        [0.1480],
        [0.1215],
        [0.2524],
        [0.1283],
        [0.1603],
        [0.1282],
        [0.2668],
        [0.1146],
        [0.2043],
        [0.2475],
        [0.0865],
        [0.1869],
        [0.0860],
        [0.1979],
        [0.1677],
        [0.1983],
        [0.2623],
        [0.1975],
        [0.1894],
        [0.3299],
        [0.1970],
        [0.1094]], device='cuda:0')
0  total_socres:  [tensor([[0.0811],
        [0.0918],
        [0.0716],
        [0.1680],
        [0.1689],
        [0.1319],
        [0.1556],
        [0.2966],
        [0.0913],
        [0.1238],
        [0.1480],
        [0.1215],
        [0.2524],
        [0.1283],
        [0.1603],
        [0.1282],
        [0.2668],
        [0.1146],
        [0.2043],
        [0.2475],
        [0.0865],
        [0.1869],
        [0.0860],
        [0.1979],
        [0.1677],
        [0.1983],
        [0.2623],
        [0.1975],
        [0.1894],
        [0.3299],
        [0.1970],
        [0.1094]], device='cuda:0')]
32  scores:  tensor([], device='cuda:0', size=(32, 0))
32  total_socres:  [tensor([[0.0811],
        [0.0918],
        [0.0716],
        [0.1680],
        [0.1689],
        [0.1319],
        [0.1556],
        [0.2966],
        [0.0913],
        [0.1238],
        [0.1480],
        [0.1215],
        [0.2524],
        [0.1283],
        [0.1603],
        [0.1282],
        [0.2668],
        [0.1146],
        [0.2043],
        [0.2475],
        [0.0865],
        [0.1869],
        [0.0860],
        [0.1979],
        [0.1677],
        [0.1983],
        [0.2623],
        [0.1975],
        [0.1894],
        [0.3299],
        [0.1970],
        [0.1094]], device='cuda:0'), tensor([], device='cuda:0', size=(32, 0))]
</code></pre>
<p>`</p>
<blockquote>
<p>RuntimeError: stack expects each tensor to be equal size, but got [32, 1] at entry 0 and [32, 0] at entry 1</p>
</blockquote>
<p>`</p>

InnovArul: <aside class="quote no-group" data-username="Umair_Javaid" data-post="1" data-topic="79386">
<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/umair_javaid/48/18886_2.png" class="avatar"> Umair_Javaid:</div>
<blockquote>
<p>scores = scores[:,labels]</p>
</blockquote>
</aside>
<p>It seems that this line gives 0 results in 2nd iteration. What does <code>labels</code> contain?</p>

Umair_Javaid: <aside class="quote no-group" data-username="Umair_Javaid" data-post="1" data-topic="79386">
<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/umair_javaid/48/18886_2.png" class="avatar"> Umair_Javaid:</div>
<blockquote>
<p>targets</p>
</blockquote>
</aside>
<p>I forward <strong>label</strong> to get_scores function<br>
<code>label = torch.ones(1)*2</code><br>
<code>get_scores(self, imgs, label)</code></p>

InnovArul: <p>Something is not right.</p>
<aside class="quote no-group" data-username="Umair_Javaid" data-post="1" data-topic="79386">
<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/umair_javaid/48/18886_2.png" class="avatar"> Umair_Javaid:</div>
<blockquote>
<p>labels = targets[i:i+batch_size]</p>
</blockquote>
</aside>
<p>You won’t be able to index <code>targets</code> if you define <code>label</code> as <code>label = torch.ones(1)*2</code>.</p>

Umair_Javaid: <p>well that works fine for me. later I will be having a much larger tensor of targets</p>

InnovArul: <p>Interesting.</p>
<pre><code class="lang-auto">x = torch.tensor(1)*2 
x[0]
</code></pre>
<p>To me, this gives <code>invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number</code> in pytorch 1.3.1.</p>

Umair_Javaid: <p>Interesting indeed.<br>
For the following code I get no error</p>
<pre><code class="lang-auto">label = torch.ones(1)*2
print(label[32:64])
</code></pre>
<p>It gives the following result:<br>
`</p>
<blockquote>
<p>tensor(<span class="chcklst-box fa fa-square-o fa-fw"></span>)</p>
</blockquote>
<p>`<br>
I am using google colab atm</p>

InnovArul: <aside class="quote no-group" data-username="Umair_Javaid" data-post="7" data-topic="79386">
<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/umair_javaid/48/18886_2.png" class="avatar"> Umair_Javaid:</div>
<blockquote>
<p>tensor(<span class="chcklst-box fa fa-square-o fa-fw"></span>)</p>
</blockquote>
</aside>
<p>This explains why there are 0 results.</p>
<p>can you check the pytorch version?<br>
<code>print(torch.__version__)</code></p>

Umair_Javaid: <pre><code class="lang-auto">PyTorch Version:  1.5.0+cu101
Torchvision Version:  0.6.0+cu101
</code></pre>
<p>(What to do now?)</p>

Umair_Javaid: <p>Are there any alternatives to this?</p>

InnovArul: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> any suggestions?</p>

ptrblck: <p>Slicing “out of bounds” will return an empty tensor, which is expected.<br>
It seems you are forwarding <code>targets</code>, not <code>labels</code> to <code>get_scores</code> and calculate <code>labels</code> as:</p>
<pre><code class="lang-python">labels = targets[i:i+batch_size]
</code></pre>
<p>Could you check the shape of <code>targets</code> before the slicing operation and <code>labels</code> afterwards?</p>

Umair_Javaid: <pre><code class="lang-auto">def get_scores(self, imgs, targets):
    b, _, _, _ = imgs.shape
    batch_size = 32
    total_scores = []
    for i in range(0, b, batch_size):
      scores = self.sub_forward(imgs[i:i+batch_size,:,:,:])
      scores = F.softmax(scores)
      print("targets: ",targets)
      labels = targets[i:i+batch_size]
      print("labels: ",labels)
      labels = labels.long()
      scores = scores[:,labels]
      print(i," scores: ", scores)
      total_scores.append(scores)
      print(i," total_socres: ", total_scores)
    total_scores = torch.stack(total_scores)
    return scores
</code></pre>
<pre><code class="lang-auto">
targets:  tensor([2.])
labels:  tensor([2.])
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:38: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
0  scores:  tensor([[0.5671],
        [0.5106],
        [0.5338],
        [0.5548],
        [0.5764],
        [0.5593],
        [0.6007],
        [0.5891],
        [0.5425],
        [0.6006],
        [0.6386],
        [0.5034],
        [0.5746],
        [0.5831],
        [0.5794],
        [0.6206],
        [0.2900],
        [0.4843],
        [0.6075],
        [0.4473],
        [0.5612],
        [0.2937],
        [0.6049],
        [0.5867],
        [0.3828],
        [0.4014],
        [0.4806],
        [0.5595],
        [0.5749],
        [0.5216],
        [0.3748],
        [0.5434]], device='cuda:0')
0  total_socres:  [tensor([[0.5671],
        [0.5106],
        [0.5338],
        [0.5548],
        [0.5764],
        [0.5593],
        [0.6007],
        [0.5891],
        [0.5425],
        [0.6006],
        [0.6386],
        [0.5034],
        [0.5746],
        [0.5831],
        [0.5794],
        [0.6206],
        [0.2900],
        [0.4843],
        [0.6075],
        [0.4473],
        [0.5612],
        [0.2937],
        [0.6049],
        [0.5867],
        [0.3828],
        [0.4014],
        [0.4806],
        [0.5595],
        [0.5749],
        [0.5216],
        [0.3748],
        [0.5434]], device='cuda:0')]
targets:  tensor([2.])
labels:  tensor([])
32  scores:  tensor([], device='cuda:0', size=(32, 0))
32  total_socres:  [tensor([[0.5671],
        [0.5106],
        [0.5338],
        [0.5548],
        [0.5764],
        [0.5593],
        [0.6007],
        [0.5891],
        [0.5425],
        [0.6006],
        [0.6386],
        [0.5034],
        [0.5746],
        [0.5831],
        [0.5794],
        [0.6206],
        [0.2900],
        [0.4843],
        [0.6075],
        [0.4473],
        [0.5612],
        [0.2937],
        [0.6049],
        [0.5867],
        [0.3828],
        [0.4014],
        [0.4806],
        [0.5595],
        [0.5749],
        [0.5216],
        [0.3748],
        [0.5434]], device='cuda:0'), tensor([], device='cuda:0', size=(32, 0))]
targets:  tensor([2.])
labels:  tensor([])
64  scores:  tensor([], device='cuda:0', size=(32, 0))
64  total_socres:  [tensor([[0.5671],
        [0.5106],
        [0.5338],
        [0.5548],
        [0.5764],
        [0.5593],
        [0.6007],
        [0.5891],
        [0.5425],
        [0.6006],
        [0.6386],
        [0.5034],
        [0.5746],
        [0.5831],
        [0.5794],
        [0.6206],
        [0.2900],
        [0.4843],
        [0.6075],
        [0.4473],
        [0.5612],
        [0.2937],
        [0.6049],
        [0.5867],
        [0.3828],
        [0.4014],
        [0.4806],
        [0.5595],
        [0.5749],
        [0.5216],
        [0.3748],
        [0.5434]], device='cuda:0'), tensor([], device='cuda:0', size=(32, 0)), tensor([], device='cuda:0', size=(32, 0))]
targets:  tensor([2.])
labels:  tensor([])
96  scores:  tensor([], device='cuda:0', size=(32, 0))
96  total_socres:  [tensor([[0.5671],
        [0.5106],
        [0.5338],
        [0.5548],
        [0.5764],
        [0.5593],
        [0.6007],
        [0.5891],
        [0.5425],
        [0.6006],
        [0.6386],
        [0.5034],
        [0.5746],
        [0.5831],
        [0.5794],
        [0.6206],
        [0.2900],
        [0.4843],
        [0.6075],
        [0.4473],
        [0.5612],
        [0.2937],
        [0.6049],
        [0.5867],
        [0.3828],
        [0.4014],
        [0.4806],
        [0.5595],
        [0.5749],
        [0.5216],
        [0.3748],
        [0.5434]], device='cuda:0'), tensor([], device='cuda:0', size=(32, 0)), tensor([], device='cuda:0', size=(32, 0)), tensor([], device='cuda:0', size=(32, 0))]
</code></pre>

Umair_Javaid: <p>Should I use an if condition before slicing to see if I’m slicing out of bounds?</p>

ptrblck: <p>I’m not sure if a condition will save you, since you expect <code>targets</code> to have at least the length <code>b+batch_size</code>, while it seems to have a single element.<br>
I would recommend to take another look, how <code>targets</code> is defined and why it’s smaller than you expect.</p>

Umair_Javaid: <p>you’re right. In my code, I’m repeating input images but I didn’t do that for the labels. Thank you so much for your time. Can you look at the other question I posted after this</p>