Hi !
I’m trying to train the network using a RandomSampler as suggested. However, if I understood correctly, it only yields a single index at a time, so I have to wrap it with a BatchSampler (?)
I tried something like this :
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import BatchSampler, RandomSampler

def prep_data(bs, epochs):
    train_transform = transforms.Compose([transforms.RandomHorizontalFlip(),
                                          transforms.ToTensor(),
                                          transforms.Normalize((.5, .5, .5), (.5, .5, .5))])

    trainset = torchvision.datasets.CIFAR10(root = "../data_CIFAR10", train = True, download = True, transform = train_transform)
    trainSampler = BatchSampler(RandomSampler(trainset, replacement = True, num_samples = epochs * len(trainset)), batch_size = bs, drop_last = True)
    trainloader = torch.utils.data.DataLoader(trainset, sampler = trainSampler)

    return trainloader

When I test it, I get a TypeError (the following code is enough to reproduce the error).
trainloader = prep_data(32, 1)
for i, data in enumerate(trainloader):
    print(i)

TypeError                                 Traceback (most recent call last)
<ipython-input-1-5c625c535dea> in <module>()
     22 
     23 trainloader = prep_data(32, 1)
---> 24 for i, data in enumerate(trainloader):
     25     print(i)

/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    344     def __next__(self):
    345         index = self._next_index()  # may raise StopIteration
--> 346         data = self.dataset_fetcher.fetch(index)  # may raise StopIteration
    347         if self.pin_memory:
    348             data = _utils.pin_memory.pin_memory(data)

/anaconda3/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/anaconda3/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/anaconda3/lib/python3.6/site-packages/torchvision/datasets/cifar.py in __getitem__(self, index)
    116         #    tuple: (image, target) where target is index of the target class.
    117         
--> 118         img, target = self.data[index], self.targets[index]
    119 
    120         # doing this so that it is consistent with all other datasets

TypeError: list indices must be integers or slices, not list

I tried to get a little bit further, and it seems that self._next_index() returns an array of shape (1, 32) instead of (32) (so the list comprehension doesn’t separate the indexes as expected by the dataset). I got it to run by replacing it with self._next_index()[0]…
I had never used samplers before, so I’m not really sure of what I’m doing here. Am I doing well in my prep_data function? And my solution to avoid the error is quite of a messy workaround, so is there a better way to handle it?
Thanks,
William