title: Pytorch iter() runs endlessly or throws RecursionError
body: <p>I am having an issue with Pytorch DataLoader. Whenever I try to load the next batch via the iter()-function the function runs indefinitely. I have also tried running the function in Google Colab and it returns a RecursionError. Here is the iteration function:</p>
  <pre><code>def create_data_loader(df, tokenizer, max_len, batch_size):
      ds = PostTitleDataset(
          title = df[&quot;clean_title&quot;].to_numpy(),
          label = df[&quot;6_way_label&quot;].to_numpy(),
          tokenizer = tokenizer,
          max_len = max_len
      )
      
      return DataLoader(
          ds,
          batch_size = batch_size,
          num_workers = 0
      )
  
  BATCH_SIZE = 16
  MAX_LEN = 80
  
  test_data_loader = create_data_loader(df_test, tokenizer, MAX_LEN, BATCH_SIZE)
  
  **# This function runs indefinitely or gives me a RecursionError in Google Colab**
  if __name__ == '__main__':
    data = next(iter(test_data_loader))
    data.keys()
  </code></pre>
  <p>Here is the error message from Google Colab. In Jupyter Notebook it just runs indefinitely without an error message:</p>
  <blockquote>
  <p>RecursionErrorTraceback (most recent call
  last)  in ()
  ----&gt; 1 data = next(iter(test_data_loader))
  2 data.keys()</p>
  <p>5 frames ... last 1 frames repeated, from the frame below ...</p>
  <p> in <strong>getitem</strong>(self, item)
  24         #batch = convert_to_batch(dataframe)
  25
  ---&gt; 26         title = self[&quot;clean_title&quot;][item]
  27         label = self[&quot;6_way_label&quot;][item]
  28</p>
  <p>RecursionError: maximum recursion depth exceeded</p>
  </blockquote>
  <p><strong>Does anyone know how to solve this issue and how to have the iter()-function run through successfully returning a batch?</strong></p>
  <p>Just to provide full information, down below you will find my custom dataset class:</p>
  <pre><code>class PostTitleDataset(Dataset):
      
      def __init__(self, title, label, tokenizer, max_len):
          self.title = title,
          self.label = label,
          self. tokenizer = tokenizer,
          self.max_len = max_len
          
      def __len__(self):
          return len(self.title)
      
      def __getitem__(self, item):
          
          #batch = convert_to_batch(dataframe)
          
          title = self[&quot;clean_title&quot;][item]
          label = self[&quot;6_way_label&quot;][item]
          
          # Encode text input content
          encoding = self.tokenizer(
              title,
              padding=True,
              truncation=True,
              add_special_tokens=True,
              return_token_type_ids=False,
              return_attention_mask=True,
              return_tensors=&quot;pt&quot;,
          )
          
          # Return first post title of batch as validation
          return {
              &quot;clean title&quot;: title,
              &quot;input_ids&quot;: encoding[&quot;input_ids&quot;].flatten(),
              &quot;attention_mask&quot;: encoding[&quot;attention_mask&quot;].flatten(),
              &quot;Label&quot;: torch.tensor(label, dtype=torch.long)
          }
  </code></pre>