Last active
October 16, 2023 13:36
-
-
Save MFreidank/821cc87b012c53fade03b0c7aba13958 to your computer and use it in GitHub Desktop.
A pytorch DataLoader that generates an unbounded/infinite number of minibatches from the dataset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from torch.utils.data import DataLoader | |
class InfiniteDataLoader(DataLoader): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
# Initialize an iterator over the dataset. | |
self.dataset_iterator = super().__iter__() | |
def __iter__(self): | |
return self | |
def __next__(self): | |
try: | |
batch = next(self.dataset_iterator) | |
except StopIteration: | |
# Dataset exhausted, use a new fresh iterator. | |
self.dataset_iterator = super().__iter__() | |
batch = next(self.dataset_iterator) | |
return batch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a similar code, but once the generator is exhausted it keeps on creating new generators.
Code in the training loop:
Functions of dyn_DataLoader:
Any ideas of what could be wrong?