I am trying to create a custom loss function,custom_loss(y_true, y_pred). I understand that y_pred is calculated by my model but I want to deliver, two kinds of y_true, such as y_true1 and ytrue2(these are pre-calculated, not delivered in model.fit(), and used as follows: (y_pred - y_true1/ y_pred - ytrue2). The problem is, I have batch size mis-match problem, because, my custom y_true1 and y_true2 is created on my total dataset. How to make their batch size as y_pred?


1 Answer 1


One approach could be:

class CustomLoss():
        def __init__(self, steps_per_epoch):
            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.

  • $\begingroup$ I am using validation_split=0.1 in the model.fit(), will this be a problem? $\endgroup$
    – N. F.
    Dec 8, 2018 at 9:10
  • $\begingroup$ In a case where you want to have also a validation spilt, you will have to keep two counters separatly: one for train steps and one for valid steps. After the train steps are finsihed, switch to valid steps counter, and later, after valid steps are finished, switch to train step counter.... Also, you will have to split the data to train and valid splits prior calling the model.fit() method. $\endgroup$ Dec 9, 2018 at 19:07
  • $\begingroup$ I think what you are proposing is easier to implement or its the way to go for other libraries like PyTorch, keras is troublesome for doing this things. $\endgroup$
    – N. F.
    Dec 10, 2018 at 6:20
  • 1
    $\begingroup$ Just the best answer I could think of to your question :) I and find it quite easy, but that's my opinion :) I did a research on this because I needed I similar stuff for custom metrics and this is what I have found. With metrics it's easier since you can use Callback classes ... $\endgroup$ Dec 10, 2018 at 8:32
  • $\begingroup$ Thanks, I am now using PyTorch and found it easier to implement what I wanted to. Nevertheless, thanks for your answer. $\endgroup$
    – N. F.
    Dec 10, 2018 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.