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

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

问题描述

如果我有类似的东西:

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

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

Keras 用loss 怎么做得到最终的loss?是不是类似:

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:字符串(目标函数名称)或目标函数.见损失.如果模型有多个输出,您可以通过传递字典或损失列表对每个输出使用不同的损失.模型将最小化的损失值将是所有单个损失的总和.

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.
    • 这篇关于keras 如何处理多重损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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