Loss involving multiple outputs

How do I get multiple outputs from a model and get these outputs to interact with each other in different custom loss functions?
I will then need to feed in the respective loss for each output.
eg. for instance I have three outputs : out1, out2, final
out1 and out2 are predictions from intermediate blocks while final is the prediction from the final block.
Loss for out1 will be :
kl loss between out1 and final + binary cross entropy between out1 and the actual label. This loss needs to be applied on out1. Similarly , I wish to do this for out2.
I have tried several different variations for getting multiple outputs and using them in a loss function, all of them are throwing different errors. Please help!

First model to generates explicit multiple outputs. Add separate Dense layers for each output like below.

model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=50, name='out1', activation='softmax'),
    tf.keras.layers.Dense(units=30 name='out2', activation='sigmoid'),
    tf.keras.layers.Dense(units=2 name='final', activation='softmax')

Now create your own custom loss functions calculate each loss1 and loss2 and then return the loss1 + loss2 in the same custom loss function and Finally feed Losses to the Optimizer like below.

losses = {'out1': loss_out1, 'out2': loss_out2, 'final': 'categorical_crossentropy'}
model.compile(optimizer='adam', loss=losses)

Hope this helps you and above approach is very generic.


However, I need to compute a loss between out1 and final and apply this to out1 layer alone and similarly for out2. How would I do that?