Keras loss weights


I’ve model with two output layers, age and gender prediction layers. I want to assign different weight values for each output layer’s loss. I’ve the following line of code to do so.

model.compile(loss=[losses.mean_squared_error,losses.categorical_crossentropy], optimizer='sgd',loss_weights=[1,10])

My question is what is the effect of loss weights on performance of a model? How can I configure the loss weights so that the model can perform better on age prediction?


As stated in the book Deep Learning with Python by François Chollet:

The mean squared error (MSE) loss used for the age-regression task
typically takes a value around 3–5, whereas the crossentropy loss used
for the gender-classification task can be as low as 0.1. In such a
situation, to balance the contribution of the different losses, you
can assign a weight of 10 to the crossentropy loss and a weight of
0.25 to the MSE loss.

Answered By – Grigorios Kalliatakis

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published