Change dataset member variable once per epoch

I would like to change a member variable of the dataset class every time the dataloader iterates trough my data again (Once per epoch). Since the Sampler gets called once per epoch when calling iter() I though of the following:

class MySampler(object): 
    def __init__(self, dataset):
        self.dataset = dataset
        self.sampler = RandomSampler(self.dataset)
        self.first_epoch = True
        self.rng = default_rng()

    def __iter__(self):
        if not self.first_epoch:
            self.dataset.other_data_indices = np.concatenate([self.rng.choice(len(100), size=len(100), replace=False))
        self.first_epoch = False
        return self.sampler

However, it doesn’t work because probably the dataset passed in the creation of the Sampler class is not shared with the dataset that my dataloader is using. Does anyone know how could I change self.dataset.other_data_indices from my Dataset or from any class/function passed to the dataloader once per epoch? (I can’t change the Dataloader itself because is a custom version of a private repo)

I don’t know how your private DataLoader works, but this post might be helpful.

Thank you, I think that would work. Is there a way to do this for all the workers if they are persistent?

I’m not aware of a clean way to change the copies in each worker process.

@ptrblck Sorry to text you here. Could you please answer to the below problem?
AttributeError: module ‘torchvision.transforms’ has no attribute ‘Scale’

How I understand is that you want 2 different sets of data depending on the epoch.
The RandomSampler is initialised on the entire dataset so regardless of the flag or the attribute you will have all training samples in a training epoch.

One way to have custom choice of samples is to implement your own BatchSampler where in the iter call you can choose the indices you wish to use depending on the current epoch.