title: unwrap a generator to use inside model.fit
body: <p>I have exactly the same problem as that addressed in this <a href="https://stackoverflow.com/questions/50928329/getting-x-test-y-test-from-generator-in-keras">post</a>: I cannot use generator in the training input of <code>model.fit</code> in <code>keras</code>, so I should unwrap it. The proposed solution:</p>
  <pre><code>from platform import python_version_tuple
  
  if python_version_tuple()[0] == '3':
      xrange = range
      izip = zip
      imap = map
  else:
      from itertools import izip, imap
  
  import numpy as np
  
  # ..
  # other code as in question
  # ..
  
  x, y = izip(*(validation_seq[i] for i in xrange(len(validation_seq))))
  x_val, y_val = np.vstack(x), np.vstack(y)
  </code></pre>
  <p>is what I am exactly looking for. The problem is that it works for the <code>ImageDataGenerator()</code> of the initial <a href="https://stackoverflow.com/questions/50928329/getting-x-test-y-test-from-generator-in-keras">question</a>, but it does not for my generator that is as follows:</p>
  <pre><code>def generator(data, L, D, i_min, i_max, shuffle=False, batch_size=16, step=1):
    if i_max is None:
       i_max = len(data) - D - 1
    i = i_min + L
    while 1:
       if shuffle:
          rows = np.random.randint(i_min + L, i_max, size=batch_size)
       else:
          if i + batch_size &gt;= i_max:
             i = i_min + L
          rows = np.arange(i, min(i + batch_size, i_max))
          i += len(rows)
       samples = np.zeros((len(rows), L // step, data.shape[-1]))
       targets = np.zeros((len(rows),))
       for j, row in enumerate(rows):
          indices = range(rows[j] - L, rows[j], step)
          samples[j] = data[indices]
          targets[j] = data[rows[j] + D][3]  # where is Q in your data
       yield samples, targets
  
  
  data = np.random.standard_normal([256,4])
  generator = generator(data=data, L=8, D=1, i_min=0, i_max=255, shuffle=False, batch_size=16, step=1)
  </code></pre>
  <p>When I execute <code>izip(*(generator[i] for i in xrange(len(generator))))</code>, I got this error: <code>object of type 'generator' has no len()</code>.</p>
  <p>I have already tried to replace <code>xrange(len(generator))</code> by <code>len(list(generator))</code>, <code>enumerate(generator)</code>, and none of them work. How could I fix this problem? Thank you.</p>
  <p>PS: I am using <code>python 3.8</code> on osx 10.13.6.</p>
  <p><strong>UPDATE</strong>:
  <em>based on the answer by @couka I tried to make the class generator, but it still does not work.</em></p>
  <pre><code>class batch_gen:
    def __init__(self, data, L, D, min_index, max_index, shuffle, batch_size, step):
       self.data = data
       self.L = L
       self.D = D
       self.min_index = min_index
       self.max_index = max_index
       self.shuffle = shuffle
       self.batch_size = batch_size
       self.step = step
   
    def __iter__(self):
       if self.max_index is None:
          self.max_index = len(self.data) - self.D - 1
       i = self.min_index + self.L
       while 1:
          if self.shuffle:
             rows = np.random.randint(self.min_index + self.L, self.max_index, size=self.batch_size)
          else:
             if i + self.batch_size &gt;= self.max_index:
                i = self.min_index + self.L
             rows = np.arange(i, min(i + self.batch_size, self.max_index))
             i += len(rows)
          samples = np.zeros((len(rows), self.L // self.step, self.data.shape[-1]))
          targets = np.zeros((len(rows),))
          for j, row in enumerate(rows):
             indices = range(rows[j] - self.L, rows[j], self.step)
             samples[j] = self.data[indices]
             targets[j] = self.data[rows[j] + self.D][3]  # where is Q in your data
          yield samples, targets
   
    def __len__(self):
       return int(math.floor(len(self.data) / float(self.batch_size)))
  </code></pre>
  <p>when I use:</p>
  <pre><code>gen_tr = batch_gen(data=data, L=L, D=D,
                    min_index=min(ind_tr), max_index=max(ind_tr),
                    shuffle=True, step=step, batch_size=batch_size)
  </code></pre>
  <p>and I got this error: <code>TypeError: 'batch_gen' object is not subscriptable</code>.</p>