'Tensorflow keras custom loss
I have 1000 XRay images and I'm trying to make some binary predictions (died/lived). I have 900 'lived' pictures and 100 'died' pictures and after some reading (mostly here), I think that I may have to make something about this imbalance of the samples.
I tried the
model.compile(...,loss=mycustom_loss,...)
approach but I think that I can't understand the syntax of the custom function. Reading some other documents, I tried the approach:
my_custom_loss_weights = [ 100/1000, 900/1000 ] # index 0 is 'died', index 1 is 'lived'
model.compile = (...,loss='binary_crossentropy',loss_weights=my_custom_loss_weights,...)
which gave some pretty good results (loss and val_loss were very close, acc and val_acc were very close and high enough).
So the questions are about how to make a custom loss function returning binary_crossentropy but weighted based on my weights (100/1000 and 900/1000) and second, if the loss_weights parameter is the solution I want and I'm really that lucky!
I tried the oversample approach of the link I mentioned, but the results were terrible.
Thank you in advance!
Solution 1:[1]
It's completely right how you are doing it, the loss_weights parameter is the way to go and also implemented correctly. It's also described in the link you posted under the paragraph Weighted loss.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Noltibus |
