I would like to use categorical focal loss in tf/keras. Binary Focal loss works for me but not the code I found for categorical f.l. Does someone have this ?
Binary classification Focal Loss can be interpreted as a binary cross-entropy function multiplied by a modulating factor (1- pₜ)^γ which reduces the contribution of easy-to-classify samples. The weighting factor aₜ balances the modulating factor.
Categorical crossentropy is a loss function that is used in multi-class classification tasks. These are tasks where an example can only belong to one out of many possible categories, and the model must decide which one. Formally, it is designed to quantify the difference between two probability distributions.
Focal loss explanation Focal loss is just an extension of the cross-entropy loss function that would down-weight easy examples and focus training on hard negatives. So to achieve this, researchers have proposed: (1- pt)γ to the cross-entropy loss, with a tunable focusing parameter γ≥0.
Creating custom loss functions in Keras A custom loss function can be created by defining a function that takes the true values and predicted values as required parameters. The function should return an array of losses. The function can then be passed at the compile stage.
Sure. I found this by googling Keras focal loss
. It was the first result, and took even less time to implement.
This was the second result on google. Tried it too, and it also works fine; took one of my classification problems up to roc score of 0.9726.
Google is your friend.
binary_focal_loss
sparse_categorical_focal_loss
pip install focal-loss
Binary
model.compile(loss=[binary_focal_loss(alpha=.25, gamma=2)], metrics=["accuracy"], optimizer=adam)
Categorical
model.compile(loss=[categorical_focal_loss(alpha=[[.25, .25, .25]], gamma=2)], metrics=["accuracy"], optimizer=adam)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With