I have a generator function that infinitely cycles over some directories of images and outputs 3-tuples of batches the form
[img1, img2], label, weight
where img1
and img2
are batch_size x M x N x 3
tensors, and label
and weight
are each batch_size
x 1 tensors.
I provide this generator to the fit_generator
function when training a model with Keras.
For this model I have a custom cosine contrastive loss function,
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)
Structurally everything runs OK with my model. There are no errors and it is consuming the inputs and labels from the generator as expected.
But now I am seeking to directly use the weights parameter per each batch and perform some customized logic inside of cosine_contrastive_loss
based on the sample-specific weight.
How can I access this parameter from the structure of a batch of samples at the moment of the loss function being executed?
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.
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 img1
, img2
and label
at the moment they are generated for a batch.