Keras has parameters class_weight
used in fit()
function and loss_weights
used in compile()
function. From the explanation(Docs) and what I understand, it seems that both are identical, as they are used to indicate the importance of each class. However, what is the difference between the two? And what are the scenarios in which they can be appropriately used?
2 Answers
From Keras Team at GitHub:
loss_weights parameter on compile is used to define how much each of your model output loss contributes to the final loss value ie. it weighs the model output losses. You could have a model with 2 outputs where one is the primary output and the other auxiliary. eg. 1. * primary + 0.3 * auxiliary. The default values for loss weights is 1.
class_weight parameter on fit is used to weigh the importance of each sample based on the class they belong to, during training. This is typically used when you have an uneven distribution of samples per class.
-
1$\begingroup$ loss_weights still not that clear $\endgroup$ Commented Feb 7, 2021 at 4:07
As I understand it: If you have one loss function this produces one loss output per sample. So if you have multiple loss functions this will generate multiple loss outputs. When using loss_weights this will weight the multiple loss function outputs per sample.
For class_weight this will look at the class of the true label and multiply its corresponding weight with the loss output.