Help with implementing unique data loading


I have a Dataset whose __getitem__ gives me a question and its correct answer (no problem here). Here’s my question: At every step, my model takes in 3 tensors:

  1. the question
  2. the correct answer
  3. a random wrong answer from other questions

Obviously I would have no problem getting item 1 and 2, but how can I cleanly get item 3 without creating another dataset? I guess using data loader would be out of the question here (correct me if I’m wrong). Any suggestions?

Thanks for your help!!

You could just sample another index which is different to the one you are getting in __getitem__(self, index) and return this negative sample.
Something like this should work:

class MyDataset(Dataset):
    def __init__(self): = torch.randn(100, 1)
    def __getitem__(self, index):
        x =[index]
        index_neg = torch.randint(0, self.__len__(), (1,))
        while index_neg==index:
            index_neg = torch.randint(0, self.__len__(), (1,))
        x_neg =[index_neg]
        print('Using {} and {}'.format(index, index_neg))
        return x, x_neg
    def __len__(self):
        return len(

dataset = MyDataset()

Oh right. That was easier than I thought it would be. I should have seen that :joy:. Thanks for your help!!!