我的模型分为 2 个输出层,如下所示:
输入 -> L1 -> L2 -> L3 -> 输出1
输入 -> L1 -> L2 -> L3 -> 输出
我这样使用它是因为我希望 out1
和 out2
有 2 个不同的激活函数。因此,我创建了一个模型:
model = Model(inputs=[input_layer], outputs=[out1, out2])
我正在使用以下方式编译它:
model.compile(Adam(lr=1e-2), loss=[loss_fn1, loss_fn2], metrics=[accuracy])
损失函数是这样定义的:
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
这是否在 out1
上使用 loss_fn1
并在 out2
张量上使用 loss_fn2
?因为,这就是我打算做的,但我不确定我的代码是否真的做到了这一点。任何指示都会有所帮助。
我想在 out1
张量上使用 loss_fn1
并在 out2
张量上使用 loss_fn2
函数。
编辑:
loss_fn1 范围内的损失值:[0, 1] - sigmoid 激活。
loss_fn2 范围内的损失值:[0, inf] - 未激活
有没有一种方法可以独立减少loss_fn1和loss_fn2,而不使用单独的模型?我担心loss1+loss2最终只会导致loss2的值减少,因为loss1与loss2相比具有较低的值。
最佳答案
是的,你的解释是正确的。来自 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.
关于python - 分支输出 Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774552/