PyTorch DataLoader - "IndexError: too many indices for tensor of dimension 0"
I am trying to implement a CNN to identify digits in the MNIST dataset and my code comes up with the error during the data loading process. I don't understand why this is happening.

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5), (0.5))
])

trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=20, shuffle=True, num_workers=2)

testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=20, shuffle=False, num_workers=2)

for i, data in enumerate(trainloader, 0):
    inputs, labels = data[0], data[1]


Error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-6-b37c638b6114&gt; in &lt;module&gt;
      2 
----&gt; 3     for i, data in enumerate(trainloader, 0):
      4         inputs, labels = data[0], data[1]

# ...

IndexError: Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 99, in &lt;listcomp&gt;
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/opt/conda/lib/python3.6/site-packages/torchvision/datasets/mnist.py", line 95, in __getitem__
    img = self.transform(img)
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 61, in __call__
    img = t(img)
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 164, in __call__
    return F.normalize(tensor, self.mean, self.std, self.inplace)
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 208, in normalize
    tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
IndexError: too many indices for tensor of dimension 0


The problem is that the mean and std have to be sequences (e.g., tuples), therefore you should add a comma after the values:

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])


Note the difference between (0.5) and (0.5,). You can check here how these values are used. If you apply the same process you'll see that:

import torch

x1 = torch.as_tensor((0.5))
x2 = torch.as_tensor((0.5,))

print(x1.shape, x1.ndim)  # output: torch.Size([]) 0
print(x2.shape, x2.ndim)  # output: torch.Size([1]) 1


Maybe you don't know, but they are also different in Python:

type((0.5))   # &lt;type 'float'&gt;
type((0.5,))  # &lt;type 'tuple'&gt;

