IndexError: too many indices for tensor of dimension 0

username: Anita
cooked: <p>Hi all,</p>
<p>I am new to python programming in deep learning. And I was executing some basic code of classifying the MNIST dataset, but I keep encountering this error:</p>
<p>IndexError: too many indices for tensor of dimension 0</p>
<p>Each time i execute this part of my code:</p>
<p>dataiter = iter(training_loader)<br>
images, labels = dataiter.next()<br>
fig = plt.figure(figsize = (25, 4))</p>
<p>for idx in np.arange(20):<br>
ax = fig.add_subplot(2, 10, idx+1)<br>
plt.imshow(im_convert(images[idx]))<br>
ax.set_title([labels[idx].item()])</p>
<p>I need HELP with this!</p>

username: ptrblck
cooked: <p>Could you print the shape of <code>images</code> before executing the for loop?<br>
Also, what does <code>im_convert</code> do?</p>

username: Anita
cooked: <p>No I am not able to print out the shape of images before the loop. The im_convert method is as follows:</p>
<p>def im_convert(tensor):<br>
image = tensor.clone().detach().numpy()<br>
image = image.transpose(1,2,0)<br>
image = image*np.array((0.5, 0.5, 0.5)) + np.array((0.5, 0.5, 0.5))<br>
image = image.clip(0, 1)<br>
return image</p>
<p>PS i keep getting the same error when i execute this part of my code too:</p>
<p>epochs = 12<br>
for i in range(epochs):<br>
for inputs, labels in training_loader:<br>
inputs = inputs.view(inputs.shape[0], -1)<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()</p>
<p>The error gets point out at this line – for inputs, labels in training_loader:</p>
<p>while in the first part of code I posted, the error gets point out at this line — images, labels = dataiter.next()</p>
<p>many many thanks for your reply <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: ptrblck
cooked: <p>In this case the error might be in the <code>Dataset</code> itself, in particular in <code>__getitem__</code>.<br>
Could you post the code of your <code>Dataset</code> or the <code>__getitem__</code> method?</p>

username: Anita
cooked: <pre><code>def __getitem__(self, index):
    """
    Args:
        index (int): Index

    Returns:
        tuple: (image, target) where target is index of the target class.
    """
    img, target = self.data[index], int(self.targets[index])

    # doing this so that it is consistent with all other datasets
    # to return a PIL Image
    img = Image.fromarray(img.numpy(), mode='L')

    if self.transform is not None:
        img = self.transform(img)

    if self.target_transform is not None:
        target = self.target_transform(target)

    return img, target
</code></pre>
<p>This is the <strong>getitem</strong> method in dataset</p>

username: ptrblck
cooked: <p>Thanks for the code!<br>
Could you now print <code>self.data</code> and <code>self.targets</code> before indexing them?<br>
Also, have you defined <code>self.target_transform</code> or is it <code>None</code>?</p>

username: Anita
cooked: <p>I printed self.data and self.targets before indexing them.</p>
<p>I have not defined self.target_transform is None</p>

username: ptrblck
cooked: <p>Could you post the output here and if possible the shape of both?</p>