title: How can one clone a Pytoch dataset into another variable?
body: <p>I want to create several subsets of the MNIST dataset provided in Pytorch. Each subset should have different classes. What I tried was the following:</p>
  
  <pre><code>def split_MNIST(mnist_set, digits):
      dset = mnist_set
      classes = []
      indices = dset.targets == digits[0]
      classes.append(dset.classes[digits[0]])
      if len(digits) &gt; 1:
          for digit in digits[1:]:
              idx = dset.targets == digit
              indices = indices + idx
              classes.append(dset.classes[digit])
      dset.targets = dset.targets[indices]
      dset.data = dset.data[indices]
      dset.classes = classes
      return dset
  
  
  train = datasets.MNIST("../data", train=True, download=True,
                          transform=transforms.Compose([transforms.ToTensor()]))
  
  test =datasets.MNIST("../data", train=False, download=True,
                        transform=transforms.Compose([transforms.ToTensor()]))
  
  tr = split_MNIST(train, [1,2,3])
  
  trainset = torch.utils.data.DataLoader(tr, batch_size=16, shuffle=True)
  
  </code></pre>
  
  <p>This works, but instead of creating a new dataset, it actually changes the original train variable. Is there a way to create a clone of the dataset instead to preserve the original one?</p>