ATLAS Offline Software
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
MVAUtils::ForestLGBMSimple Class Referencefinalabstract

Implement LGBM Forest without nan support. More...

#include <ForestLGBM.h>

Inheritance diagram for MVAUtils::ForestLGBMSimple:
Collaboration diagram for MVAUtils::ForestLGBMSimple:

Public Member Functions

 ForestLGBMSimple (TTree *tree)
 
 ForestLGBMSimple ()=default
 
 ForestLGBMSimple (const ForestLGBMSimple &)=default
 
ForestLGBMSimpleoperator= (const ForestLGBMSimple &)=default
 
 ForestLGBMSimple (ForestLGBMSimple &&)=default
 
ForestLGBMSimpleoperator= (ForestLGBMSimple &&)=default
 
 ~ForestLGBMSimple ()=default
 
virtual TTree * WriteTree (TString name) const override
 Return a TTree representing the BDT. More...
 
virtual void PrintForest () const override
 
virtual int GetNVars () const override
 Get the number of input variable to be passed with std::vector to Get* methods. More...
 
virtual float GetClassification (const std::vector< float > &values) const final
 Compute the prediction of a classification. More...
 
virtual float GetClassification (const std::vector< float * > &pointers) const final
 
virtual float GetTreeResponse (const std::vector< float > &values, unsigned int itree) const override final
 Return the response of one tree Must pass the features in a std::vector<float> values and the index of the tree. More...
 
virtual float GetTreeResponse (const std::vector< float * > &pointers, unsigned int itree) const override final
 
virtual float GetOffset () const override
 Return the offset of the forest. More...
 
virtual float GetOffset () const =0
 Return the global offset. More...
 
virtual float GetRawResponse (const std::vector< float > &values) const override final
 Return the response of the whole Forest. More...
 
virtual float GetRawResponse (const std::vector< float * > &pointers) const override final
 
virtual float GetResponse (const std::vector< float > &values) const override
 Compute the prediction for regression. More...
 
virtual float GetResponse (const std::vector< float * > &pointers) const override
 
virtual std::vector< float > GetMultiResponse (const std::vector< float > &values, unsigned int numClasses) const override
 Compute the prediction for multiclassification (a score for each class). More...
 
virtual std::vector< float > GetMultiResponse (const std::vector< float * > &pointers, unsigned int numClasses) const override
 
virtual unsigned int GetNTrees () const override final
 
virtual unsigned int GetNTrees () const =0
 
virtual void PrintForest () const override
 
virtual void PrintTree (unsigned int itree) const override
 
std::vector< NodeLGBMSimpleGetTree (unsigned int itree) const
 Return the vector of nodes for the tree itree. More...
 

Protected Member Functions

float GetTreeResponseFromNode (const std::vector< float > &values, index_t index) const
 Get the response of a tree. More...
 
float GetTreeResponseFromNode (const std::vector< float * > &pointers, index_t index) const
 
void newTree (const std::vector< NodeLGBMSimple > &nodes)
 append a new tree (defined by a vector of nodes serialized in preorder) to the forest More...
 

Private Attributes

int m_max_var =0
 
std::vector< index_tm_forest
 indices of the top-level nodes of each tree More...
 
std::vector< NodeLGBMSimplem_nodes
 where the nodes of the forest are stored More...
 

Detailed Description

Implement LGBM Forest without nan support.

Definition at line 42 of file ForestLGBM.h.

Constructor & Destructor Documentation

◆ ForestLGBMSimple() [1/4]

ForestLGBMSimple::ForestLGBMSimple ( TTree *  tree)
explicit

Definition at line 12 of file ForestLGBM.cxx.

14  , m_max_var(0)
15 {
16 
17 
18  // variables read from the TTree
19  std::vector<int> *vars = nullptr;
20  std::vector<float> *values = nullptr;
21 
22  std::vector<NodeLGBMSimple> nodes;
23 
24  tree->SetBranchAddress("vars", &vars);
25  tree->SetBranchAddress("values", &values);
26 
27  int numEntries = tree->GetEntries();
28  for (int entry = 0; entry < numEntries; ++entry)
29  {
30  // each entry in the TTree is a decision tree
31  tree->GetEntry(entry);
32  if (!vars) {
33  throw std::runtime_error(
34  "vars pointer is null in ForestLGBMSimple constructor");
35  }
36  if (!values) {
37  throw std::runtime_error(
38  "values pointers is null in ForestLGBMSimple constructor");
39  }
40  if (vars->size() != values->size()) {
41  throw std::runtime_error("inconsistent size for vars and values in "
42  "ForestLGBMSimple constructor");
43  }
44 
45  nodes.clear();
46 
47  std::vector<MVAUtils::index_t> right = detail::computeRight(*vars);
48 
49  for (size_t i = 0; i < vars->size(); ++i) {
50  nodes.emplace_back(vars->at(i), values->at(i), right[i]);
51  if (vars->at(i) > m_max_var) { m_max_var = vars->at(i); }
52  }
53  newTree(nodes);
54  } // end loop on TTree, all decision tree loaded
55  delete vars;
56  delete values;
57 }

