custom loss in keras, problem with batch size
One approach could be:



class CustomLoss():
        def __init__(self, steps_per_epoch):
            super().__init__()
            self.steps_per_epoch = steps_per_epoch
            self.step = 0

        def calc_custom_loss(self, y_true, y_pred):
            y_true1 = get_y_true1(self.step)
            y_true2 = get_y_true2(self.step)

            self.step += 1
            self.step %= self.steps_per_epoch


This way you only need to provide get_y_true1() and get_y_true2() functions which receive step index. That index is used to generate appropriate batch.

To use it in code:



# Assuming there are 100 batches in one epoch ...
custom_loss = CustomLoss(steps_per_epoch=100)
model.compile(loss=custom_loss.calc_custom_loss, ...)


Be carefull if you also provide validation data during training as it might change your self.step value ... If you want to use validation data, you will need to handle the step appropriately.