根据纪元更改多输出损失权重

Change multi-output loss weights based on epoch

是否可以根据epoch更改TF2模型不同输出的权重?

现在我正在使用以下代码片段来定义不同输出的优先级:

model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=['mean_squared_error', 'mean_squared_error', 'mean_squared_error'],
              loss_weights=[0.25, 1., 1.])

但是,如果这些权重会根据纪元发生变化,我更愿意。例如像这样:

loss_weights=[0.25 + 0.01*epoch, 1. - 0.005*epoch , 1. - 0.005*epoch]

这可能吗,如果是的话。最好的方法是什么?

tf.keras.Model_loss_weights_list 属性 - 你可以尝试通过自定义回调来改变它,就像 tf.keras.callbacks.LearningRateScheduler 在训练过程中操纵学习率一样。

编辑: 另一个想法是重复调用 model.compile() 然后 model.fit() 仅一个时期:

for epoch_idx in range(total_epochs):
    loss_weights=[0.25 + 0.01 * epoch_idx, 1. - 0.005 * epoch_idx, 1. - 0.005 * epoch_idx]
    model.compile(..., loss_weights=loss_weights)
    model.fit(..., epochs=1, initial_epoch=epoch_idx)