title: Pytorch - TypeError: 'torch.Size' object cannot be interpreted as an integer
body: <p>Hi I am training a PyTorch model and occurred this error:</p>
  
  <p><code>----&gt; 5     for i, data in enumerate(trainloader, 0):</code></p>
  
  <p><code>TypeError: 'torch.Size' object cannot be interpreted as an integer</code></p>
  
  <p>Not sure what this error means.</p>
  
  <p>You can find my code here :</p>
  
  <pre><code>model.train()
  for epoch in range(10):
      running_loss = 0
  
      for i, data in enumerate(trainloader, 0):
  
          inputs, labels = data
  
          optimizer.zero_grad()
  
          outputs = model(inputs)
          loss = criterion(outputs, labels)
          loss.backward()
          optimizer.step()
  
          if i % 2000 == 0:
            print (loss.item())
          running_loss += loss.item()
          if i % 1000 == 0:
              print ('[%d, %5d] loss: %.3f' % (epoch, i, running_loss/ 1000))
              running_loss = 0
  
  torch.save(model, 'FeatureNet.pkl')
  </code></pre>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>This is the codeblock for DataLoader. I am using a customized dataloader and datasets, which <code>x</code> are pictures with size (1025, 16) and <code>y</code> are one-hot encoded vectors for classification.</p>
  
  <p><code>x_train.shape</code> = (1100, 1025, 16)</p>
  
  <p><code>y_train.shape</code> = (1100, 10)</p>
  
  <pre><code>clean_dir = '/home/tk/Documents/clean/' 
  mix_dir = '/home/tk/Documents/mix/' 
  clean_label_dir = '/home/tk/Documents/clean_labels/' 
  mix_label_dir = '/home/tk/Documents/mix_labels/' 
  
  class MSourceDataSet(Dataset):
  
      def __init__(self, clean_dir, mix_dir, clean_label_dir, mix_label_dir):
  
          with open(clean_dir + 'clean0.json') as f:
              clean0 = torch.Tensor(json.load(f))
  
          with open(mix_dir + 'mix0.json') as f:
              mix0 = torch.Tensor(json.load(f))
  
          with open(clean_label_dir + 'clean_label0.json') as f:
              clean_label0 = torch.Tensor(json.load(f))
  
  
          with open(mix_label_dir + 'mix_label0.json') as f:
              mix_label0 = torch.Tensor(json.load(f))
  
  
          self.spec = torch.cat([clean0, mix0], 0)
          self.label = torch.cat([clean_label0, mix_label0], 0)
  
      def __len__(self):
          return self.spec.shape
  
  
      def __getitem__(self, index): 
  
          spec = self.spec[index]
          label = self.label[index]
          return spec, label
  </code></pre>
  
  <h2><strong>getitem</strong></h2>
  
  <pre><code>a, b = trainset.__getitem__(1000)
  print (a.shape)
  print (b.shape)
  </code></pre>
  
  <p>a.shape = <code>torch.Size([1025, 16])</code>;
  b.shape = <code>torch.Size([10])</code></p>
  
  <h2>Error message</h2>
  
  <pre><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-9-3bd71e5c00e1&gt; in &lt;module&gt;()
        3     running_loss = 0
        4 
  ----&gt; 5     for i, data in enumerate(trainloader, 0):
        6 
        7         inputs, labels = data
  
  ~/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
      311     def __next__(self):
      312         if self.num_workers == 0:  # same-process loading
  --&gt; 313             indices = next(self.sample_iter)  # may raise StopIteration
      314             batch = self.collate_fn([self.dataset[i] for i in indices])
      315             if self.pin_memory:
  
  ~/anaconda3/lib/python3.7/site-packages/torch/utils/data/sampler.py in __iter__(self)
      136     def __iter__(self):
      137         batch = []
  --&gt; 138         for idx in self.sampler:
      139             batch.append(idx)
      140             if len(batch) == self.batch_size:
  
  ~/anaconda3/lib/python3.7/site-packages/torch/utils/data/sampler.py in __iter__(self)
       32 
       33     def __iter__(self):
  ---&gt; 34         return iter(range(len(self.data_source)))
       35 
       36     def __len__(self):
  
  TypeError: 'torch.Size' object cannot be interpreted as an integer
  </code></pre>