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:
IndexError: too many indices for tensor of dimension 0
PS i keep getting the same error when i execute this part of my code too:
epochs = 12
for i in range(epochs):
for inputs, labels in training_loader:
inputs = inputs.view(inputs.shape[0], -1)
outputs = model(inputs)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
The error gets point out at this line – for inputs, labels in training_loader:
while in the first part of code I posted, the error gets point out at this line — images, labels = dataiter.next()
In this case the error might be in the Dataset itself, in particular in __getitem__.
Could you post the code of your Dataset or the __getitem__ method?
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