透視 XGBoost(3) 蘋果樹下的 objective function
XGBoost General Objective Function
XGboost 是 gradient boosting machine 的一種實作方式,xgboost 也是建一顆新樹 fm(x)fm(x) 去擬合上一步模型輸出 Fm−1(x)Fm−1(x) 的 residualresidual
Fm(x)=Fm−1(x)+fm(x)Fm−1(x)=F0(x)+m−1∑i=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=[n∑il(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
- Ω(fm)=τTm+12λ‖γ‖2
在 第 m−1 步建完 tree fm−1時,total cost 為
L(m−1)=[n∑i=1l(yi,^yi(m−1))]+Ω(f(m−1))=[n∑i=1l(yi,F(m−1)(xi)]+Ω(f(m−1))- ^yi(m−1) 表 Fm−1(xi) 預測值
- fm−1 表 第 m−1 步建立的 XGB tree
進入第 m 步,我們建一顆新樹 fm(x) 進一步減少 total loss,使得 L(m)<L(m−1) , 則 m 步 cost function 為
L(m)=[n∑il(yi,Fm−1(xi)+fm(xi))]+Ω(f(m))<L(m−1)現在要找出新樹 fm(xi) 的每個 leaf node 輸出能使式 (5) L(m) 最小
γj,m=argminγ∑xi∈Rj,m[l(yi,Fm−1(xi)+γ)]+Ω(f(m))- j 表 leaf node index
- m 表第 m 步
- γj,m m 步 fm 第 j 個 leaf node 的輸出值
- Rjm 表 m 步 第 j 個 leaf node,所包含的 data sample x 集合
當 loss function l(y,ˆy) 為 square error 時,求解式 (6) 不難,但如果是 classification problem 時,就變得很棘手。
所以 GBDT 對 regression problem 與 classification problem 分別用了兩種不同方式求解極值,求出 leaf node 的輸出
- for regression :硬解 derivative ,因爲 MSE 的 derivative 簡單 一步步透視 GBDT Regression Tree
- for classification: use the Second Order Tayler Approximation 化簡 loss function 一步步透視 GBDT Classifier
Second Order Tayler Approximation 的步步逼近
XGBoost 直接以 Second Order Tayler Approximation 處理 classification 跟 regression 的 loss function part l(y,ˆy)
式(5) 的 loss function part 在 ˆym−1(xi) 附近展開:
l(y,ˆy(m−1)+fm(x))≈l(y,ˆy(m−1))+[dd ˆy(m−1) l(y,ˆy(m−1))]fm(x)+12[d2d (ˆy(m−1))2 l(y,ˆy(m−1))]fm(x)2=l(y,Fm−1(x))+[dd Fm−1 l(y,Fm−1)]fm(x)+12[d2d F2m−1l(y,Fm−1)]fm(x)2=l(y,Fm−1(x))+gfm(x)+12hfm(x)2- ˆy(m−1) 為 XGB 在 m−1 步的 prediction Fm−1(x)
- g=dd Fm−1 l(y,Fm−1)
- h=d2d2 Fm−1l(y,Fm−1)
將式 (7) 代入式(5):
L(m)≈ [n∑i(l(yi,ˆy(m−1)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(m−1)i)
˜L(m)=[n∑i(gifm(xi)+12hifm(xi)2)]+[τTm+12λT∑jγ2m,j]=Tm∑j=1[(∑i∈Rm,jgi)γj,m+12(∑i∈Rj,mhi+λ)γ2j,m]+τTm- Tm is the number of leaf in tree fm
- τ 表對 Tm 的 factor
- γj,m 表 m 步 XGB tree fm 第 j 個 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)=Tm∑j=1[(∑i∈Rm,jgi)+(∑i∈Rj,mhi)γj,m]=0對於已經固定結構了 tree fm(x),式 (10) leaf node j 的 output value γj,m 為
γ∗j,m=−∑i∈Rj,mgi∑i∈Rj,mhi+λ- gi 為 first derivative of loss function related to data sample xi : gi=dd Fm−1 l(yi,Fm−1(xi))
- hi 為 second derivative of loss function related to data sample xi: hi=d2d F2m−1l(yi,Fm−1(xi))
將 式(11) γ∗j,m 代回 式 (9),即 objective function 的極值
˜L(m)=Tm∑j=1[(∑i∈Rm,jgi)γj,m+12(∑i∈Rj,mhi+λ)γ2j,m]+τTm=Tm∑j=1[−(∑i∈Rj,mgi)∑i∈Rj,mgi∑i∈Rj,mhi+λ+12(∑i∈Rj,mhi+λ)(∑i∈Rj,mgi∑i∈Rj,mhi+λ)2]+τTm=Tm∑j=1[−(∑gi)2∑hi+λ+12(∑gi)2∑hi+λ]+τTm=−12Tm∑j=1[(∑gi)2∑hi+λ]+τTm總結
XGBoost 通用 Objective Function
˜L(m)=Tm∑j=1[(∑i∈Rm,jgi)γj,m+12(∑i∈Rj,mhi+λ)γ2j,m]+τTm- gi 為 first derivative of loss function related to data sample xi : gi=dd Fm−1 l(yi,Fm−1(xi))
- hi 為 second derivative of loss function related to data sample xi: hi=d2d F2m−1l(yi,Fm−1(xi))
- Tm is the number of leaf in tree fm
- τ 表對 Tm 的 factor
- γj,m 表 m 步 XGB tree fm 第 j 個 leaf node 的輸出值
Optimal Output of Leaf Node j
γ∗j,m=−∑i∈Rj,mgi∑i∈Rj,mhi+λOptimal Value of Objective Function
˜L∗(m)=12Tm∑j=1[(∑gi)2∑hi+λ]+τTmWhy 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)=Fm−1(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=−∑i∈Rj,mgi∑i∈Rj,mhi+λ- γj,m 表 m 步 XGB tree fm 第 j 個 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(m−1)+fm(x))≈l(y,Fm−1(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
- Tayler expansion
- XGBoost Loss function Approximation With Taylor Expansion https://stats.stackexchange.com/questions/202858/xgboost-loss-function-approximation-with-taylor-expansion
- https://en.wikipedia.org/wiki/Taylor_series#Approximation_and_convergence
- https://en.wikipedia.org/wiki/Taylor’s_theorem
透視 XGBoost(3) 蘋果樹下的 objective function
https://seed9d.github.io/XGBoost-General-Objective-Function/
Be the first person to leave a comment!