The target label appearing on image grid is dfferent from the label being printed

aleemsidra: <p>I am trying to show the images along with respective labels being loaded in the data loader batch. Labels are: 0,1,2,3. However, the labels displayed on the top image are different from the labels that I simply print using the print command. Should not be both these labels same?</p>
<pre><code class="lang-auto">def imshow(inp, title=None):
    """Imshow for Tensor."""
    inp = inp.numpy().transpose((1, 2, 0))
    plt.imshow(inp)
    if title is not None:
         plt.title(title)

# Get a batch of training data
image, label = next(iter(train_loader))
print(label) # prinitng respective labels 
# Make a grid from batch
out = torchvision.utils.make_grid(image)
imshow(out, title=[label[x] for x in label])

</code></pre>
<p>Below is the screenshot of printed labels and labels being displayed as titles on respective images.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/e/1e6e3b2d291233b6faa7161e324c41a63192d0a4.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1e6e3b2d291233b6faa7161e324c41a63192d0a4" title="Screenshot 2021-06-14 at 3.12.15 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/e/1e6e3b2d291233b6faa7161e324c41a63192d0a4_2_690x110.jpeg" alt="Screenshot 2021-06-14 at 3.12.15 PM" data-base62-sha1="4lcuAaqils4d1NLOtthgxcwfxAw" width="690" height="110" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/e/1e6e3b2d291233b6faa7161e324c41a63192d0a4_2_690x110.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/e/1e6e3b2d291233b6faa7161e324c41a63192d0a4_2_1035x165.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/e/1e6e3b2d291233b6faa7161e324c41a63192d0a4_2_1380x220.jpeg 2x" data-dominant-color="E7E3E2"><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">Screenshot 2021-06-14 at 3.12.15 PM</span><span class="informations">2118×338 53.7 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>

ptrblck: <p>The different outputs are expected, since you are indexing <code>label</code> with itself:</p>
<pre><code class="lang-python">label = torch.tensor([1, 3, 0, 1])
print(label)
&gt; tensor([1, 3, 0, 1])
print([label[x] for x in label])
&gt; [tensor(3), tensor(1), tensor(1), tensor(3)]
</code></pre>

aleemsidra: <p>I did not get your point. Since the label of the 4 images respectively is tensor([1, 3, 0, 1]). So, how indexing through label is changing the output? lable[1]-&gt; 1, lable[2]-&gt;3, lable[3]-&gt;0, label[4]-&gt; 1. Should not be this output. Can you please explain?</p>

ptrblck: <p>The list comprehension uses:</p>
<pre><code class="lang-python">label = torch.tensor([1, 3, 0, 1])
# which equals to
label[0] = 1
label[1] = 3
label[2] = 0
label[3] = 1

# your list comprehension uses the label itself to index into it
[label[x] for x in label]
== [label[1], label[3], label[0], label[1]]
== [3, 1, 1, 3]
</code></pre>