title: Keras: Correct use of fit_generator, predict_generator, and evaluate_generator
body: <p>I'm encountering weird behaviour when using <code>fit_generator</code>, <code>predict_generator</code>, and <code>evaluate_generator</code>, and I would like to ask the following questions, for which I could find no answer in the documentation:</p>
  
  <ol>
  <li>Is it ok to have batches of different sizes when using <code>fit_generator</code>?</li>
  </ol>
  
  <p>My batches are defined time-wise: they group the events that took place in the same hour. Each batch can therefore group a different number of events. For clarity, this is how my generators look like (following the logic in <a href="https://stackoverflow.com/questions/46647008/keras-aggregated-objective-function">this thread</a>):</p>
  
  <pre><code>def grouper(g,x,y):
       while True:
          for gr in g.unique():
              # this assigns indices to the entire set of values in g,
              # the subsects to all the rows in which g == gr
              indices = g == gr
              yield (x[indices],y[indices])
  
  all_data_generator = grouper(df['batch_id'], X, Y)
  train_generator = grouper(df.loc[df['set'] == 'train', 'batch_id'], X_train, Y_train)
  validation_generator = grouper(df.loc[df['set'] == 'val', 'batch_id'], X_val, Y_val)
  test_generator = grouper(df.loc[df['set'] == 'test', 'batch_id'], X_test, Y_test)
  </code></pre>
  
  <ol start="2">
  <li>Is it ok to have different number of batches in <code>train_generator</code> and <code>validation_generator</code>? </li>
  </ol>
  
  <p>For clarity, I pass those two (different) numbers explicitly to <code>fit_generator</code> in the call:</p>
  
  <pre><code>train_batches = df.loc[df['set'] == 'train', 'batch_id'].nunique()
  val_batches = df.loc[df['set'] == 'val', 'batch_id'].nunique()
  
  history = fmodel.fit_generator(train_generator, 
                               steps_per_epoch=train_batches, 
                               validation_data=validation_generator,
                               validation_steps=val_batches,
                               epochs=20, verbose = 0)
  </code></pre>
  
  <ol start="3">
  <li>Predictions are wildly different depending on whether I use <code>predict_classes</code> or <code>predict_generator</code>, which baffles me.</li>
  </ol>
  
  <p>Here's the code:</p>
  
  <pre><code>df['pred'] = fmodel.predict_classes(X)
  
  # returns different results from
  total_batches = df['batch_id'].nunique()
  df['pred_gen'] = fmodel.predict_generator(all_data_generator, steps = total_batches)
  </code></pre>
  
  <ol start="4">
  <li>Similarly, <code>evaluate</code> and <code>evaluate_generator</code> return different results.</li>
  </ol>
  
  <p>The code:</p>
  
  <pre><code>scores = model.evaluate(X_test, Y_test, verbose = 0)
  
  # returns different results from
  scores_generator = fmodel.evaluate_generator(test_generator, steps=test_batches)
  </code></pre>
  
  <p>I know there are already many issues referring to my points 3. and 4. (e.g., <a href="https://github.com/keras-team/keras/issues/3477" rel="nofollow noreferrer">3477</a>, <a href="https://github.com/keras-team/keras/issues/6499" rel="nofollow noreferrer">6499</a>) but the main takeaways there seem to refer to </p>
  
  <ul>
  <li>using ImageDataGenerator with/without rescaling;</li>
  <li>shuffling data, which according to <a href="https://keras.io/models/sequential/" rel="nofollow noreferrer">fit_generator documentation</a> "Has no effect when steps_per_epoch is not None."</li>
  <li>using <code>workers &gt; 1</code>, which by default is not the case.</li>
  </ul>
  
  <p>So I'm wondering whether points 1. and 2. might be the culprits here.</p>