◆ ForestLGBMSimple() [2/4]

MVAUtils::ForestLGBMSimple::ForestLGBMSimple ( )
default

◆ ForestLGBMSimple() [3/4]

MVAUtils::ForestLGBMSimple::ForestLGBMSimple ( const ForestLGBMSimple )
default

◆ ForestLGBMSimple() [4/4]

MVAUtils::ForestLGBMSimple::ForestLGBMSimple ( ForestLGBMSimple &&  )
default

◆ ~ForestLGBMSimple()

MVAUtils::ForestLGBMSimple::~ForestLGBMSimple ( )
default

Member Function Documentation

◆ GetClassification() [1/2]

virtual float MVAUtils::ForestLGBMBase< NodeLGBMSimple >::GetClassification ( const std::vector< float * > &  pointers) const
inlinefinalvirtualinherited

Implements MVAUtils::IForest.

Definition at line 35 of file ForestLGBM.h.

36  {
38  }

◆ GetClassification() [2/2]

virtual float MVAUtils::ForestLGBMBase< NodeLGBMSimple >::GetClassification ( const std::vector< float > &  values) const
inlinefinalvirtualinherited

Compute the prediction of a classification.

Implements MVAUtils::IForest.

Definition at line 31 of file ForestLGBM.h.

32  {
34  }

◆ GetMultiResponse() [1/2]

virtual std::vector<float> MVAUtils::Forest< NodeLGBMSimple >::GetMultiResponse ( const std::vector< float * > &  pointers,
unsigned int  numClasses 
) const
overridevirtualinherited

Implements MVAUtils::IForest.

◆ GetMultiResponse() [2/2]

virtual std::vector<float> MVAUtils::Forest< NodeLGBMSimple >::GetMultiResponse ( const std::vector< float > &  values,
unsigned int  numClasses 
) const
overridevirtualinherited

Compute the prediction for multiclassification (a score for each class).

In addition to the input values need to pass the number of classes

Implements MVAUtils::IForest.

◆ GetNTrees() [1/2]

virtual unsigned int MVAUtils::IForest::GetNTrees ( ) const
pure virtualinherited

Implemented in MVAUtils::Forest< Node_t >.

◆ GetNTrees() [2/2]

virtual unsigned int MVAUtils::Forest< NodeLGBMSimple >::GetNTrees
inlinefinaloverridevirtualinherited

Definition at line 94 of file Forest.h.

95  {
96  return m_forest.size();
97  }

◆ GetNVars()

virtual int MVAUtils::ForestLGBMSimple::GetNVars ( ) const
inlineoverridevirtual

Get the number of input variable to be passed with std::vector to Get* methods.

Implements MVAUtils::IForest.

Definition at line 54 of file ForestLGBM.h.

54 { return m_max_var + 1; }

◆ GetOffset() [1/2]

virtual float MVAUtils::IForest::GetOffset ( ) const
pure virtualinherited

Return the global offset.

For many implementation this is just 0

Implemented in MVAUtils::Forest< Node_t >, and MVAUtils::ForestWeighted< Node_t >.

