keras如何处理多重损失?

How does keras handle multiple losses?

如果我有类似的东西:

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? 是不是像:

final_loss = l1*loss1 + l2*loss2

还有,训练时是什么意思? loss2 是否仅用于更新 y2 来自的层的权重?还是用于模型的所有层?

来自model documentation

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: 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.

所以,是的,最终损失将是 "weighted sum of all individual losses, weighted by the loss_weights coeffiecients"。

您可以查看code where the loss is calculated

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?

权重通过 backpropagation 更新,因此每个损失只会影响将输入连接到损失的层。

例如:

                        +----+         
                        > C  |-->loss1 
                       /+----+         
                      /                
                     /                 
    +----+    +----+/                  
 -->| A  |--->| B  |\                  
    +----+    +----+ \                 
                      \                
                       \+----+         
                        > D  |-->loss2 
                        +----+         
  • loss1会影响A、B、C
  • loss2会影响A、B、D

对于反向传播的多个输出,我认为这不是 Fábio Perez 提到的完整答案。

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?

对于输出C和输出D,keras将计算最终损失F_loss=w1 * loss1 + w2 * loss2。然后,最终损失 F_loss 应用于输出 C 和输出 D。最后来自输出 C 和输出 D 的反向传播使用相同的 F_loss 反向传播。