CMS 3D CMS Logo

GBRForest.h
Go to the documentation of this file.
1 #ifndef EGAMMAOBJECTS_GBRForest
2 #define EGAMMAOBJECTS_GBRForest
3 
5 // //
6 // GBRForest //
7 // //
8 // A fast minimal implementation of Gradient-Boosted Regression Trees //
9 // which has been especially optimized for size on disk and in memory. //
10 // //
11 // Designed to be built from TMVA-trained trees, but could also be //
12 // generalized to otherwise-trained trees, classification, //
13 // or other boosting methods in the future //
14 // //
15 // Josh Bendavid - MIT //
17 
20 
21 #include <cmath>
22 #include <vector>
23 
24 class GBRForest {
25 public:
26  GBRForest() {}
27 
28  double GetResponse(const float* vector) const;
29  double GetGradBoostClassifier(const float* vector) const;
30  double GetAdaBoostClassifier(const float* vector) const { return GetResponse(vector); }
31 
32  //for backwards-compatibility
33  double GetClassifier(const float* vector) const { return GetGradBoostClassifier(vector); }
34 
35  void SetInitialResponse(double response) { fInitialResponse = response; }
36 
37  std::vector<GBRTree>& Trees() { return fTrees; }
38  const std::vector<GBRTree>& Trees() const { return fTrees; }
39 
40 protected:
41  double fInitialResponse = 0.0;
42  std::vector<GBRTree> fTrees;
43 
45 };
46 
47 //_______________________________________________________________________
48 inline double GBRForest::GetResponse(const float* vector) const {
49  double response = fInitialResponse;
50  for (auto const& tree : fTrees) {
51  response += tree.GetResponse(vector);
52  }
53  return response;
54 }
55 
56 //_______________________________________________________________________
57 inline double GBRForest::GetGradBoostClassifier(const float* vector) const {
58  double response = GetResponse(vector);
59  return 2.0 / (1.0 + std::exp(-2.0 * response)) - 1; //MVA output between -1 and 1
60 }
61 
62 #endif
double fInitialResponse
Definition: GBRForest.h:41
double GetAdaBoostClassifier(const float *vector) const
Definition: GBRForest.h:30
std::vector< GBRTree > & Trees()
Definition: GBRForest.h:37
GBRForest()
Definition: GBRForest.h:26
void SetInitialResponse(double response)
Definition: GBRForest.h:35
const std::vector< GBRTree > & Trees() const
Definition: GBRForest.h:38
std::vector< GBRTree > fTrees
Definition: GBRForest.h:42
double GetClassifier(const float *vector) const
Definition: GBRForest.h:33
double GetGradBoostClassifier(const float *vector) const
Definition: GBRForest.h:57
#define COND_SERIALIZABLE
Definition: Serializable.h:39
double GetResponse(const float *vector) const
Definition: GBRForest.h:48
Definition: tree.py:1