title: How to add weight for each training example while training in keras
body: <p>i'm trying to add weight for each observation while training.<br>
  Depending on how difficult this observation.<br>
  Note: i'm using fit_generator</p>
  
  <p>I'have tried to build a new generator, which generate (x,y,weight)</p>
  
  <pre class="lang-py prettyprint-override"><code>def ensembleCategoricalGenerator(generator, nBatches, batchSize):
      i = 0
      while i &lt; nBatches:
          train_x, train_y = generator[i]
          yhat = classifier.predict(train_x)
          yTrue =  train_y[:, np.newaxis] # convert shape (batchSize,) to (batchSize, 1)
          result = yTrue == yhat.round()
          nCorrect = result.sum()
          correctWeight, incorrectWeight = .5/nCorrect, .5/(batchSize-nCorrect)
          weights = np.array([correctWeight if oneResult else incorrectWeight for oneResult in result])
          yield (train_x, train_y, weights)
          i = i + 1
  #### End
  ensembleGenerator = ensembleCategoricalGenerator(trainIterator,len(trainIterator), batchSize)
  print(type(ensembleGenerator))
  print(type(trainIterator))
  </code></pre>
  
  <p>Output :-  </p>
  
  <pre><code>class 'generator'  
  class 'keras_preprocessing.image.dataframe_iterator.DataFrameIterator'
  </code></pre>
  
  <p>I expected fit_generator to accept my generator but this didn't work.</p>