Change sampler every epoch

I would like to filter out my dataset every epoch by changing the sampler for the data loader, but when I change the sampler it gives me :
ValueError: sampler attribute should not be set after DataLoader is initialized

here’s my sample code :

def train_test():  
        epoch = epoch_n
        for e in range(epoch):
                train_loss, train_acuracy, losses = train(train_loader,criterion,optimizer,e+1,True)
                test_loss, test_acuracy = test(test_loader,criterion,optimizer,e+1,True)
                print('Epoch %d, Training Loss/Acc: %f//%f, Testing Loss/Acc: %f//%f' % (e+1,train_loss,train_acuracy,test_loss,test_acuracy))
                indexsorted = rankloss(losses)
                indexcuted  = cutdataset(indexsorted)
                train_loader.sampler = sampler.SubsetRandomSampler(indexcuted)

is there a way to do this ?

1 Like

You could create a new dataloader with the new sampler every epoch. If you don’t want to do this, do you mind sharing what the differences in the samplers are? Perhaps you can create one sampler that can take the behavior of all the samplers you need

solved by creating custom sampler by writing a method to update the sampler’s properties. Thank you

Mind to share your code?

1 Like