+2 votes
1 view
in Machine Learning by (4.8k points)

I built a simple generator that yields a tuple(inputs, targets) with only single items in the inputsand targets lists. Basically, it is crawling the data set, one sample item at a time.

I pass this generator into:

  model.fit_generator(my_generator(),
                      nb_epoch=10,
                      samples_per_epoch=1,
                      max_q_size=1  # defaults to 10
                      )

I get that:

  • nb_epoch is the number of times the training batch will be run
  • samples_per_epoch is the number of samples trained with per epoch

But what is max_q_size for and why would it default to 10? I thought the purpose of using a generator was to batch data sets into reasonable chunks, so why the additional queue?

1 Answer

+3 votes
by (7.9k points)

This simply defines the maximum size of the internal training queue which is used to "precache" your samples from the generator. It is used during generation of the queues

def generator_queue(generator, max_q_size=10, wait_time=0.05, nb_worker=1): '''Builds a threading queue out of a data generator. Used in `fit_generator`, `evaluate_generator`, `predict_generator`. ''' 

  q = queue.Queue() 

  _stop = threading.Event() 

   def data_generator_task(): 

     while not _stop.is_set(): 

        try: 

           if q.qsize() < max_q_size: 

             try: 

                generator_output = next(generator) 

                except ValueError:

                    continue q.put(generator_output)

         Else:

            time.sleep(wait_time) 

            except Exception:

            _stop.set()

     raise generator_threads = [threading.Thread(target=data_generator_task) for _ in range(nb_worker)] 

     for thread in generator_threads: 

        thread.daemon = True thread.start() 

  return q, _stop

In other words, you have a thread filling the queue up to give, maximum capacity directly from your generator, while (for example) training routine consumes its elements (and sometimes waits for the completion) and why default of 10? No particular reason, like most of the defaults - it simply makes sense, but you could use different values too.

while samples_seen < samples_per_epoch: 

generator_output = None 

while not _stop.is_set(): 

if not data_gen_queue.empty():

  generator_output = data_gen_queue.get()

  Break

  else: 

time.sleep(wait_time)

Construction like this suggests, that authors thought about expensive data generators, which might take time to execute. For example, consider downloading data over a network in generator call - then it makes sense to precache some next batches, and download next ones in parallel for the sake of efficiency and to be robust to network errors, etc.

...