-
Notifications
You must be signed in to change notification settings - Fork 73
/
leaves.go
307 lines (276 loc) · 9.46 KB
/
leaves.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
package leaves
import (
"fmt"
"math"
"runtime"
"sync"
"github.com/dmitryikh/leaves/transformation"
)
// BatchSize for parallel task
const BatchSize = 16
type ensembleBaseInterface interface {
NEstimators() int
NRawOutputGroups() int
NFeatures() int
NLeaves() []int
Name() string
adjustNEstimators(nEstimators int) int
predictInner(fvals []float64, nEstimators int, predictions []float64, startIndex int)
predictLeafIndicesInner(fvals []float64, nEstimators int, predictions []float64, startIndex int)
resetFVals(fvals []float64)
}
// Ensemble is a common wrapper for all models
type Ensemble struct {
ensembleBaseInterface
transform transformation.Transform
}
func (e *Ensemble) predictInnerAndTransform(fvals []float64, nEstimators int, predictions []float64, startIndex int) {
if e.Transformation().Type() == transformation.Raw {
e.predictInner(fvals, nEstimators, predictions, startIndex)
} else if e.Transformation().Type() == transformation.LeafIndex {
e.predictLeafIndicesInner(fvals, nEstimators, predictions, startIndex)
} else {
// TODO: avoid allocation here
rawPredictions := make([]float64, e.NRawOutputGroups())
e.predictInner(fvals, nEstimators, rawPredictions, 0)
e.transform.Transform(rawPredictions, predictions, startIndex)
}
}
func (e *Ensemble) checkNEstimators(nEstimators int) error {
if e.transform.Type() == transformation.LeafIndex && nEstimators != e.NEstimators() {
return fmt.Errorf("while predicting leaf indices all estimators should be used (provided num. of estimators = %d, should be %d)",
nEstimators, e.NEstimators())
}
return nil
}
// PredictSingle calculates prediction for single class model. If ensemble is
// multiclass, will return quitely 0.0. Only `nEstimators` first estimators
// (trees in most cases) will be used. If `len(fvals)` is not enough function
// will quietly return 0.0.
// NOTE: for multiclass or leaf indices predictions use Predict
func (e *Ensemble) PredictSingle(fvals []float64, nEstimators int) float64 {
if e.NOutputGroups() != 1 {
return 0.0
}
if e.NFeatures() > len(fvals) {
return 0.0
}
nEstimators = e.adjustNEstimators(nEstimators)
err := e.checkNEstimators(nEstimators)
if err != nil {
return 0.0
}
ret := [1]float64{0.0}
e.predictInnerAndTransform(fvals, nEstimators, ret[:], 0)
return ret[0]
}
// Predict calculates single prediction for one or multiclass ensembles. Only
// `nEstimators` first estimators (trees in most cases) will be used.
// NOTE: for single class predictions one can use simplified function PredictSingle
func (e *Ensemble) Predict(fvals []float64, nEstimators int, predictions []float64) error {
nRows := 1
if len(predictions) < e.NOutputGroups()*nRows {
return fmt.Errorf("predictions slice too short (should be at least %d)", e.NOutputGroups()*nRows)
}
if e.NFeatures() > len(fvals) {
return fmt.Errorf("incorrect number of features (%d)", len(fvals))
}
nEstimators = e.adjustNEstimators(nEstimators)
err := e.checkNEstimators(nEstimators)
if err != nil {
return err
}
e.predictInnerAndTransform(fvals, nEstimators, predictions, 0)
return nil
}
// PredictCSR calculates predictions from ensemble. `indptr`, `cols`, `vals`
// represent data structures from Compressed Sparse Row Matrix format (see
// CSRMat). Only `nEstimators` first estimators (trees) will be used.
// `nThreads` points to number of threads that will be utilized (maximum
// is GO_MAX_PROCS)
// Note, `predictions` slice should be properly allocated on call side
func (e *Ensemble) PredictCSR(indptr []int, cols []int, vals []float64, predictions []float64, nEstimators int, nThreads int) error {
nRows := len(indptr) - 1
if len(predictions) < e.NOutputGroups()*nRows {
return fmt.Errorf("predictions slice too short (should be at least %d)", e.NOutputGroups()*nRows)
}
nEstimators = e.adjustNEstimators(nEstimators)
err := e.checkNEstimators(nEstimators)
if err != nil {
return err
}
if nRows <= BatchSize || nThreads == 0 || nThreads == 1 {
// single thread calculations
fvals := make([]float64, e.NFeatures())
e.resetFVals(fvals)
e.predictCSRInner(indptr, cols, vals, 0, len(indptr)-1, predictions, nEstimators, fvals)
return nil
}
if nThreads > runtime.GOMAXPROCS(0) || nThreads < 1 {
nThreads = runtime.GOMAXPROCS(0)
}
nBatches := int(math.Ceil(float64(nRows) / BatchSize))
if nThreads > nBatches {
nThreads = nBatches
}
tasks := make(chan int)
wg := sync.WaitGroup{}
for i := 0; i < nThreads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
fvals := make([]float64, e.NFeatures())
e.resetFVals(fvals)
for startIndex := range tasks {
endIndex := startIndex + BatchSize
if endIndex > nRows {
endIndex = nRows
}
e.predictCSRInner(indptr, cols, vals, startIndex, endIndex, predictions, nEstimators, fvals)
}
}()
}
// feed the queue
for i := 0; i < nBatches; i++ {
tasks <- i * BatchSize
}
close(tasks)
wg.Wait()
return nil
}
func (e *Ensemble) predictCSRInner(
indptr []int,
cols []int,
vals []float64,
startIndex int,
endIndex int,
predictions []float64,
nEstimators int,
fvals []float64,
) {
for i := startIndex; i < endIndex; i++ {
start := indptr[i]
end := indptr[i+1]
for j := start; j < end; j++ {
if cols[j] < len(fvals) {
fvals[cols[j]] = vals[j]
}
}
e.predictInnerAndTransform(fvals, nEstimators, predictions, i*e.NOutputGroups())
e.resetFVals(fvals)
}
}
// PredictDense calculates predictions from ensemble. `vals`, `rows`, `cols`
// represent data structures from Rom Major Matrix format (see DenseMat). Only
// `nEstimators` first estimators (trees in most cases) will be used. `nThreads`
// points to number of threads that will be utilized (maximum is GO_MAX_PROCS)
// Note, `predictions` slice should be properly allocated on call side
func (e *Ensemble) PredictDense(
vals []float64,
nrows int,
ncols int,
predictions []float64,
nEstimators int,
nThreads int,
) error {
nRows := nrows
if len(predictions) < e.NOutputGroups()*nRows {
return fmt.Errorf("predictions slice too short (should be at least %d)", e.NOutputGroups()*nRows)
}
if ncols == 0 || e.NFeatures() > ncols {
return fmt.Errorf("incorrect number of columns")
}
nEstimators = e.adjustNEstimators(nEstimators)
err := e.checkNEstimators(nEstimators)
if err != nil {
return err
}
if nRows <= BatchSize || nThreads == 0 || nThreads == 1 {
// single thread calculations
for i := 0; i < nRows; i++ {
fvals := vals[i*ncols : (i+1)*ncols]
e.predictInnerAndTransform(fvals, nEstimators, predictions, i*e.NOutputGroups())
}
return nil
}
if nThreads > runtime.GOMAXPROCS(0) || nThreads < 1 {
nThreads = runtime.GOMAXPROCS(0)
}
nBatches := int(math.Ceil(float64(nRows) / BatchSize))
if nThreads > nBatches {
nThreads = nBatches
}
tasks := make(chan int)
wg := sync.WaitGroup{}
for i := 0; i < nThreads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for startIndex := range tasks {
endIndex := startIndex + BatchSize
if endIndex > nRows {
endIndex = nRows
}
for i := startIndex; i < endIndex; i++ {
e.predictInnerAndTransform(vals[i*int(ncols):(i+1)*int(ncols)], nEstimators, predictions, i*e.NOutputGroups())
}
}
}()
}
// feed the queue
for i := 0; i < nBatches; i++ {
tasks <- i * BatchSize
}
close(tasks)
wg.Wait()
return nil
}
// NEstimators returns number of estimators (trees) in ensemble (per group)
func (e *Ensemble) NEstimators() int {
return e.ensembleBaseInterface.NEstimators()
}
// NRawOutputGroups returns number of groups (numbers) in every object
// predictions before transformation function applied. This value is provided
// mainly for information purpose
func (e *Ensemble) NRawOutputGroups() int {
return e.ensembleBaseInterface.NRawOutputGroups()
}
// NOutputGroups returns number of groups (numbers) in every object predictions.
// For example binary logistic model will give 1, but 4-class prediction model
// will give 4 numbers per object. This value usually used to preallocate slice
// for prediction values
func (e *Ensemble) NOutputGroups() int {
return e.transform.NOutputGroups()
}
// NFeatures returns number of features in the model
func (e *Ensemble) NFeatures() int {
return e.ensembleBaseInterface.NFeatures()
}
// NLeaves returns number of leaves in each tree of the ensemble. Returned
// vector has size NRawOutputGroups() * NEstimators(). For example to get number
// of leaves in group groupID for estimator estimatorID:
// NLeaves()[groupID*NEstimators() + estimatorID].
// In case of NRawOutputGroups() == 1 (binary classification or regression):
// NLeaves()[estimatorID]
func (e *Ensemble) NLeaves() []int {
return e.ensembleBaseInterface.NLeaves()
}
// Name returns name of the estimator
func (e *Ensemble) Name() string {
return e.ensembleBaseInterface.Name()
}
// Transformation returns transformation objects which applied to model outputs.
func (e *Ensemble) Transformation() transformation.Transform {
return e.transform
}
// EnsembleWithRawPredictions returns ensemble instance with TransformRaw (no
// transformation functions will be applied to the model resulst)
func (e *Ensemble) EnsembleWithRawPredictions() *Ensemble {
return &Ensemble{e, &transformation.TransformRaw{e.NRawOutputGroups()}}
}
// EnsembleWithLeafPredictions returns ensemble instance with TransformLeafIndex
// (return trees indices instead of numerical values)
func (e *Ensemble) EnsembleWithLeafPredictions() *Ensemble {
// each predictions will produce NRawOutputGroups() * NEstimators() values
return &Ensemble{e, &transformation.TransformLeafIndex{e.NRawOutputGroups() * e.NEstimators()}}
}