[英]Getting next item from generator fails
There is a code segment. 有代码段。 running the program gets the following error
运行该程序会收到以下错误
epoch, step, d_train_feed_dict, g_train_feed_dict = inf_data_gen.next()
AttributeError: 'generator' object has no attribute 'next'
The corresponding code segment is listed as follows. 相应的代码段列出如下。 What can be the reason underlying it?
它背后的原因是什么?
inf_data_gen = self.inf_get_next_batch(config)
def inf_get_next_batch(self, config):
"""Loop through batches for infinite epoches.
"""
if config.dataset == 'mnist':
num_batches = min(len(self.data_X), config.train_size) // config.batch_size
else:
self.data = glob(os.path.join("./data", config.dataset, self.input_fname_pattern))
num_batches = min(len(self.data), config.train_size) // config.batch_size
epoch = 0
while True:
epoch += 1
for (step, d_train_feed_dict, g_train_feed_dict) in \
self.get_next_batch_one_epoch(num_batches, config):
yield epoch, step, d_train_feed_dict, g_train_feed_dict
You need to use: 你需要使用:
next(inf_data_gen)
Rather than: 而不是:
inf_data_gen.next()
Python 3 did away with .next()
, renaming it as .__next__()
, but its best that you use next(generator)
instead. Python 3取消了
.next()
,将其重命名为.__next__()
,但最好用next(generator)
代替。
Try this: 尝试这个:
epoch, step, d_train_feed_dict, g_train_feed_dict = next(inf_data_gen)
See this: there's no next() function in a yield generator in python 3 看到这个: python 3中的yield生成器中没有next()函数
In Python 3 it's required to use next()
rather than .next()
. 在Python 3中,需要使用
next()
而不是.next()
。
Suggested by Dillon Davis: You can also use .__next__()
, although .next()
is better. Dillon Davis建议:你也可以使用
.__next__()
,尽管.next()
更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.