Processing math: 100%

透視 XGBoost(4) 神奇 optimization 在哪裡?

XGBoost 不只在算法上的改造,真正讓 XGBoost 大放異彩的是與工程優化的結合,這讓 XGBoost 在工業上可以應用於大規模數據的處理。

  • Approximate Greedy Algorithm
  • Weighted Quantile Sketch & Parallel Learning
  • Sparsity-Aware Split Finding
  • System Design

Approximate Greedy Algorithm

XGBoost 有兩種 split finding 策略,Exact Greedy and Approximate Greedy

data sample 量小的情況下, XGB tree 在建立分支時,會逐一掃過每個可能分裂點 (已對特徵值排序) 計算 similarity score 跟 gain, 即是 greedy algorithm。

greedy algorithm 會窮舉所有可能分裂點,因此能達到最準確的結果,因為他把所有可能的結果都看過一遍後選了一個最佳的。

但如果 data sample 不僅數量多 (row),特徵維度也多 (columns),採用 greedy algorithm,雖然準確卻也沒效率。


透視 XGBoost(3) 蘋果樹下的 objective function

XGBoost General Objective Function

XGboost 是 gradient boosting machine 的一種實作方式,xgboost 也是建一顆新樹 fm(x) 去擬合上一步模型輸出 Fm1(x)residual

Fm(x)=Fm1(x)+fm(x)Fm1(x)=F0(x)+m1i=1fi(x)

不同的是, XGBoost 用一種比較聰明且精準的方式去擬合 residual 建立子樹 fm(x)

此篇首先推導了 XGBoost 的通用 Objective Function,然後解釋為何 second order Taylor expansion 可以讓 XGBoost 收斂更快更準確

XGBoost’s Objective Function

XGBoost 的 objective function 由 loss function term l(yi,^yi) 和 regularized term Ω 組成

L=[nil(yi,^yi)]+mΩ(fm)
  • L 表 objective function
  • l(yi,^yi) 表 loss function
    • in regression, loss function l(yi,^yi) commonly use square error
    • in classification,最大化 log likelihood 就是最小化 cross entropy
  • fm 表 第 m 步 XGB tree
  • regularized term Ω 跟 XGB tree fm(x) leaf node number Tm 與 each leaf node output γ 有關
    • Ω(fm)=τTm+12λγ2
      • λ is a regularization parameter
      • Tm is the number of leaf in tree fm
      • τ 表對 Tm 的 factor

在 第 m1 步建完 tree fm1時,total cost 為

