title: Taking subsets of a pytorch dataset
body: <p>I have a network which I want to train on some dataset (as an example, say <code>CIFAR10</code>). I can create data loader object via</p>
  
  <pre><code>trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                          download=True, transform=transform)
  trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                            shuffle=True, num_workers=2)
  </code></pre>
  
  <p>My question is as follows: Suppose I want to make several different training iterations. Let's say I want at first to train the network on all images in odd positions, then on all images in even positions and so on. In order to do that, I need to be able to access to those images. Unfortunately, it seems that <code>trainset</code> does not allow such access. That is, trying to do <code>trainset[:1000]</code> or more generally <code>trainset[mask]</code> will throw an error.</p>
  
  <p>I could do instead </p>
  
  <pre><code>trainset.train_data=trainset.train_data[mask]
  trainset.train_labels=trainset.train_labels[mask]
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                                shuffle=True, num_workers=2)
  </code></pre>
  
  <p>However, that will force me to create a new copy of the full dataset in each iteration (as I already changed <code>trainset.train_data</code> so I will need to redefine <code>trainset</code>). Is there some way to avoid it?</p>
  
  <p>Ideally, I would like to have something "equivalent" to</p>
  
  <pre><code>trainloader = torch.utils.data.DataLoader(trainset[mask], batch_size=4,
                                                shuffle=True, num_workers=2)
  </code></pre>