Infinite Random Sampler

Does PyTorch have a sampler that continually gives me a batch of random samples, without reaching a StopIteration error when it’s done? I want the ability to sample infinitely. If not, how could I implement one?

Thanks!

Hi,

I am not sure this would work. You would need to subclass Sampler and give an instance of your custom sampler to the dataloader you’re creating.
The problem is that the length of the sampler cannot be infinite as python does not have infinite integer. So you may have to use a very large number there like int(1e100).
Then the iterator is simply an iterator that return a random valid index every time. You can do that with a function that yeild such a number inside an infinite loop for example.

I’ll give it a try :slight_smile:

I do this by setting the length of dataset to 1e100.

...
def __len__(self):
        return 1e100
...

You can do this with the RandomSampler by setting replacement=True and num_samples to a very high number.

sampler = RandomSampler(dataset, replacement=True, num_samples=1e10)