Modify dataloader at a given epoch

I need to switch on class-balanced sampling from a certain epoch. What is the most efficient way to achieve this?

I have tried creating a callback that sets the trainer.train_dataloader attribute inside the on_train_epoch_start hook. It checks the trainer.current_epoch and, if needed, tries to set the train_dataloader attribute to the appropriate Dataloader. However, during actual run, this step fails with an AttributeError saying that the attribute cannot be set.

The best alternative that I can think of right now is to redefine the train_dataloader in my DataModule. That is, choose a sampler based on self.trainer.current_epoch. And accompany this by setting the reload_dataloaders_every_n_epochs to some appropriate number.

Is there a better way to do this? I just need random sampling till, say epoch n, and class-balanced sampling thereafter.

I am gonna jump into this question because I want to know something kind of similar:
Is there a way to activate/deactivate a sampler between epochs? I would like to alternate if it is enabled or not.

Hi there

The best alternative that I can think of right now is to redefine the train_dataloader in my DataModule. That is, choose a sampler based on self.trainer.current_epoch . And accompany this by setting the reload_dataloaders_every_n_epochs to some appropriate number.

This is the way to do it, this is what the reload_dataloaders_every_n_epochs is meant for. And the same can be done in the LightningModule as well. Does it work for you?

Thank you for your confirmation. The technique does indeed solve the issue that I was trying to address.

1 Like