Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Categorical focal loss on keras

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 ?

like image 484
aa bb Avatar asked May 24 '19 13:05

aa bb


People also ask

Can focal loss be used for classification?

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.

What is categorical cross-entropy keras?

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.

What is focal loss?

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.

How do you define a loss in keras?

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.


2 Answers

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.

like image 51
TheLoneDeranger Avatar answered Sep 21 '22 06:09

TheLoneDeranger


  1. focal-loss 0.0.6 https://pypi.org/project/focal-loss/
    includes the functions
  • binary_focal_loss

  • sparse_categorical_focal_loss

    pip install focal-loss
    

  1. https://github.com/umbertogriffo/focal-loss-keras
  • 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)
    
like image 44
Aravinda_gn Avatar answered Sep 22 '22 06:09

Aravinda_gn