I use keras and I want to combine ssim and some other functions in loss function (link).
To see the values and effect of each components, I define each one as metric as well. But it leads to compute each one twice, once in loss function and another in metric function.
Is there a way to compute each component once and use the values of them as metric and in loss function? Thanks
Try to define your network as a multi-output Model, like:
Model(inputs=your_input, outputs=[your_output, your_output, ...])
Then you can define a different loss function for each of the (identical) outputs, like this:
model.compile(..., loss=[my_loss_1, my_loss_2, ...], loss_weights=[w1, w2, ...])
To make it a bit nicer, you can use "identity" lambda layers to define separate named output layers (and set the losses by name).
out_loss1 = Lambda(lambda x: x, name='loss1')(my_output)
out_loss2 = Lambda(lambda x: x, name='loss2')(my_output)
...
model.compile(..., loss={'loss1': my_loss_1, 'loss2': my_loss_2, ...}, loss_weights={'loss1':w1, 'loss2':w2, ...})
This way your overall loss is a combination of several loss functions, you get the values for each loss and no redundant computations.