喀拉拉邦如何处理多重损失? [英] How does keras handle multiple losses?

查看:213
本文介绍了喀拉拉邦如何处理多重损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有类似的东西:

model = Model(inputs = input, outputs = [y1,y2])

l1 = 0.5
l2 = 0.3
model.compile(loss = [loss1,loss2], loss_weights = [l1,l2], ...)

Keras如何处理损失以获得最终损失? 是这样的吗?

what does Keras do with the losses to obtain the final loss? Is it something like:

final_loss = l1*loss1 + l2*loss2

此外,这在培训期间是什么意思? loss2是否仅用于更新y2来源的图层上的权重?还是将其用于模型的所有图层?

Also, what does it mean during training? Is the loss2 only used to update the weights on layers where y2 comes from? Or is it used for all the model's layers?

推荐答案

来自 model文档 :

损失:字符串(目标函数的名称)或目标函数.见损失.如果模型具有多个输出,则可以通过传递字典或损失列表来在每个输出上使用不同的损失.该模型将使损失值最小化,将是所有单个损失的总和.

loss: String (name of objective function) or objective function. See losses. 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.

...

loss_weights :可选列表或字典,用于指定标量系数(Python浮点数)以加权不同模型输出的损失贡献.然后,将由模型最小化的损失值将是所有单个损失的加权总和,并由loss_weights系数加权.如果是列表,则期望与模型的输出具有1:1的映射.如果是张量,则期望将输出名称(字符串)映射到标量系数.

loss_weights: Optional list or dictionary specifying scalar coefficients (Python floats) to weight the loss contributions of different model outputs. The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the loss_weights coefficients. If a list, it is expected to have a 1:1 mapping to the model's outputs. If a tensor, it is expected to map output names (strings) to scalar coefficients.

所以,是的,最终损失将是所有个体损失的加权总和,由loss_weights系数加权".

So, yes, the final loss will be the "weighted sum of all individual losses, weighted by the loss_weights coeffiecients".

您可以检查此处的代码损失是计算出来的.

此外,这在培训期间是什么意思? loss2是否仅用于更新y2来源的图层上的权重?还是将其用于模型的所有图层?

Also, what does it mean during training? Is the loss2 only used to update the weights on layers where y2 comes from? Or is it used for all the model's layers?

权重通过反向传播进行更新,因此每次丢失只会影响连接输入的图层.损失.

The weights are updated through backpropagation, so each loss will affect only layers that connect the input to the loss.

例如:

                        +----+         
                        > C  |-->loss1 
                       /+----+         
                      /                
                     /                 
    +----+    +----+/                  
 -->| A  |--->| B  |\                  
    +----+    +----+ \                 
                      \                
                       \+----+         
                        > D  |-->loss2 
                        +----+         

  • loss1将影响A,B和C.
  • loss2将影响A,B和D.
    • loss1 will affect A, B, and C.
    • loss2 will affect A, B, and D.
    • 这篇关于喀拉拉邦如何处理多重损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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