title: Iterating over subsets from torch.utils.data.random_split
body: <p>I am currently loading a folder with AI training data in it. The subfolders represent the label names with the corresponding images inside. This works well by using pyTorch's ImageFolder loader.</p>
  
  <pre><code>def load_dataset():
      data_path = 'C:/example_folder/'
  
      train_dataset_manual = torchvision.datasets.ImageFolder(
          root=data_path,
          transform=torchvision.transforms.ToTensor()
      )
  
      train_loader_manual = torch.utils.data.DataLoader(
          train_dataset_manual,
          batch_size=1,
          num_workers=0,
          shuffle=True
      )
  
      return train_loader_manual
  
  full_dataset = load_dataset()
  </code></pre>
  
  <p>Now I want to have this dataset split into a training and a test data set. I am using the random_split function for this:</p>
  
  <pre><code>training_data_size = 0.8
  
  train_size = int(training_data_size * len(full_dataset))
  test_size = len(full_dataset) - train_size
  train_dataset, test_dataset = torch.utils.data.random_split(full_dataset, [train_size, test_size])
  </code></pre>
  
  <p>The full_dataset is an object of type <code>torch.utils.data.dataloader.DataLoader</code>. I can iterate through it with a loop like this:</p>
  
  <pre><code>for batch_idx, (data, target) in enumerate(full_dataset):
      print(batch_idx)
  </code></pre>
  
  <p>The <code>train_dataset</code> is an object of type <code>torch.utils.data.dataset.Subset</code>. If I try to loop through it, I get:</p>
  
  <blockquote>
    <p>TypeError 'DataLoader' object is not subscriptable:</p>
  </blockquote>
  
  <pre><code>for batch_idx, (data, target) in enumerate(train_dataset):
      print(batch_idx)
  </code></pre>
  
  <p>How can I loop through it? I am relatively new to Python.</p>
  
  <p>Thanks!</p>