The Keras Sequence class documentation says
Every Sequence must implement the __getitem__ and the __len__ methods. If you
want to modify your dataset between epochs you may implement on_epoch_end. The
method __getitem__ should return a complete batch.
However the source code for fit_generator
& other similar methods in training_generator.py calls next
method with the generator passed as the argument. From my understanding this means, the class which subclasses from Sequence
should be an iterator which requires __next__
method to be implemented.
In order to use a class subclassed from Sequence
with methods like fit_generator
, predict_generator
, etc. is it required to implement __next__
method?
No, there is no need to implement
__next__
, if you carefully checkfit_generator
intraining_generator.py
, you will see that another API is used if the generator is a subclass ofSequence
,__next__
is not used for sequences.This was implemented that way because a
Sequence
can be read by multiple workers, that's why it uses a index-based API and not a stateful API like__next__
.