Initially, a data loader is created with certain samples. While training I need to replace a sample which is in dataloader. How to replace it in to dataloader.

train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)

for sample,label in train_dataloader:
        prediction of model
        select misclassified samples and change them in train_dataloader but      how to change sample in train_dataloader

While training, the misclassified samples need to be modified. So How to replace a sample within train_dataloader?

  • 1
    $\begingroup$ Can you post a minimal example which illustrates the same? $\endgroup$
    – Aditya
    May 26, 2021 at 19:44
  • $\begingroup$ @Aditya I have posted some minimal code can you please recheck $\endgroup$ May 27, 2021 at 2:55
  • $\begingroup$ Before commenting something, I would like to know how do you decide which sample is to be changed? If it's to be done for every batch, then I would suggest you to look at collate_fn in PyTorch DataLoader, you can modify the tensors directly there before they reach your model. $\endgroup$
    – Aditya
    May 27, 2021 at 3:29
  • $\begingroup$ i would like to have minimal changes in samples that are misclassified, i.e. input dataloader is passed to model and then misclassified samples index is considered to change them. But the problem is how can I change within dataloader? $\endgroup$ May 27, 2021 at 4:03

1 Answer 1


Process per sample

Usually, you would process data at the sample level by creating your own class inheriting torch.utils.data.Dataset and do the processing inside __getitem__ method.

from torch.utils.data import Dataset
class MyDataset(Dataset):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

  def __getitem__(self, i):
    # Call the __getitem__ from Dataset class
    sample = super().__getitem__(i)
    # ...Do some processing on the sample here
    return sample

train_data = MyDataset()
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)

Process per batch

You can use the collate_fn parameter of torch.utils.data.DataLoader to specify a custom function which processes your batch.

from torch.utils.data import Dataset, DataLoader

def collate_fn(samples):
  # samples is a list of samples you get from the __getitem__ function of your torch.utils.data.Dataset instance
  # You can write here whatever processing you need before stacking samples into one batch of data
  batch = torch.stack(samples, dim=0)
  return batch

train_data = Dataset()
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size, collate_fn=collate_fn)
  • $\begingroup$ Nice workaround to use the sampler class! $\endgroup$
    – Aditya
    May 27, 2021 at 4:09
  • $\begingroup$ How to change samples in already defined train_dataloader $\endgroup$ May 27, 2021 at 4:11
  • 1
    $\begingroup$ @SSVarshini For this, you can use collate_fn, which defines how to stack your data samples into one batch. I updated my answer with this solution. $\endgroup$
    – Adam Oudad
    May 29, 2021 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.