user288609
user288609

Reputation: 13015

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

Upvotes: 5

Views: 210

Answers (2)

Sheshank S.
Sheshank S.

Reputation: 3278

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

In Python 3 it's required to use next() rather than .next().

Suggested by Dillon Davis: You can also use .__next__(), although .next() is better.

Upvotes: 1

Dillon Davis
Dillon Davis

Reputation: 7740

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.

Upvotes: 1

Related Questions