Search code examples
pythontensorflowkerasloss-function

how to add tensorflow loss functions?


I can not add these two losses as follows

real_loss = tf.losses.BinaryCrossentropy(tf.ones_like(train_images[0]),train_images[0])
fake_loss = tf.losses.BinaryCrossentropy(tf.zeros_like(train_images[0]),train_images[0])
fake_loss+real_loss

the error is:

TypeError: unsupported operand type(s) for +: 'BinaryCrossentropy' and 'BinaryCrossentropy'


Solution

  • You can just add them as multiple losses in model.compile

    model.compile(loss = [loss1,loss2], loss_weights = [l1,l2], ...)
    

    This translates to final_loss = l1*loss1 + l2*loss2. Just set l1 and l2 as 1.