生成器“ TypeError:'generator'对象不是迭代器”

生成器“ TypeError:'generator'对象不是迭代器”,第1张

生成器“ TypeError:'generator'对象不是迭代器”

我遇到了同样的问题,我设法通过定义一个

__next__
方法解决了这个问题:

class My_Generator(Sequence):    def __init__(self, image_filenames, labels, batch_size):        self.image_filenames, self.labels = image_filenames, labels        self.batch_size = batch_size        self.n = 0        self.max = self.__len__()    def __len__(self):        return np.ceil(len(self.image_filenames) / float(self.batch_size))    def __getitem__(self, idx):        batch_x = self.image_filenames[idx * self.batch_size:(idx + 1) * self.batch_size]        batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]        return np.array([        resize(imread(file_name), (200, 200))for file_name in batch_x]), np.array(batch_y)    def __next__(self):        if self.n >= self.max:self.n = 0        result = self.__getitem__(self.n)        self.n += 1        return result

请注意,我在

__init__
函数中声明了两个新变量



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5667426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存