分支输出Keras [英] Branched output Keras

查看:70
本文介绍了分支输出Keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型是这样的,它分为两个输出层,如下所示:

My model is such that it branches into 2 output layers as follows:

输入-> L1-> L2-> L3-> out1

Input -> L1 -> L2 -> L3 -> out1

输入-> L1-> L2-> L3-> out2

Input -> L1 -> L2 -> L3 -> out2

我以这种方式使用它,因为我希望out1out2具有两个不同的激活功能.因此,我创建了一个模型:

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

这是否使用out1上的loss_fn1out2张量上的loss_fn2?因为,这就是我打算做的事情,但是我不确定我的代码是否真正做到了这一点.任何指针都会有所帮助.

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.

我想在out1张量上使用loss_fn1并在out2张量上使用loss_fn2函数.

I want to use loss_fn1 on out1 tensor and loss_fn2 function on out2 tensor.

loss_fn1范围内的损失值:[0,1]-乙状结肠激活.

loss value from loss_fn1 range: [0, 1] - sigmoid activation.

loss_fn2范围内的损失值:[0,inf]-未激活

loss value from loss_fn2 range: [0, inf] - no activation

是否有一种方法可以在不使用单独模型的情况下分别减少loss_fn1和loss_fn2?恐怕损失1 +损失2最终只会导致损失2的价值下降,因为损失1与损失2相比价值较低.

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.

推荐答案

是的,您的解释正确.从 Keras文档:

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.

这篇关于分支输出Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