我的模型是这样的,它分为 2 个输出层,如下所示:
输入 -> L1 -> L2 -> L3 -> 输出 1
输入 -> L1 -> L2 -> L3 -> 输出 2
我这样使用它是因为我希望 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 的值(value)下降,因为 loss1 与 loss2 相比具有较低的值(value)。
请您参考如下方法:
是的,你的解释是正确的。来自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.
