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
|
#ifndef EGAMMAOBJECTS_GBRTree
#define EGAMMAOBJECTS_GBRTree
//////////////////////////////////////////////////////////////////////////
// //
// GBRForest //
// //
// A fast minimal implementation of Gradient-Boosted Regression Trees //
// which has been especially optimized for size on disk and in memory. //
// //
// Designed to be built from TMVA-trained trees, but could also be //
// generalized to otherwise-trained trees, classification, //
// or other boosting methods in the future //
// //
// Josh Bendavid - MIT //
//////////////////////////////////////////////////////////////////////////
// The decision tree is implemented here as a set of two arrays, one for
// intermediate nodes, containing the variable index and cut value, as well
// as the indices of the 'left' and 'right' daughter nodes. Positive indices
// indicate further intermediate nodes, whereas negative indices indicate
// terminal nodes, which are stored simply as a vector of regression responses
#include "CondFormats/Serialization/interface/Serializable.h"
#include <vector>
class GBRTree {
public:
GBRTree() {}
explicit GBRTree(int nIntermediate, int nTerminal);
double GetResponse(const float *vector) const;
std::vector<float> &Responses() { return fResponses; }
const std::vector<float> &Responses() const { return fResponses; }
std::vector<unsigned char> &CutIndices() { return fCutIndices; }
const std::vector<unsigned char> &CutIndices() const { return fCutIndices; }
std::vector<float> &CutVals() { return fCutVals; }
const std::vector<float> &CutVals() const { return fCutVals; }
std::vector<int> &LeftIndices() { return fLeftIndices; }
const std::vector<int> &LeftIndices() const { return fLeftIndices; }
std::vector<int> &RightIndices() { return fRightIndices; }
const std::vector<int> &RightIndices() const { return fRightIndices; }
protected:
std::vector<unsigned char> fCutIndices;
std::vector<float> fCutVals;
std::vector<int> fLeftIndices;
std::vector<int> fRightIndices;
std::vector<float> fResponses;
COND_SERIALIZABLE;
};
//_______________________________________________________________________
inline double GBRTree::GetResponse(const float *vector) const {
int index = 0;
do {
auto r = fRightIndices[index];
auto l = fLeftIndices[index];
unsigned int x = vector[fCutIndices[index]] > fCutVals[index] ? ~0 : 0;
index = (x & r) | ((~x) & l);
} while (index > 0);
return fResponses[-index];
}
#endif
|