项目介绍

jrzaurin/LightGBM-with-Focal-Loss 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,目前获得 253 个 Star、39 次 Fork。

仓库全名
jrzaurin/LightGBM-with-Focal-Loss
Star 数
253
Fork 数
39
未关闭 Issue
3

README 摘要

LightGBM with Focal Loss This is implementation of the Focal Loss[1] to be used with LightGBM. The companion Medium post can be found here. The Focal Loss for LightGBM[2] can be simply coded as: to use it one would need the corresponding evaluation function: And to use it, simply: In the examples directory you will find more details, including how to use Hyperopt in combination with LightGBM and the Focal Loss, or…

完整内容见 GitHub 原始 README 或本页 。

常见问题

LightGBM-with-Focal-Loss 用什么语言开发?

主要使用 Python 编写,目前有 253 个 Star。

如何获取 LightGBM-with-Focal-Loss 的源码?

可以直接克隆仓库:git clone https://github.com/jrzaurin/LightGBM-with-Focal-Loss.git,默认分支为 master;安装与运行步骤见仓库 README。

LightGBM-with-Focal-Loss 最近还在维护吗?

仓库最近一次更新为 2019年11月10日,当前有 3 个未关闭 Issue;Star 数为 253,可作为社区活跃度的参考。

关于

An implementation of the focal loss to be used with LightGBM for binary and multi-class classification problems

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
334
价值分
163
活跃状态
活跃

语言占比

Python

贡献者

README

LightGBM with Focal Loss

This is implementation of the Focal Loss[1] to be used with LightGBM.

The companion Medium post can be found here.

The Focal Loss for LightGBM[2] can be simply coded as:

def focal_loss_lgb(y_pred, dtrain, alpha, gamma):
	a,g = alpha, gamma
	y_true = dtrain.label
	def fl(x,t):
		p = 1/(1+np.exp(-x))
		return -( a*t + (1-a)*(1-t) ) * (( 1 - ( t*p + (1-t)*(1-p)) )**g) * ( t*np.log(p)+(1-t)*np.log(1-p) )
	partial_fl = lambda x: fl(x, y_true)
	grad = derivative(partial_fl, y_pred, n=1, dx=1e-6)
	hess = derivative(partial_fl, y_pred, n=2, dx=1e-6)
	return grad, hess

to use it one would need the corresponding evaluation function:

def focal_loss_lgb_eval_error(y_pred, dtrain, alpha, gamma):
	a,g = alpha, gamma
	y_true = dtrain.label
	p = 1/(1+np.exp(-y_pred))
	loss = -( a*y_true + (1-a)*(1-y_true) ) * (( 1 - ( y_true*p + (1-y_true)*(1-p)) )**g) * ( y_true*np.log(p)+(1-y_true)*np.log(1-p) )
	return 'focal_loss', np.mean(loss), False

And to use it, simply:

focal_loss = lambda x,y: focal_loss_lgb(x, y, 0.25, 1.)
eval_error = lambda x,y: focal_loss_lgb_eval_error(x, y, 0.25, 1.)
lgbtrain = lgb.Dataset(X_tr, y_tr, free_raw_data=True)
lgbeval = lgb.Dataset(X_val, y_val)
params  = {'learning_rate':0.1, 'num_boost_round':10}
model = lgb.train(params, lgbtrain, valid_sets=[lgbeval], fobj=focal_loss, feval=eval_error )

In the examples directory you will find more details, including how to use Hyperopt in combination with LightGBM and the Focal Loss, or how to adapt the Focal Loss to a multi-class classification problem.

Any comment: [email protected]

References:

[1] Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár. Focal Loss for Dense Object Detection

[2] Guolin Ke, Qi Meng Thomas Finley, et al., 2017. LightGBM: A Highly Efficient Gradient Boosting Decision Tree

评论

暂无评论