forked from tonylee19544/leaves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgensemble_io.go
383 lines (351 loc) · 11.8 KB
/
xgensemble_io.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package leaves
import (
"bufio"
"fmt"
"math"
"os"
"github.com/dmitryikh/leaves/internal/xgbin"
"github.com/dmitryikh/leaves/internal/xgjson"
"github.com/dmitryikh/leaves/transformation"
)
func xgSplitIndex(origNode *xgbin.Node) uint32 {
return origNode.SIndex & ((1 << 31) - 1)
}
func xgDefaultLeft(origNode *xgbin.Node) bool {
return (origNode.SIndex >> 31) != 0
}
func xgIsLeaf(origNode *xgbin.Node) bool {
return origNode.CLeft == -1
}
func xgTreeFromTreeModel(origTree *xgbin.TreeModel, numFeatures uint32) (lgTree, error) {
t := lgTree{}
if origTree.Param.NumFeature > int32(numFeatures) {
return t, fmt.Errorf(
"tree number of features %d, but header number of features %d",
origTree.Param.NumFeature,
numFeatures,
)
}
if origTree.Param.NumRoots != 1 {
return t, fmt.Errorf("support only trees with 1 root (got %d)", origTree.Param.NumRoots)
}
if origTree.Param.NumNodes == 0 {
return t, fmt.Errorf("tree with zero number of nodes")
}
numNodes := origTree.Param.NumNodes
// XGBoost doesn't support categorical features
t.nCategorical = 0
if numNodes == 1 {
// special case - constant value tree
t.leafValues = append(t.leafValues, float64(origTree.Nodes[0].Info))
return t, nil
}
createNode := func(origNode *xgbin.Node) (lgNode, error) {
node := lgNode{}
// count nan as missing value
// NOTE: this differs with XGBosst realization: could be a problem
missingType := uint8(missingNan)
defaultType := uint8(0)
if xgDefaultLeft(origNode) {
defaultType = defaultLeft
}
node = numericalNode(xgSplitIndex(origNode), missingType, float64(origNode.Info), defaultType)
if origNode.CLeft < 0 {
return node, fmt.Errorf("logic error: got origNode.CLeft < 0")
}
if origNode.CRight < 0 {
return node, fmt.Errorf("logic error: got origNode.CRight < 0")
}
if xgIsLeaf(&origTree.Nodes[origNode.CLeft]) {
node.Flags |= leftLeaf
node.Left = uint32(len(t.leafValues))
t.leafValues = append(t.leafValues, float64(origTree.Nodes[origNode.CLeft].Info))
}
if xgIsLeaf(&origTree.Nodes[origNode.CRight]) {
node.Flags |= rightLeaf
node.Right = uint32(len(t.leafValues))
t.leafValues = append(t.leafValues, float64(origTree.Nodes[origNode.CRight].Info))
}
return node, nil
}
origNodeIdxStack := make([]uint32, 0, numNodes)
convNodeIdxStack := make([]uint32, 0, numNodes)
visited := make([]bool, numNodes)
t.nodes = make([]lgNode, 0, numNodes)
node, err := createNode(&origTree.Nodes[0])
if err != nil {
return t, err
}
t.nodes = append(t.nodes, node)
origNodeIdxStack = append(origNodeIdxStack, 0)
convNodeIdxStack = append(convNodeIdxStack, 0)
for len(origNodeIdxStack) > 0 {
convIdx := convNodeIdxStack[len(convNodeIdxStack)-1]
if t.nodes[convIdx].Flags&rightLeaf == 0 {
origIdx := origTree.Nodes[origNodeIdxStack[len(origNodeIdxStack)-1]].CRight
if !visited[origIdx] {
node, err := createNode(&origTree.Nodes[origIdx])
if err != nil {
return t, err
}
t.nodes = append(t.nodes, node)
convNewIdx := len(t.nodes) - 1
convNodeIdxStack = append(convNodeIdxStack, uint32(convNewIdx))
origNodeIdxStack = append(origNodeIdxStack, uint32(origIdx))
visited[origIdx] = true
t.nodes[convIdx].Right = uint32(convNewIdx)
continue
}
}
if t.nodes[convIdx].Flags&leftLeaf == 0 {
origIdx := origTree.Nodes[origNodeIdxStack[len(origNodeIdxStack)-1]].CLeft
if !visited[origIdx] {
node, err := createNode(&origTree.Nodes[origIdx])
if err != nil {
return t, err
}
t.nodes = append(t.nodes, node)
convNewIdx := len(t.nodes) - 1
convNodeIdxStack = append(convNodeIdxStack, uint32(convNewIdx))
origNodeIdxStack = append(origNodeIdxStack, uint32(origIdx))
visited[origIdx] = true
t.nodes[convIdx].Left = uint32(convNewIdx)
continue
}
}
origNodeIdxStack = origNodeIdxStack[:len(origNodeIdxStack)-1]
convNodeIdxStack = convNodeIdxStack[:len(convNodeIdxStack)-1]
}
return t, nil
}
func readWeightDropFromReader(reader *bufio.Reader, numTrees int, modelName string) ([]float64, error) {
origModelWeightDrop := make([]float64, numTrees)
if err := checkModelName(modelName); err != nil {
return nil, err
}
if modelName == "dart" {
// read additional float32 slice of weighs of dropped trees. Only for 'dart' models
weightDrop, err := xgbin.ReadFloat32Slice(reader)
if err != nil {
return nil, err
}
if len(weightDrop) != numTrees {
return nil, fmt.Errorf(
"unexpected len(weightDrop) for 'dart' (got: %d, expected: %d)",
len(weightDrop),
numTrees,
)
}
for i, v := range weightDrop {
origModelWeightDrop[i] = float64(v)
}
} else if modelName == "gbtree" {
// use 1.0 as default. 1.0 scale will not break down anything
for i := 0; i < numTrees; i++ {
origModelWeightDrop[i] = 1.0
}
}
return origModelWeightDrop, nil
}
// XGEnsembleFromReader reads XGBoost model from `reader`. Works with 'gbtree' and 'dart' models
func XGEnsembleFromReader(reader *bufio.Reader, loadTransformation bool) (*Ensemble, error) {
e := &xgEnsemble{}
//to support version after 1.0.0
xgbin.ReadBinf(reader)
// reading header info
header, err := xgbin.ReadModelHeader(reader)
if err != nil {
return nil, err
}
// reading gbtree
origModel, err := xgbin.ReadGBTreeModel(reader)
if err != nil {
return nil, err
}
if e.name, err = createModelName(header.NameGbm); err != nil {
return nil, err
}
if e.MaxFeatureIdx, err = calculateMaxFeatureIdx(int(header.Param.NumFeatures)); err != nil {
return nil, err
}
//To support version before 1.0.0
if header.Param.MajorVersion > uint32(0) {
e.nRawOutputGroups = getNRawOutputGroups(header.Param.NumClass)
e.BaseScore = calculateBaseScoreFromLearnerParam(float64(header.Param.BaseScore))
} else {
e.nRawOutputGroups = getNRawOutputGroups(origModel.Param.DeprecatedNumOutputGroup)
e.BaseScore = float64(header.Param.BaseScore)
}
if origModel.Param.DeprecatedNumFeature > int32(header.Param.NumFeatures) {
return nil, fmt.Errorf(
"gbtee number of features %d, but header number of features %d",
origModel.Param.DeprecatedNumFeature,
header.Param.NumFeatures,
)
}
if e.WeightDrop, err = readWeightDropFromReader(reader, int(origModel.Param.NumTrees), header.NameGbm); err != nil {
return nil, err
}
// TODO: below is not true (see Agaricus test). Why?
// if header.GbTreeModelParam.NumClass != origModel.GbTreeModelParam.DeprecatedNumOutputGroup {
// return nil, fmt.Errorf("header number of class and model number of class should be the same (%d != %d)",
// header.GbTreeModelParam.NumClass, origModel.GbTreeModelParam.DeprecatedNumOutputGroup)
// }
if origModel.Param.DeprecatedNumRoots != 1 {
return nil, fmt.Errorf("support only trees with 1 root (got %d)", origModel.Param.DeprecatedNumRoots)
}
if len(origModel.TreeInfo) != int(origModel.Param.NumTrees) {
return nil, fmt.Errorf("TreeInfo size should be %d (got %d)",
int(origModel.Param.NumTrees),
len(origModel.TreeInfo))
}
if err = checkTreeInfo(origModel.TreeInfo, e.nRawOutputGroups); err != nil {
return nil, err
}
transform, err := createTransform(loadTransformation, e.nRawOutputGroups, header.NameObj)
if err != nil {
return nil, err
}
if e.Trees, err = createTrees(origModel.Param.NumTrees, header.Param.NumFeatures, origModel); err != nil {
return nil, err
}
return &Ensemble{e, transform}, nil
}
func checkTreeInfo(treeInfo []int32, nRawOutputGroups int) error {
// Check that TreeInfo has expected pattern (0 1 2 0 1 2...)
curID := 0
for i := 0; i < len(treeInfo); i++ {
if int(treeInfo[i]) != curID {
return fmt.Errorf("TreeInfo expected to have pattern [0 1 2 0 1 2...] (got %v)", treeInfo)
}
curID++
if curID >= nRawOutputGroups {
curID = 0
}
}
return nil
}
// XGEnsembleFromFile reads XGBoost model from binary file or json file. Works with 'gbtree' and 'dart' models
func XGEnsembleFromFile(filename string, loadTransformation bool) (*Ensemble, error) {
if ensemble, err := xgEnsembleFromJsonFile(filename, loadTransformation); err == nil {
return ensemble, nil
}
reader, err := os.Open(filename)
if err != nil {
return nil, err
}
defer reader.Close()
bufReader := bufio.NewReader(reader)
return XGEnsembleFromReader(bufReader, loadTransformation)
}
func xgEnsembleFromJsonFile(filename string, loadTransformation bool) (*Ensemble, error) {
gbTreeJson, err := xgjson.ReadGBTree(filename)
if err != nil {
return nil, err
}
e, err := createXGEnsembleFromGBTreeJson(gbTreeJson)
if err != nil {
return nil, err
}
transform, err := createTransform(loadTransformation, e.nRawOutputGroups, gbTreeJson.Learner.Objective.Name)
if err != nil {
return nil, err
}
return &Ensemble{e, transform}, nil
}
func createXGEnsembleFromGBTreeJson(gbTreeJson *xgjson.GBTreeJson) (*xgEnsemble, error) {
e := &xgEnsemble{}
var err error
e.nRawOutputGroups = getNRawOutputGroups(gbTreeJson.Learner.LearnerModelParam.NumClass)
e.BaseScore = calculateBaseScoreFromLearnerParam(float64(gbTreeJson.Learner.LearnerModelParam.BaseScore))
if e.MaxFeatureIdx, err = calculateMaxFeatureIdx(int(gbTreeJson.Learner.LearnerModelParam.NumFeatures)); err != nil {
return nil, err
}
if e.name, err = createModelName(gbTreeJson.Learner.GradientBooster.Name); err != nil {
return nil, err
}
e.WeightDrop, err = getWeightDrop(gbTreeJson)
if err != nil {
return nil, err
}
if err = checkTreeInfo(gbTreeJson.Learner.GradientBooster.Model.TreeInfo, e.nRawOutputGroups); err != nil {
return nil, err
}
e.Trees, err = createTrees(
gbTreeJson.Learner.GradientBooster.Model.GbTreeModelParam.NumTrees,
gbTreeJson.Learner.LearnerModelParam.NumFeatures,
gbTreeJson.Learner.GradientBooster.Model.ToBinGBTreeModel(),
)
if err != nil {
return nil, err
}
return e, nil
}
func checkModelName(name string) error {
switch name {
case "dart", "gbtree":
return nil
default:
return fmt.Errorf("only 'gbtree' or 'dart' is supported (got %s)", name)
}
}
func createModelName(name string) (string, error) {
if err := checkModelName(name); err != nil {
return "", err
}
return fmt.Sprintf("xgboost.%s", name), nil
}
func calculateMaxFeatureIdx(numFeatures int) (int, error) {
if maxFeatureIdx := numFeatures - 1; maxFeatureIdx < 0 {
return -1, fmt.Errorf("zero number of features")
} else {
return maxFeatureIdx, nil
}
}
func calculateBaseScoreFromLearnerParam(rawBaseScore float64) float64 {
return math.Log(rawBaseScore) - math.Log(1-rawBaseScore)
}
func getNRawOutputGroups(numClass int32) int {
nRawOutputGroups := 1
if numClass != 0 {
nRawOutputGroups = int(numClass)
}
return nRawOutputGroups
}
func getWeightDrop(gbTreeJson *xgjson.GBTreeJson) ([]float64, error) {
weightDrop := make([]float64, gbTreeJson.Learner.GradientBooster.Model.GbTreeModelParam.NumTrees)
if gbTreeJson.Learner.GradientBooster.Name == "dart" {
weightDrop = gbTreeJson.Learner.GradientBooster.WeightDrop
} else if gbTreeJson.Learner.GradientBooster.Name == "gbtree" {
for idx := range weightDrop {
weightDrop[idx] = 1.0
}
}
return weightDrop, nil
}
func createTrees(numTrees int32, numFeatures uint32, model *xgbin.GBTreeModel) ([]lgTree, error) {
if numTrees == 0 {
return nil, fmt.Errorf("no trees in model")
}
trees := make([]lgTree, 0, numTrees)
for i := int32(0); i < numTrees; i++ {
tree, err := xgTreeFromTreeModel(model.Trees[i], numFeatures)
if err != nil {
return nil, fmt.Errorf("error while reading %d tree: %s", i, err.Error())
}
trees = append(trees, tree)
}
return trees, nil
}
func createTransform(loadTransformation bool, nRawOutputGroups int, objectiveName string) (transformation.Transform, error) {
var transform transformation.Transform
transform = &transformation.TransformRaw{NumOutputGroups: nRawOutputGroups}
if loadTransformation {
if objectiveName == "binary:logistic" {
transform = &transformation.TransformLogistic{}
} else {
return nil, fmt.Errorf("unknown transformation function '%s'", objectiveName)
}
}
return transform, nil
}