◆ GetOffset() [2/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetOffset
inlineoverridevirtualinherited

Return the offset of the forest.

Since by default there is no offset, return 0

Definition at line 63 of file Forest.h.

63 { return 0.; }

◆ GetRawResponse() [1/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetRawResponse ( const std::vector< float * > &  pointers) const
finaloverridevirtualinherited

Implements MVAUtils::IForest.

◆ GetRawResponse() [2/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetRawResponse ( const std::vector< float > &  values) const
finaloverridevirtualinherited

Return the response of the whole Forest.

Raw is just the sum of all the trees

Implements MVAUtils::IForest.

◆ GetResponse() [1/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetResponse ( const std::vector< float * > &  pointers) const
overridevirtualinherited

Implements MVAUtils::IForest.

◆ GetResponse() [2/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetResponse ( const std::vector< float > &  values) const
overridevirtualinherited

Compute the prediction for regression.

Implements MVAUtils::IForest.

◆ GetTree()

std::vector<NodeLGBMSimple > MVAUtils::Forest< NodeLGBMSimple >::GetTree ( unsigned int  itree) const
inherited

Return the vector of nodes for the tree itree.

◆ GetTreeResponse() [1/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetTreeResponse ( const std::vector< float * > &  pointers,
unsigned int  itree 
) const
finaloverridevirtualinherited

Implements MVAUtils::IForest.

◆ GetTreeResponse() [2/2]

virtual float MVAUtils::Forest< NodeLGBMSimple >::GetTreeResponse ( const std::vector< float > &  values,
unsigned int  itree 
) const
finaloverridevirtualinherited

Return the response of one tree Must pass the features in a std::vector<float> values and the index of the tree.

Implements MVAUtils::IForest.

◆ GetTreeResponseFromNode() [1/2]

float MVAUtils::Forest< NodeLGBMSimple >::GetTreeResponseFromNode ( const std::vector< float * > &  pointers,
index_t  index 
) const
protectedinherited

◆ GetTreeResponseFromNode() [2/2]

float MVAUtils::Forest< NodeLGBMSimple >::GetTreeResponseFromNode ( const std::vector< float > &  values,
index_t  index 
) const
protectedinherited

Get the response of a tree.

Instead of specifying the index of the tree (as in GetTreeResponse) the index of the top node of the tree should be specified

◆ newTree()

void MVAUtils::Forest< NodeLGBMSimple >::newTree ( const std::vector< NodeLGBMSimple > &  nodes)
protectedinherited

append a new tree (defined by a vector of nodes serialized in preorder) to the forest

◆ operator=() [1/2]

ForestLGBMSimple& MVAUtils::ForestLGBMSimple::operator= ( const ForestLGBMSimple )
default

◆ operator=() [2/2]

ForestLGBMSimple& MVAUtils::ForestLGBMSimple::operator= ( ForestLGBMSimple &&  )
default

◆ PrintForest() [1/2]

void ForestLGBMSimple::PrintForest ( ) const
overridevirtual

Implements MVAUtils::IForest.

Definition at line 82 of file ForestLGBM.cxx.

83 {
84  std::cout << "***BDT LGBMSimple: Printing entire forest***" << std::endl;
86 }

◆ PrintForest() [2/2]

virtual void MVAUtils::Forest< NodeLGBMSimple >::PrintForest
overridevirtualinherited

◆ PrintTree()

virtual void MVAUtils::Forest< NodeLGBMSimple >::PrintTree ( unsigned int  itree) const
overridevirtualinherited

Implements MVAUtils::IForest.

◆ WriteTree()

TTree * ForestLGBMSimple::WriteTree ( TString  ) const
overridevirtual

Return a TTree representing the BDT.

The called is the owner of the returned TTree

Implements MVAUtils::IForest.

Definition at line 60 of file ForestLGBM.cxx.

61 {
62  TTree *tree = new TTree(name.Data(), "creator=lgbm;node_type=lgbm_simple");
63 
64  std::vector<int> vars;
65  std::vector<float> values;
66 
67  tree->Branch("vars", &vars);
68  tree->Branch("values", &values);
69 
70  for (size_t itree = 0; itree < GetNTrees(); ++itree) {
71  vars.clear();
72  values.clear();
73  for(const auto& node : GetTree(itree)) {
74  vars.push_back(node.GetVar());
75  values.push_back(node.GetVal());
76  }
77  tree->Fill();
78  }
79  return tree;
80 }

Member Data Documentation

◆ m_forest

std::vector<index_t> MVAUtils::Forest< NodeLGBMSimple >::m_forest
privateinherited

indices of the top-level nodes of each tree

Definition at line 117 of file Forest.h.

◆ m_max_var

int MVAUtils::ForestLGBMSimple::m_max_var =0
private

Definition at line 56 of file ForestLGBM.h.

◆ m_nodes

std::vector<NodeLGBMSimple > MVAUtils::Forest< NodeLGBMSimple >::m_nodes
privateinherited

where the nodes of the forest are stored

Definition at line 118 of file Forest.h.


The documentation for this class was generated from the following files:
sigmoid
Double_t sigmoid(Double_t x)
Definition: errors/trainNN.cxx:34
MVAUtils::ForestLGBMSimple::m_max_var
int m_max_var
Definition: ForestLGBM.h:56
MVAUtils::Forest< NodeLGBMSimple >::GetResponse
virtual float GetResponse(const std::vector< float > &values) const override
Compute the prediction for regression.
MVAUtils::ForestLGBMBase< NodeLGBMSimple >
tree
TChain * tree
Definition: tile_monitor.h:30
MVAUtils::Forest< NodeLGBMSimple >::GetTree
std::vector< NodeLGBMSimple > GetTree(unsigned int itree) const
Return the vector of nodes for the tree itree.
MVAUtils::detail::computeRight
std::vector< index_t > computeRight(const std::vector< int > &vars)
Compute the offsets between the nodes to their right children from a serialized representation of the...
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
MVAUtils::Forest< NodeLGBMSimple >::GetNTrees
virtual unsigned int GetNTrees() const override final
Definition: Forest.h:94
MVAUtils::Forest< NodeLGBMSimple >::m_forest
std::vector< index_t > m_forest
indices of the top-level nodes of each tree
Definition: Forest.h:117
lumiFormat.i
int i
Definition: lumiFormat.py:85
MVAUtils::Forest< NodeLGBMSimple >::newTree
void newTree(const std::vector< NodeLGBMSimple > &nodes)
append a new tree (defined by a vector of nodes serialized in preorder) to the forest
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
MVAUtils::Forest::PrintForest
virtual void PrintForest() const override
pointers
std::vector< T * > pointers(std::vector< T > &v)
Definition: rmain.cxx:366
node
Definition: memory_hooks-stdcmalloc.h:74