Can we use multiple loss functions in same layer?

前端 未结 2 1692
暗喜
暗喜 2021-01-06 23:43

Can we use mulitple loss function in this architecture: I have two different type of loss functions and want to use it on last layer [Output] loss functions :

  • b
2条回答
  •  长发绾君心
    2021-01-07 00:08

    You can calculate two different losses. Then get weighted mean and return as the final value of the loss. Technically, it can be implemented like this (that is an example, I didn't run it):

    def joint_loss(y_true, y_pred):
        part_binary_crossentropy = 0.4
        part_custom = 0.6
    
        # binary_crossentropy
        loss_binary_crossentropy = tf.keras.losses.binary_crossentropy(y_true, y_pred)
    
        # custom_loss
        loss_custom = some_custom_loss(y_true, y_pred))
    
        return part_binary_crossentropy * loss_binary_crossentropy + part_custom * loss_custom
    
    
    model.compile(loss=joint_loss, optimizer='Adam')
    

提交回复
热议问题