Search code examples
pythonkeras

'tuple' object is not an iterator


The following code is the entry point to my word embedding neural network:

negative_ratio, n_positive = 1, 10
t = Trainer()
epoch = t.generate_batch(n_positive, negative_ratio=negative_ratio)
model = t.model()
h = model.fit_generator(
    epoch,
    epochs=15,
    steps_per_epoch=negative_ratio,
    verbose=2
)

epoch above is a data from a generator that yields (encoded) training data in the following format:

 [[list([57, 41, 49, 50, 55, 19, 26, 38, 5, 14, 51])
  list([50, 0, 0, 0, 0, 49, 0, 0, 0, 0, 26, 0, 0, 0, 0, 41, 55, 19, 38, 5, 51, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 57, 0, 0, 0, 0, 0, 0, 0])
  1]
 [list([35, 50, 12, 15, 21, 19, 26, 34, 13, 52])
  list([50, 12, 0, 0, 0, 0, 0, 0, 0, 0, 26, 34, 0, 0, 0, 21, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 15, 13, 0, 0, 0, 0, 0, 0])
  1]
 [list([20, 28, 41, 56, 2, 1, 51, 23, 22])
  list([28, 0, 0, 22, 0, 0, 0, 0, 0, 0, 2, 23, 0, 0, 0, 41, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 20, 56, 0, 0, 0, 0, 0, 0, 0])
  1]
 [list([30, 20, 9, 12, 15, 19, 34, 5, 52, 51, 22])
  list([12, 0, 0, 22, 0, 0, 0, 9, 0, 0, 34, 0, 0, 0, 0, 19, 5, 51, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 20, 15, 0, 0, 0, 0, 0, 0, 0])
  1]]

Keras keeps telling me, though, that the generator is not valid:

TypeError: 'tuple' object is not an iterator

What am I doing wrong?


Solution

  • As the error says, you're passing a tuple instead of a generator object. fit_generator() expects a generator object. Internally, it would call next() on the generator object to get a batch of data.

    If I do, next() on a tuple, I would get the same error:

    >>> next((1,2))
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
        next((1,2))
    TypeError: 'tuple' object is not an iterator
    
    >>> sample_generator = ((1,2) for i in range(3))
    >>> x,y = next(sample_generator)
    >>> x,y
    (1, 2)