title: PyTorch DataLoader - "IndexError: too many indices for tensor of dimension 0"
body: <p>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.</p>
  
  <pre class="lang-py prettyprint-override"><code>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]
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>---------------------------------------------------------------------------
  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
  </code></pre>