title: How to access sample weights in a Keras custom loss function supplied by a generator?
body: <p>I have a generator function that infinitely cycles over some directories of images and outputs 3-tuples of batches the form</p>
  
  <pre><code>[img1, img2], label, weight
  </code></pre>
  
  <p>where <code>img1</code> and <code>img2</code> are <code>batch_size x M x N x 3</code> tensors, and <code>label</code> and <code>weight</code> are each <code>batch_size</code> x 1 tensors.</p>
  
  <p>I provide this generator to the <code>fit_generator</code> function when training a model with Keras.</p>
  
  <p>For this model I have a custom cosine contrastive loss function,</p>
  
  <pre><code>def cosine_constrastive_loss(y_true, y_pred):
      cosine_distance = 1 - y_pred
      margin = 0.9
      cdist = y_true * y_pred + (1 - y_true) * keras.backend.maximum(margin - y_pred, 0.0)
      return keras.backend.mean(cdist)
  </code></pre>
  
  <p>Structurally everything runs OK with my model. There are no errors and it is consuming the inputs and labels from the generator as expected.</p>
  
  <p>But now I am seeking to directly use the weights parameter per each batch and perform some customized logic inside of <code>cosine_contrastive_loss</code> based on the sample-specific weight.</p>
  
  <p>How can I access this parameter from the structure of a batch of samples at the moment of the loss function being executed?</p>
  
  <p>Note that since it is an infinitely cycling generator, it is not possible to precompute weights or compute them on the fly to either curry the weights into the loss function or generate them. </p>
  
  <p>They have to be generated in unison with the samples being generated, and indeed there is custom logic in my data generator that determines the weights dynamically from properties of <code>img1</code>, <code>img2</code> and <code>label</code> at the moment they are generated for a batch. </p>