mathlf2015 / Xgboost-With-Imbalance-And-Focal-Loss

Small project to embed Weighted Imbalanced Loss and Focal Loss to Xgboost

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Xgboost-With-Imbalance-And-Focal-Loss

This small project includes the codes of Weighted Imbalance Loss and Focal Loss [1] for Xgboost [2](<\url> https://github.com/dmlc/xgboost) in 2-class classification problems. The principal reason for us to use Weighted Imbalance Loss and Focal Loss is to address the problem of label-imbalanced data, which could significantly degrade the performance of Xgboost. The original Xgboost program provides a convinient method to customize the loss function, but one will be needing to compute the first+second order derivatives and implement them. The major contribution of the project to the drivation of the gradients and the implementations of them.
For both of the loss functions, since the task is 2-class classification, the activation would be sigmoid:

And below the two types of loss will be discussed respectively.

1. Weighted Imbalance (Cross-entropoy) Loss

And combining with $\hat{y}$, which are the true labels, the weighted imbalance loss for 2-class data could be denoted as:

Where $\alpha$ is the 'imbalance factor'. And $\alpha$ value greater than 1 means to put extra loss on 'classifying 1 as 0'.
The gradient would be:

And the second order gradient would be:

2. Focal Loss

The focal loss is proposed in [1] and the expression of it would be:

The first order gradient would be:

And the second order gradient would be a little bit complex. To simplify the expression, we firstly denotes the terms in the 1-st order gradient as the following notations:

Then the 2-nd order derivative will be:

Scared by the above equations? It's ok

That's the reason I upload this to github: I have done the job and coded them into the script, you can simply use it and ignore the equations! Remember to call Xgboost_classsifier_sklearn class and specify the parameter special_objective when implementing the class to an object. Also, you can change the prarameter $\alpha$ or $\gamma$ inside the script.

Enjoy Using!

@author: Chen Wang, College of Science and Art, Rutgers University (previously affiliated with University College London, Sichuan University and Northwestern Polytechnical University)
@version: 0.1

References

[1] Lin, Tsung-Yi, Priyal Goyal, Ross Girshick, Kaiming He, and Piotr Dollár. "Focal loss for dense object detection." IEEE transactions on pattern analysis and machine intelligence (2018).
[2] Chen, Tianqi, and Carlos Guestrin. "Xgboost: A scalable tree boosting system." In Proceedings of the 22nd acm sigkdd international conference on knowledge discovery and data mining, pp. 785-794. ACM, 2016.

About

Small project to embed Weighted Imbalanced Loss and Focal Loss to Xgboost


Languages

Language:Python 100.0%