Here's a link to [the source code for the keras Sequence class][1].
I have created what I believe to be the exact same object by doing the following:
- copying and pasting the same
__init__,__getitem__, and__len__methods from theInfiniteGeneratorclass to theKGenclass. - copying the iter method from the source code of the
Sequenceclass the theInfiniteGeneratorclass.
My hypothesis is that these two generators should both create a finite list, but the Sequence object will continue iterating forever. Why?
class InfiniteGenerator(object):
def __init__(self, alist):
self.alist = alist
def __getitem__(self, idx):
return self.alist[idx]
def __len__(self):
return len(self.alist)
def __iter__(self):
for item in (self[i] for i in range(len(self))):
yield item
from keras.utils import Sequence
class KGen(Sequence):
def __init__(self, alist):
self.alist = alist
def __getitem__(self, idx):
return self.alist[idx]
def __len__(self):
return len(self.alist)
if __name__ == '__main__':
ig = InfiniteGenerator(list(range(4)))
for item in ig:
print(item)
print('now trying second iterator')
import time
time.sleep(1)
kg = KGen(list(range(4)))
for item in kg:
print(item)