这是 [keras Sequence 类的源代码][1] 的链接。我通过执行以下操作创建了我认为完全相同的对象:将相同的__init__、__getitem__和__len__方法从InfiniteGenerator类复制并粘贴到KGen类。复制ITER从源代码方法Sequence类的InfiniteGenerator类。我的假设是这两个生成器都应该创建一个有限列表,但Sequence对象将永远继续迭代。为什么?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 itemfrom keras.utils import Sequenceclass 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)
添加回答
举报
0/150
提交
取消