5

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
Alex Taylor
  • 7,785
  • 4
  • 25
  • 37
user288609
  • 11,491
  • 24
  • 77
  • 114
  • 1
    Possible duplicate of [Is generator.next() visible in python 3.0?](https://stackoverflow.com/questions/1073396/is-generator-next-visible-in-python-3-0) – MegaIng Jul 04 '18 at 23:44

2 Answers2

1

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.

Dillon Davis
  • 5,496
  • 2
  • 14
  • 35
1

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.

Sheshank S.
  • 2,823
  • 3
  • 17
  • 36