L(m1)=[ni=1l(yi,^yi(m1))]+Ω(f(m1))=[ni=1l(yi,F(m1)(xi)]+Ω(f(m1))
  • ^yi(m1)Fm1(xi) 預測值
  • fm1 表 第 m1 步建立的 XGB tree

進入第 m 步,我們建一顆新樹 fm(x) 進一步減少 total loss,使得 L(m)<L(m1) , 則 m 步 cost function 為

L(m)=[nil(yi,Fm1(xi)+fm(xi))]+Ω(f(m))<L(m1)

現在要找出新樹 fm(xi) 的每個 leaf node 輸出能使式 (5) L(m) 最小

γj,m=argminγxiRj,m[l(yi,Fm1(xi)+γ)]+Ω(f(m))
  • j 表 leaf node index
  • m 表第 m
  • γj,m mfmj 個 leaf node 的輸出值
  • Rjmm 步 第 j 個 leaf node,所包含的 data sample x 集合

當 loss function l(y,ˆy) 為 square error 時,求解式 (6) 不難,但如果是 classification problem 時,就變得很棘手。

所以 GBDT 對 regression problem 與 classification problem 分別用了兩種不同方式求解極值,求出 leaf node 的輸出

Second Order Tayler Approximation 的步步逼近

XGBoost 直接以 Second Order Tayler Approximation 處理 classification 跟 regression 的 loss function part l(y,ˆy)

式(5) 的 loss function partˆym1(xi) 附近展開:

l(y,ˆy(m1)+fm(x))l(y,ˆy(m1))+[dd ˆy(m1) l(y,ˆy(m1))]fm(x)+12[d2d (ˆy(m1))2 l(y,ˆy(m1))]fm(x)2=l(y,Fm1(x))+[dd Fm1 l(y,Fm1)]fm(x)+12[d2d F2m1l(y,Fm1)]fm(x)2=l(y,Fm1(x))+gfm(x)+12hfm(x)2
  • ˆy(m1) 為 XGB 在 m1 步的 prediction Fm1(x)
  • g=dd Fm1 l(y,Fm1)
  • h=d2d2 Fm1l(y,Fm1)

將式 (7) 代入式(5):

L(m) [ni(l(yi,ˆy(m1)i)+gifm(xi)+12hifm(xi)2]+Ω(fm)
  • gi 為 first derivative of loss function related to data sample xi
  • hi 為 second derivative of loss function related to data sample xi
  • fm(xi)xi 在 XGB tree fm(x) 的 output value

式 (8) 即 XGBoost 在 m 步的 cost function

L(m) 束手就擒

Second Order Tayler Approximation 逼近 loss function l(y,ˆy)後 ,L(m)γj,m 極值,就變得容易多了

先將 regularization term Ω(fm) 展開代入式 (8),並拿掉之後對微分沒影響的常數項 l(yi,ˆy(m1)i)

˜L(m)=[ni(gifm(xi)+12hifm(xi)2)]+[τTm+12λTjγ2m,j]=Tmj=1[(iRm,jgi)γj,m+12(iRj,mhi+λ)γ2j,m]+τTm
  • Tm is the number of leaf in tree fm
  • τ 表對 Tm 的 factor
  • γj,mm 步 XGB tree fmj 個 leaf node 的輸出值
  • Rm,j 表 leaf node j 下的 data sample x 集合
  • 第一個等式到第二個等式 : 從原本遍歷所有 data sample x 到遍歷所有 leaf node Rm,j 下的 data sample

式 (9) 對 γj,m 微分取極值

dd γj,m˜L(m)=Tmj=1[(iRm,jgi)+(iRj,mhi)γj,m]=0

對於已經固定結構了 tree fm(x),式 (10) leaf node j 的 output value γj,m

γj,m=iRj,mgiiRj,mhi+λ
  • gi 為 first derivative of loss function related to data sample xi : gi=dd Fm1 l(yi,Fm1(xi))
  • hi 為 second derivative of loss function related to data sample xi: hi=d2d F2m1l(yi,Fm1(xi))

將 式(11) γj,m 代回 式 (9),即 objective function 的極值

˜L(m)=Tmj=1[(iRm,jgi)γj,m+12(iRj,mhi+λ)γ2j,m]+τTm=Tmj=1[(iRj,mgi)iRj,mgiiRj,mhi+λ+12(iRj,mhi+λ)(iRj,mgiiRj,mhi+λ)2]+τTm=Tmj=1[(gi)2hi+λ+12(gi)2hi+λ]+τTm=12Tmj=1[(gi)2hi+λ]+τTm

總結

XGBoost 通用 Objective Function

˜L(m)=Tmj=1[(iRm,jgi)γj,m+12(iRj,mhi+λ)γ2j,m]+τTm
  • gi 為 first derivative of loss function related to data sample xi : gi=dd Fm1 l(yi,Fm1(xi))
  • hi 為 second derivative of loss function related to data sample xi: hi=d2d F2m1l(yi,Fm1(xi))
  • Tm is the number of leaf in tree fm
  • τ 表對 Tm 的 factor
  • γj,mm 步 XGB tree fmj 個 leaf node 的輸出值

Optimal Output of Leaf Node j

γj,m=iRj,mgiiRj,mhi+λ

Optimal Value of Objective Function

˜L(m)=12Tmj=1[(gi)2hi+λ]+τTm

Why does Approximation with Taylor Expansion Work?

Traditional Gradient Decent

當我們在做 gradient decent 時,我們是在嘗試 minimize a cost function f(x),然後讓 w(i) 往 negative gradient 的方向移動

w(i+1)=w(i)f(w(i))

但 gradient f(w(i)) 本身只告訴我們方向,不會告訴我們真正的 the minimum value,我們得到的是每步後盡量靠近 the minimum value 一點,這使的我們無法保證最終到達極值點

同樣的表達式下的 gradient boost machine 也相似的的問題

Fm(x)=Fm1(x)+νfm(x)

傳統的 GBDT 透過建一顆 regression tree fm(x) 和合適的 step size ν (learning rate) 擬合 negative gradient 往低谷靠近,可以說 GBDT tree fm(x) 本身就代表 loss function 的 negative graident 的方向

Better Gradient Decent: Newton’s Method

Newton’s method 是個改良自 gradient decent 的做法。他不只單純考慮了 gradient 方向 (即 first order derivative) ,還多考慮了 second order derivative 即所謂 gradient 的變化趨勢,這他更精準的定位極值的方向

w(i+1)=w(i)f(w(i))2f(w(i))=w(i)f(w(i))Hessf(w(i))

XGBoost 引入了 Newton’s method 思維,在建立子樹 fm(x) 不再單純只是 negatvie gradient 方向 (即 first order derivative) ,還多考慮了 second order derivative 即 gradient 的變化趨勢,這也是為什麼 XGBoost 全稱叫 Extreme Gradient Boosting

fm(xi)=γj,m=iRj,mgiiRj,mhi+λ
  • γj,mm 步 XGB tree fmj 個 leaf node 的輸出值
  • gi 為 first derivative of loss function related to data sample xi
  • hi 為 second derivative of loss function related to data sample xi

而 loss function l(yi,^yi) 會因不同應用: regression , classification, rank 而有不同的 objective function,並不一定存在 second order derivative,所以 XGBoost 利用 Taylor expansion 藉由 polynomial function 可以逼近任何 function 的特性,讓 loss function 在 fm(xi) 附近存在 second order derivative

l(y,ˆy(m1)+fm(x))l(y,Fm1(x))+gfm(x)+12hfm(x)2

可以說,XGBoos tree 以一種更聰明的方式往 the minimum 移動

總結

  • 一般 GBDT 用 regression tree 擬合 residuals,本質上是往 negative gradient 方向移動
  • XGBoost tree fm(x) 擬合 residuals,同時考慮 gradient 的方向和 gradient 變化趨勢,這讓他朝 optimal value 移動時顯得更加聰明有效
    • gradient 的方向: first order derivative
    • gradient 變化趨勢: second order derivative

Reference

  • Chen, T., & Guestrin, C. (2016). XGBoost: A scalable tree boosting system. Proceedings of the ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 13-17-Augu, 785–794. https://doi.org/10.1145/2939672.2939785

XGBoost Part 1 (of 4): Regression

XGBoost Part 3 (of 4): Mathematical Details


透視 XGBoost(2) 圖解 Classification

TL;DR

  • XGBoost 與 GBDT 相比,同樣都是擬合上一步 Fm1(x) 預測值的 residual 。不同的是 XGBoost 在擬合 residual 時更為聰明有效,他有自己衡量分裂增益 gain 的方式去擬合 residual 建立 XGB tree fm(x)
Gain=Leftsimilarity+RightsimilarityRootsimilarity
  • 分裂增益 gain ,的背後原理可以透過 XGBoost 通用 objective function 並填入 classification/regression/rank/etc.. 各自的 loss function 後推導出的 first derivative of loss function=giandsecond derivative of loss function=hi
  • XGBoost 的 通用 objective function 由兩部份組成
    • first part is loss function,根據不同任務有不同的 loss function
    • second part is regularization term 防止 overfitting,限制 leaf node 輸出值大小 和 leaf nodes 的總數

透視 XGBoost(1) 圖解 Regression

Introduction

XGBoost

XGBoost

XGboost 是 gradient boosting machine 的一種實作方式,xgboost 也是建一顆新樹 fm(x) 去擬合上一步模型輸出 Fm1(x)residual

Fm(x)=Fm1(x)+fm(x)Fm1(x)=F0(x)+m1i=0fm1(x)

不同的是,Xgboost 做了大量運算和擬合的優化,這讓他比起傳統 GBDT 更為高效率與有效


一步步透視 GBDT Classifier

TL;DR

  • 訓練完的 GBDT 是由多棵樹組成的 function set f1(x),,fM(x)
    • FM(x)=F0(x)+νMi=1fi(x)
  • 訓練中的 GBDT,每棵新樹 fm(x) 都去擬合 target yFm1(x)residual,也就是 gradient decent 的方向
    • Fm(x)=Fm1(x)+νfm(x)
  • GBDT classifier 常用的 loss function 為 cross entropy
  • classifier F(x) 輸出的是 log(odds),但衡量 residualprobability 有關,得將 F(x) 通過 sigmoid function  獲得 probability
    • p=σ(F(x))

GBDT 簡介在 一步步透視 GBDT Regression Tree

直接進入正題吧


一步步透視 GBDT Regression Tree

TL;DR

  • 訓練完的 GBDT 是由多棵樹組成的 function set f1(x),,fM(x)

    • FM(x)=F0(x)+νMi=1fi(x)
  • 訓練中的 GBDT,每棵新樹 fm(x) 都去擬合 target yFm1(x)residual,也就是 gradient decent 的方向

    • Fm(x)=Fm1(x)+νfm(x)

Golf Boosting

Golf Boosting