Search code examples
tensorflowkerasdeep-learningtensorboardloss-function

How to log out Keras custom loss component at the end of an epoch?


I have a custom loss function as this format:

def CustomLoss(y_true,y_pred):
    ......
    loss = loss1 + loss2 + loss3
    return loss

How can I return each loss component (loss1, loss2, loss3) at the end of an epoch? By default, I can only observe loss.

Normally, if we have multiple outputs, keras can show it easily. But how to show the value component like what I am mentioned?


Solution

  • You need to create a function for each loss separately and pass them them into metrics when compiling the model as shown below

    def CustomLoss():
        ......
        return loss1() + loss2() + loss3()
    
    def Loss1():
        ......
        return value1
    
    def Loss2():
        ......
        return value2
    
    def Loss3():
        ......
        return value3
    
    
    model.compile(optimizer=Adam(),
                           loss=CustomLoss,
                           metrics=[Loss1, Loss2, Loss3])