Weighted Categorical Cross Entropy in Keras

It took me a while until I find where the weighted categorical cross entropy is in the Keras. It’s actually supported by CategoricalFocalCrossentropy. If you set gamma=0 and give alpha=[… list of class weight …], it becomes the weighted one.

Since you’re looking for weighted one probably due to class imbalance, I suggest to look at focal loss itself. It basically tones down the loss of high probably cases and is very effective.

To use the focal loss, you should install tf-nightly, at the moment, i.e., pip install tf-nightly.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *