-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgboost_optimized.go
182 lines (158 loc) · 4.23 KB
/
xgboost_optimized.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package arboreal
import math "github.com/chewxy/math32"
type OptimizedGBDTClassifier struct {
Model *GBTModelOptimized
NumClasses int
}
func NewOptimizedGBDTClassifierFromSchema(model *XGBoostSchema) OptimizedGBDTClassifier {
origModel := model.Learner.GradientBooster.(*GBTModelOptimized)
return OptimizedGBDTClassifier{
Model: origModel,
NumClasses: model.Learner.LearnerModelParam.NumClass,
}
}
func sigmoidOpt(x *[]float32) []float32 {
for i, v := range *x {
val := sigmoidSingleOpt(v)
(*x)[i] = val
}
return *x
}
func sigmoidSingleOpt(x float32) float32 {
return 1.0 / (1.0 + math.Exp(-x))
}
func (m *OptimizedGBDTClassifier) Predict(features SparseVector) ([]float32, error) {
numClasses := max(m.NumClasses, 1)
treesPerClass := len(m.Model.Trees) / numClasses
perClassScore := make([]float32, numClasses)
for i := 0; i < numClasses; i++ {
offset := i * treesPerClass
for j := 0; j < treesPerClass; j++ {
perClassScore[i] += m.Model.Trees[offset+j].Predict(features)
}
perClassScore[i] = sigmoidSingleOpt(perClassScore[i])
}
// TODO: handle objective
return perClassScore, nil
}
func (m *OptimizedGBDTClassifier) PredictFloats(features []float32) ([]float32, error) {
sv := make(SparseVector, len(features))
for i, v := range features {
sv[i] = v
}
numClasses := max(m.NumClasses, 1)
treesPerClass := len(m.Model.Trees) / numClasses
perClassScore := make([]float32, numClasses)
for i := 0; i < numClasses; i++ {
offset := (i * treesPerClass)
for j := 0; j < treesPerClass; j++ {
perClassScore[i] += m.Model.Trees[offset+j].Predict(sv)
}
perClassScore[i] = sigmoidSingleOpt(perClassScore[i])
}
// TODO: handle objective
return perClassScore, nil
}
type GBTModelOptimized struct {
Trees []*TreeOptimized `json:"trees"`
}
type TreeOptimized struct {
Nodes []*NodeOptimized
}
type NodeOptimized struct {
CategoricalSize int
Category int
CategoriesNode int
CategoriesSegment int
LeftChild int
RightChild int
SplitIndex int
SplitType int
SplitCondition float32
DefaultLeft bool
IsLeaf bool
}
func (m *GBTModelOptimized) GetName() string {
return "gbtree_optimized"
}
func (m *GBTModelOptimized) Predict(features SparseVector) ([]float32, error) {
result := make([]float32, len(m.Trees))
for idx, tree := range m.Trees {
res := tree.Predict(features)
result[idx] = res
}
return result, nil
}
func (t *TreeOptimized) predictCategorical(features SparseVector) float32 {
idx := 0
for {
node := t.Nodes[idx]
if node.IsLeaf {
// splitConditions[idx] is return value for a leaf node
return node.SplitCondition
}
leftChild := node.LeftChild
// We don't need to do the insane optimization here, as
// the optimized version already takes advantage of cache locality
// rightChild := leftChild + 1
rightChild := node.RightChild
splitCol := node.SplitIndex
// splitVal := node.SplitCondition
fval, ok := features[splitCol]
// missing value behavior is determined by default left
if !ok {
if node.DefaultLeft {
idx = leftChild
} else {
idx = rightChild
}
continue
}
// todo: doublecheck this
if int(fval) == node.Category {
idx = rightChild
} else {
idx = leftChild
}
}
}
func (t *TreeOptimized) predictNumerical(features SparseVector) float32 {
idx := 0
for {
node := t.Nodes[idx]
if node.IsLeaf {
// splitConditions[idx] is return value for a leaf node
return node.SplitCondition
}
leftChild := node.LeftChild
// We don't need to do the insane optimization here, as
// the optimized version already takes advantage of cache locality
rightChild := leftChild + 1
// rightChild := node.RightChild
splitCol := node.SplitIndex
splitVal := node.SplitCondition
fval, ok := features[splitCol]
// missing value behavior is determined by default left
if !ok {
if node.DefaultLeft {
idx = leftChild
} else {
idx = rightChild
}
continue
}
// xgboost uses <, lightgbm uses <=
if fval < splitVal {
idx = leftChild
} else {
idx = rightChild
}
}
}
func (t *TreeOptimized) Predict(features SparseVector) float32 {
if t.Nodes[0].SplitType == 1 {
return t.predictCategorical(features)
} else {
return t.predictNumerical(features)
}
}