问题
My model is such that it branches into 2 output layers as follows:
Input -> L1 -> L2 -> L3 -> out1
Input -> L1 -> L2 -> L3 -> out2
I am using it this way because I want out1
and out2
to have 2 different activation functions. Therefore, I have created a model:
model = Model(inputs=[input_layer], outputs=[out1, out2])
I am compiling it using:
model.compile(Adam(lr=1e-2), loss=[loss_fn1, loss_fn2], metrics=[accuracy])
loss functions are defined this way:
def loss_fn1(y_true, y_pred):
#send channel 1 to get bce dice loss
loss1 = binary_crossentropy(y_true[:, :, :, 0:1], y_pred[:, :, :, 0:1])
return loss1
def loss_fn2(y_true, y_pred):
#l2 loss for channels 2 and 3
loss2 = mean_squared_error(y_true[:, :, :, 1:3], y_pred[:, :, :, 1:3])
return loss2
Does this use loss_fn1
on out1
and loss_fn2
on out2
tensor? Because, that is what I intend to do, but I am unsure regarding whether my code actually does that. Any pointers would help.
I want to use loss_fn1
on out1
tensor and loss_fn2
function on out2
tensor.
EDIT:
loss value from loss_fn1 range: [0, 1] - sigmoid activation.
loss value from loss_fn2 range: [0, inf] - no activation
Is there a way to reduce loss_fn1 and loss_fn2 independently, without using separate models? I am afraid that loss1 + loss2 would eventually only cause a decrease in value of loss2 as loss1 has a low value in comparison to loss2.
回答1:
Yes, your interpretation is right. From the Keras documentation:
If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.
来源:https://stackoverflow.com/questions/47774552/branched-output-keras