Thank you, your solution works for me.
I also found another method here:

  
      stackoverflow.com
  
  
      
    
  

  Pytorch DataLoader - Choose Class STL10 Dataset 71



  pytorch, torch, torchvision



  
  answered by
  
    iacolippo
  
  on 08:52AM - 14 Jul 18 UTC


  
  
    
    
  
  


I also included an example implementation here:
def get_indices(dataset,class_name):
    indices =  []
    for i in range(len(dataset.targets)):
        if dataset.targets[i] == class_name:
            indices.append(i)
    return indices


dataset = torchvision.datasets.CIFAR10(root='./data',
                           transform=torchvision.transforms.ToTensor())

idx = get_indices(dataset, 1)
loader = Data.DataLoader(dataset,batch_size=64, sampler = Data.sampler.SubsetRandomSampler(idx))

for idx, (data, target) in enumerate(loader):
    print(target)

I hope this works for all the datasets available in torchvision.datasets.