CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
conifer::BDT< T, U, useAddTree > Class Template Reference

#include <conifer.h>

Public Member Functions

std::vector< double > _decision_function_double (std::vector< double > x) const
 
 BDT (std::string filename)
 
std::vector< U > decision_function (std::vector< T > x) const
 
 NLOHMANN_DEFINE_TYPE_INTRUSIVE (BDT, n_classes, n_trees, n_features, init_predict, trees)
 

Private Attributes

OpAdd< U > add
 
std::vector< double > init_predict
 
std::vector< U > init_predict_
 
unsigned int n_classes
 
unsigned int n_features
 
unsigned int n_trees
 
std::vector< std::vector< DecisionTree< T, U > > > trees
 

Detailed Description

template<class T, class U, bool useAddTree = false>
class conifer::BDT< T, U, useAddTree >

Definition at line 87 of file conifer.h.

Constructor & Destructor Documentation

◆ BDT()

template<class T, class U, bool useAddTree = false>
conifer::BDT< T, U, useAddTree >::BDT ( std::string  filename)
inline

Definition at line 102 of file conifer.h.

102  {
103  /* Construct the BDT from conifer cpp backend JSON file */
104  std::ifstream ifs(filename);
106  from_json(j, *this);
107  /* Do some transformation to initialise things into the proper emulation T, U types */
108  if (n_classes == 2)
109  n_classes = 1;
110  std::transform(init_predict.begin(), init_predict.end(), std::back_inserter(init_predict_), [](double ip) -> U {
111  return (U)ip;
112  });
113  for (unsigned int i = 0; i < n_trees; i++) {
114  for (unsigned int j = 0; j < n_classes; j++) {
115  trees.at(i).at(j).init_();
116  }
117  }
118  }
vector< string > parse(string line, const string &delimiter)
nlohmann::json json
std::vector< double > init_predict
Definition: conifer.h:92
unsigned int n_classes
Definition: conifer.h:89
unsigned int n_trees
Definition: conifer.h:90
std::vector< U > init_predict_
Definition: conifer.h:93
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
std::vector< std::vector< DecisionTree< T, U > > > trees
Definition: conifer.h:95
unsigned transform(const HcalDetId &id, unsigned transformCode)

Member Function Documentation

◆ _decision_function_double()

template<class T, class U, bool useAddTree = false>
std::vector<double> conifer::BDT< T, U, useAddTree >::_decision_function_double ( std::vector< double >  x) const
inline

Definition at line 152 of file conifer.h.

152  {
153  /* Do the prediction with data in/out as double, cast to T, U before prediction */
154  std::vector<T> xt;
155  std::transform(x.begin(), x.end(), std::back_inserter(xt), [](double xi) -> T { return (T)xi; });
156  std::vector<U> y = decision_function(xt);
157  std::vector<double> yd;
158  std::transform(y.begin(), y.end(), std::back_inserter(yd), [](U yi) -> double { return (double)yi; });
159  return yd;
160  }
std::vector< U > decision_function(std::vector< T > x) const
Definition: conifer.h:120
long double T
unsigned transform(const HcalDetId &id, unsigned transformCode)

◆ decision_function()

template<class T, class U, bool useAddTree = false>
std::vector<U> conifer::BDT< T, U, useAddTree >::decision_function ( std::vector< T x) const
inline

Definition at line 120 of file conifer.h.

Referenced by conifer::BDT< bdt_feature_t, ap_fixed< 12, 3, AP_RND_CONV, AP_SAT >, false >::_decision_function_double().

120  {
121  /* Do the prediction */
122 #ifdef CMSSW_GIT_HASH
123  if (x.size() != n_features) {
124  throw cms::Exception("RuntimeError")
125  << "Conifer : Size of feature vector mismatches expected n_features" << std::endl;
126  }
127 #else
128  if (x.size() != n_features) {
129  throw std::runtime_error("Conifer : Size of feature vector mismatches expected n_features");
130  }
131 #endif
132  std::vector<U> values;
133  std::vector<std::vector<U>> values_trees;
134  values_trees.resize(n_classes);
135  values.resize(n_classes, U(0));
136  for (unsigned int i = 0; i < n_classes; i++) {
137  std::transform(trees.begin(),
138  trees.end(),
139  std::back_inserter(values_trees.at(i)),
140  [&i, &x](std::vector<DecisionTree<T, U>> tree_v) { return tree_v.at(i).decision_function(x); });
141  if (useAddTree) {
142  values.at(i) = init_predict_.at(i);
143  values.at(i) += reduce<U, OpAdd<U>>(values_trees.at(i), add);
144  } else {
145  values.at(i) = std::accumulate(values_trees.at(i).begin(), values_trees.at(i).end(), U(init_predict_.at(i)));
146  }
147  }
148 
149  return values;
150  }
OpAdd< U > add
Definition: conifer.h:96
unsigned int n_classes
Definition: conifer.h:89
std::vector< U > init_predict_
Definition: conifer.h:93
unsigned int n_features
Definition: conifer.h:91
std::vector< std::vector< DecisionTree< T, U > > > trees
Definition: conifer.h:95
unsigned transform(const HcalDetId &id, unsigned transformCode)

◆ NLOHMANN_DEFINE_TYPE_INTRUSIVE()

template<class T, class U, bool useAddTree = false>
conifer::BDT< T, U, useAddTree >::NLOHMANN_DEFINE_TYPE_INTRUSIVE ( BDT< T, U, useAddTree >  ,
n_classes  ,
n_trees  ,
n_features  ,
init_predict  ,
trees   
)

Member Data Documentation

◆ add

template<class T, class U, bool useAddTree = false>
OpAdd<U> conifer::BDT< T, U, useAddTree >::add
private

◆ init_predict

template<class T, class U, bool useAddTree = false>
std::vector<double> conifer::BDT< T, U, useAddTree >::init_predict
private

◆ init_predict_

template<class T, class U, bool useAddTree = false>
std::vector<U> conifer::BDT< T, U, useAddTree >::init_predict_
private

◆ n_classes

template<class T, class U, bool useAddTree = false>
unsigned int conifer::BDT< T, U, useAddTree >::n_classes
private

◆ n_features

template<class T, class U, bool useAddTree = false>
unsigned int conifer::BDT< T, U, useAddTree >::n_features
private

◆ n_trees

template<class T, class U, bool useAddTree = false>
unsigned int conifer::BDT< T, U, useAddTree >::n_trees
private

◆ trees

template<class T, class U, bool useAddTree = false>
std::vector<std::vector<DecisionTree<T, U> > > conifer::BDT< T, U, useAddTree >::trees
private