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

I am trying to use a Subset random Sampler in my data-loading flow. But I am getting
"TypeError: list indices must be integers or slices, not tuple".
Without using Subset Random Sampler my code is working fine.
I am using a custom dataset class and below is the __getitem__ function, it returns a tuple, but it is a standard way taken from pytorch tutorials.
Where I am doing wrong as I am not violating the default return of the function?
How to fix it?

def __getitem__(self, index):
    """Generate one sample of data."""
    ID = self.list_IDs[index]
    image =, ID))
    y = self.labels[ID]
    if self.train_transform:
        image = self.train_transforms(image)
    elif self.valid_transform:
        image = self.valid_transforms(image)
        image = self.test_transforms(image)
    img = np.array(image)
    return img, y

My data loading pipeline -

training_set = Dataset(partition['train_set'], labels, root_dir='path', train_transform=True) # noqa
train_sampler = SubsetRandomSampler(training_set)
train_loader =, shuffle=False, sampler=train_sampler,pin_memory=True, num_workers=0, batch_size=batch_size,) # noqa

It looks like in the documentation for SubsetRandomSampler the constructor expects indices that can be sampled from rather than the actual dataset itself:

1 Like

So I do the modification like below, it should work but it is not working still

train_indices= training_set[:500]
train_sampler = SubsetRandomSampler(train_indices)

Right, that is is still giving you data elements rather than indices.
It appears that the expected use is something like
train_sampler = SubsetRandomSampler([i for i in range(500)])