Logo ROOT  
Reference Guide
TMultiLayerPerceptron.h
Go to the documentation of this file.
1 // @(#)root/mlp:$Id$
2 // Author: Christophe.Delaere@cern.ch 20/07/03
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TMultiLayerPerceptron
13 #define ROOT_TMultiLayerPerceptron
14 
15 #include "TObject.h"
16 #include "TString.h"
17 #include "TObjArray.h"
18 #include "TMatrixD.h"
19 #include "TNeuron.h"
20 
21 class TTree;
22 class TEventList;
23 class TTreeFormula;
25 
27  friend class TMLPAnalyzer;
28 
29  public:
34  TMultiLayerPerceptron(const char* layout, TTree* data = 0,
35  const char* training = "Entry$%2==0",
36  const char* test = "",
38  const char* extF = "", const char* extD = "");
39  TMultiLayerPerceptron(const char* layout,
40  const char* weight, TTree* data = 0,
41  const char* training = "Entry$%2==0",
42  const char* test = "",
44  const char* extF = "", const char* extD = "");
45  TMultiLayerPerceptron(const char* layout, TTree* data,
46  TEventList* training,
49  const char* extF = "", const char* extD = "");
50  TMultiLayerPerceptron(const char* layout,
51  const char* weight, TTree* data,
52  TEventList* training,
55  const char* extF = "", const char* extD = "");
56  virtual ~TMultiLayerPerceptron();
57  void SetData(TTree*);
58  void SetTrainingDataSet(TEventList* train);
60  void SetTrainingDataSet(const char* train);
61  void SetTestDataSet(const char* test);
63  void SetEventWeight(const char*);
64  void Train(Int_t nEpoch, Option_t* option = "text", Double_t minE=0);
65  Double_t Result(Int_t event, Int_t index = 0) const;
66  Double_t GetError(Int_t event) const;
68  void ComputeDEDw() const;
69  void Randomize() const;
70  void SetEta(Double_t eta);
71  void SetEpsilon(Double_t eps);
72  void SetDelta(Double_t delta);
73  void SetEtaDecay(Double_t ed);
74  void SetTau(Double_t tau);
75  void SetReset(Int_t reset);
76  inline Double_t GetEta() const { return fEta; }
77  inline Double_t GetEpsilon() const { return fEpsilon; }
78  inline Double_t GetDelta() const { return fDelta; }
79  inline Double_t GetEtaDecay() const { return fEtaDecay; }
81  inline Double_t GetTau() const { return fTau; }
82  inline Int_t GetReset() const { return fReset; }
83  inline TString GetStructure() const { return fStructure; }
84  inline TNeuron::ENeuronType GetType() const { return fType; }
85  void DrawResult(Int_t index = 0, Option_t* option = "test") const;
86  Bool_t DumpWeights(Option_t* filename = "-") const;
87  Bool_t LoadWeights(Option_t* filename = "");
88  Double_t Evaluate(Int_t index, Double_t* params) const;
89  void Export(Option_t* filename = "NNfunction", Option_t* language = "C++") const;
90  virtual void Draw(Option_t *option="");
91 
92  protected:
93  void AttachData();
94  void BuildNetwork();
95  void GetEntry(Int_t) const;
96  // it's a choice not to force learning function being const, even if possible
97  void MLP_Stochastic(Double_t*);
98  void MLP_Batch(Double_t*);
100  void SteepestDir(Double_t*);
103  bool GetBFGSH(TMatrixD&, TMatrixD &, TMatrixD&);
104  void BFGSDir(TMatrixD&, Double_t*);
107  Double_t GetCrossEntropy() const;
108  Double_t GetSumSquareError() const;
109 
110  private:
111  TMultiLayerPerceptron(const TMultiLayerPerceptron&); // Not implemented
113  void ExpandStructure();
114  void BuildFirstLayer(TString&);
115  void BuildHiddenLayers(TString&);
116  void BuildOneHiddenLayer(const TString& sNumNodes, Int_t& layer,
117  Int_t& prevStart, Int_t& prevStop,
118  Bool_t lastLayer);
119  void BuildLastLayer(TString&, Int_t);
120  void Shuffle(Int_t*, Int_t) const;
122 
123  TTree* fData; ///<! pointer to the tree used as datasource
124  Int_t fCurrentTree; ///<! index of the current tree in a chain
125  Double_t fCurrentTreeWeight; ///<! weight of the current tree in a chain
126  TObjArray fNetwork; ///< Collection of all the neurons in the network
127  TObjArray fFirstLayer; ///< Collection of the input neurons; subset of fNetwork
128  TObjArray fLastLayer; ///< Collection of the output neurons; subset of fNetwork
129  TObjArray fSynapses; ///< Collection of all the synapses in the network
130  TString fStructure; ///< String containing the network structure
131  TString fWeight; ///< String containing the event weight
132  TNeuron::ENeuronType fType; ///< Type of hidden neurons
133  TNeuron::ENeuronType fOutType; ///< Type of output neurons
134  TString fextF; ///< String containing the function name
135  TString fextD; ///< String containing the derivative name
136  TEventList *fTraining; ///<! EventList defining the events in the training dataset
137  TEventList *fTest; ///<! EventList defining the events in the test dataset
138  ELearningMethod fLearningMethod; ///<! The Learning Method
139  TTreeFormula* fEventWeight; ///<! formula representing the event weight
140  TTreeFormulaManager* fManager; ///<! TTreeFormulaManager for the weight and neurons
141  Double_t fEta; ///<! Eta - used in stochastic minimisation - Default=0.1
142  Double_t fEpsilon; ///<! Epsilon - used in stochastic minimisation - Default=0.
143  Double_t fDelta; ///<! Delta - used in stochastic minimisation - Default=0.
144  Double_t fEtaDecay; ///<! EtaDecay - Eta *= EtaDecay at each epoch - Default=1.
145  Double_t fTau; ///<! Tau - used in line search - Default=3.
146  Double_t fLastAlpha; ///<! internal parameter used in line search
147  Int_t fReset; ///<! number of epochs between two resets of the search direction to the steepest descent - Default=50
148  Bool_t fTrainingOwner; ///<! internal flag whether one has to delete fTraining or not
149  Bool_t fTestOwner; ///<! internal flag whether one has to delete fTest or not
150 
151  ClassDef(TMultiLayerPerceptron, 4) // a Neural Network
152 };
153 
154 #endif
TMultiLayerPerceptron::fLastAlpha
Double_t fLastAlpha
! internal parameter used in line search
Definition: TMultiLayerPerceptron.h:146
TMultiLayerPerceptron::SetLearningMethod
void SetLearningMethod(TMultiLayerPerceptron::ELearningMethod method)
Sets the learning method.
Definition: TMultiLayerPerceptron.cxx:672
TMultiLayerPerceptron::GetEtaDecay
Double_t GetEtaDecay() const
Definition: TMultiLayerPerceptron.h:79
TMultiLayerPerceptron::EDataSet
EDataSet
Definition: TMultiLayerPerceptron.h:32
TMultiLayerPerceptron::fNetwork
TObjArray fNetwork
Collection of all the neurons in the network.
Definition: TMultiLayerPerceptron.h:126
TMultiLayerPerceptron::BuildFirstLayer
void BuildFirstLayer(TString &)
Instantiates the neurons in input Inputs are normalised and the type is set to kOff (simple forward o...
Definition: TMultiLayerPerceptron.cxx:1383
TMultiLayerPerceptron::GetStructure
TString GetStructure() const
Definition: TMultiLayerPerceptron.h:83
TMultiLayerPerceptron::LineSearch
Bool_t LineSearch(Double_t *, Double_t *)
Search along the line defined by direction.
Definition: TMultiLayerPerceptron.cxx:2256
TMultiLayerPerceptron::fTau
Double_t fTau
! Tau - used in line search - Default=3.
Definition: TMultiLayerPerceptron.h:145
TMultiLayerPerceptron::GetSumSquareError
Double_t GetSumSquareError() const
Error on the output for a given event.
Definition: TMultiLayerPerceptron.cxx:1080
TEventList
A TEventList object is a list of selected events (entries) in a TTree.
Definition: TEventList.h:31
TMultiLayerPerceptron::LoadWeights
Bool_t LoadWeights(Option_t *filename="")
Loads the weights from a text file conforming to the format defined by DumpWeights.
Definition: TMultiLayerPerceptron.cxx:1639
TObjArray
An array of TObjects.
Definition: TObjArray.h:37
TMultiLayerPerceptron
This class describes a neural network.
Definition: TMultiLayerPerceptron.h:26
TMultiLayerPerceptron::SetEtaDecay
void SetEtaDecay(Double_t ed)
Sets EtaDecay - Eta *= EtaDecay at each epoch (look at the constructor for the complete description o...
Definition: TMultiLayerPerceptron.cxx:712
Option_t
const char Option_t
Definition: RtypesCore.h:66
TMultiLayerPerceptron::fStructure
TString fStructure
String containing the network structure.
Definition: TMultiLayerPerceptron.h:130
TMultiLayerPerceptron::fextF
TString fextF
String containing the function name.
Definition: TMultiLayerPerceptron.h:134
TMultiLayerPerceptron::TMultiLayerPerceptron
TMultiLayerPerceptron()
Default constructor.
Definition: TMultiLayerPerceptron.cxx:264
TMultiLayerPerceptron::Randomize
void Randomize() const
Randomize the weights.
Definition: TMultiLayerPerceptron.cxx:1221
TMultiLayerPerceptron::Evaluate
Double_t Evaluate(Int_t index, Double_t *params) const
Returns the Neural Net for a given set of input parameters #parameters must equal #input neurons.
Definition: TMultiLayerPerceptron.cxx:1695
TMultiLayerPerceptron::kTest
@ kTest
Definition: TMultiLayerPerceptron.h:32
TMultiLayerPerceptron::kStochastic
@ kStochastic
Definition: TMultiLayerPerceptron.h:30
TMultiLayerPerceptron::fOutType
TNeuron::ENeuronType fOutType
Type of output neurons.
Definition: TMultiLayerPerceptron.h:133
TMultiLayerPerceptron::fSynapses
TObjArray fSynapses
Collection of all the synapses in the network.
Definition: TMultiLayerPerceptron.h:129
TMultiLayerPerceptron::fManager
TTreeFormulaManager * fManager
! TTreeFormulaManager for the weight and neurons
Definition: TMultiLayerPerceptron.h:140
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
TMultiLayerPerceptron::GetReset
Int_t GetReset() const
Definition: TMultiLayerPerceptron.h:82
TMultiLayerPerceptron::Export
void Export(Option_t *filename="NNfunction", Option_t *language="C++") const
Exports the NN as a function for any non-ROOT-dependant code Supported languages are: only C++ ,...
Definition: TMultiLayerPerceptron.cxx:1720
TMultiLayerPerceptron::AttachData
void AttachData()
Connects the TTree to Neurons in input and output layers.
Definition: TMultiLayerPerceptron.cxx:1248
TMultiLayerPerceptron::fType
TNeuron::ENeuronType fType
Type of hidden neurons.
Definition: TMultiLayerPerceptron.h:132
Int_t
int Int_t
Definition: RtypesCore.h:45
TMultiLayerPerceptron::fFirstLayer
TObjArray fFirstLayer
Collection of the input neurons; subset of fNetwork.
Definition: TMultiLayerPerceptron.h:127
TMultiLayerPerceptron::kBatch
@ kBatch
Definition: TMultiLayerPerceptron.h:30
TMultiLayerPerceptron::DrawResult
void DrawResult(Int_t index=0, Option_t *option="test") const
Draws the neural net output It produces an histogram with the output for the two datasets.
Definition: TMultiLayerPerceptron.cxx:1515
TMultiLayerPerceptron::ComputeDEDw
void ComputeDEDw() const
Compute the DEDw = sum on all training events of dedw for each weight normalized by the number of eve...
Definition: TMultiLayerPerceptron.cxx:1145
TMultiLayerPerceptron::DerivDir
Double_t DerivDir(Double_t *)
scalar product between gradient and direction = derivative along direction
Definition: TMultiLayerPerceptron.cxx:2452
TMultiLayerPerceptron::SetReset
void SetReset(Int_t reset)
Sets number of epochs between two resets of the search direction to the steepest descent.
Definition: TMultiLayerPerceptron.cxx:733
TMultiLayerPerceptron::GetEta
Double_t GetEta() const
Definition: TMultiLayerPerceptron.h:76
TNeuron::kSigmoid
@ kSigmoid
Definition: TNeuron.h:29
TString
Basic string class.
Definition: TString.h:136
TMultiLayerPerceptron::GetCrossEntropyBinary
Double_t GetCrossEntropyBinary() const
Cross entropy error for sigmoid output neurons, for a given event.
Definition: TMultiLayerPerceptron.cxx:1093
TMatrixT< Double_t >
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TMultiLayerPerceptron::DumpWeights
Bool_t DumpWeights(Option_t *filename="-") const
Dumps the weights to a text file.
Definition: TMultiLayerPerceptron.cxx:1589
TString.h
test
Definition: test.py:1
TMultiLayerPerceptron::kFletcherReeves
@ kFletcherReeves
Definition: TMultiLayerPerceptron.h:31
bool
TMultiLayerPerceptron::ExpandStructure
void ExpandStructure()
Expand the structure of the first layer.
Definition: TMultiLayerPerceptron.cxx:1306
TNeuron::ENeuronType
ENeuronType
Definition: TNeuron.h:29
TMultiLayerPerceptron::MLP_Batch
void MLP_Batch(Double_t *)
One step for the batch (stochastic) method.
Definition: TMultiLayerPerceptron.cxx:2185
TMultiLayerPerceptron::fTest
TEventList * fTest
! EventList defining the events in the test dataset
Definition: TMultiLayerPerceptron.h:137
TMultiLayerPerceptron::MLP_Stochastic
void MLP_Stochastic(Double_t *)
One step for the stochastic method buffer should contain the previous dw vector and will be updated.
Definition: TMultiLayerPerceptron.cxx:2140
TMultiLayerPerceptron::BuildHiddenLayers
void BuildHiddenLayers(TString &)
Builds hidden layers.
Definition: TMultiLayerPerceptron.cxx:1401
TMultiLayerPerceptron::SetEta
void SetEta(Double_t eta)
Sets Eta - used in stochastic minimisation (look at the constructor for the complete description of l...
Definition: TMultiLayerPerceptron.cxx:682
TMultiLayerPerceptron::fTestOwner
Bool_t fTestOwner
! internal flag whether one has to delete fTest or not
Definition: TMultiLayerPerceptron.h:149
TMLPAnalyzer
This utility class contains a set of tests usefull when developing a neural network.
Definition: TMLPAnalyzer.h:25
TMultiLayerPerceptron::GetType
TNeuron::ENeuronType GetType() const
Definition: TMultiLayerPerceptron.h:84
TMultiLayerPerceptron::GetError
Double_t GetError(Int_t event) const
Error on the output for a given event.
Definition: TMultiLayerPerceptron.cxx:1028
TMultiLayerPerceptron::fCurrentTreeWeight
Double_t fCurrentTreeWeight
! weight of the current tree in a chain
Definition: TMultiLayerPerceptron.h:125
TTreeFormulaManager
Used to coordinate one or more TTreeFormula objects.
Definition: TTreeFormulaManager.h:30
TMultiLayerPerceptron::fReset
Int_t fReset
! number of epochs between two resets of the search direction to the steepest descent - Default=50
Definition: TMultiLayerPerceptron.h:147
TMultiLayerPerceptron::ELearningMethod
ELearningMethod
Definition: TMultiLayerPerceptron.h:30
TMultiLayerPerceptron::GetLearningMethod
TMultiLayerPerceptron::ELearningMethod GetLearningMethod() const
Definition: TMultiLayerPerceptron.h:80
TMultiLayerPerceptron::operator=
TMultiLayerPerceptron & operator=(const TMultiLayerPerceptron &)
TMultiLayerPerceptron::~TMultiLayerPerceptron
virtual ~TMultiLayerPerceptron()
Destructor.
Definition: TMultiLayerPerceptron.cxx:569
TMultiLayerPerceptron::SetDelta
void SetDelta(Double_t delta)
Sets Delta - used in stochastic minimisation (look at the constructor for the complete description of...
Definition: TMultiLayerPerceptron.cxx:702
TMultiLayerPerceptron::fTrainingOwner
Bool_t fTrainingOwner
! internal flag whether one has to delete fTraining or not
Definition: TMultiLayerPerceptron.h:148
TMultiLayerPerceptron::SetTau
void SetTau(Double_t tau)
Sets Tau - used in line search (look at the constructor for the complete description of learning meth...
Definition: TMultiLayerPerceptron.cxx:722
TMultiLayerPerceptron::kTraining
@ kTraining
Definition: TMultiLayerPerceptron.h:32
TTreeFormula
Used to pass a selection expression to the Tree drawing routine.
Definition: TTreeFormula.h:58
TMultiLayerPerceptron::fData
TTree * fData
! pointer to the tree used as datasource
Definition: TMultiLayerPerceptron.h:123
TMultiLayerPerceptron::kBFGS
@ kBFGS
Definition: TMultiLayerPerceptron.h:31
TMultiLayerPerceptron::GetEpsilon
Double_t GetEpsilon() const
Definition: TMultiLayerPerceptron.h:77
TMultiLayerPerceptron::fLearningMethod
ELearningMethod fLearningMethod
! The Learning Method
Definition: TMultiLayerPerceptron.h:138
TMultiLayerPerceptron::SteepestDir
void SteepestDir(Double_t *)
Sets the search direction to steepest descent.
Definition: TMultiLayerPerceptron.cxx:2235
TMultiLayerPerceptron::TMultiLayerPerceptron
TMultiLayerPerceptron(const TMultiLayerPerceptron &)
TMultiLayerPerceptron::GetDelta
Double_t GetDelta() const
Definition: TMultiLayerPerceptron.h:78
TMultiLayerPerceptron::fextD
TString fextD
String containing the derivative name.
Definition: TMultiLayerPerceptron.h:135
TMultiLayerPerceptron::GetTau
Double_t GetTau() const
Definition: TMultiLayerPerceptron.h:81
TMultiLayerPerceptron::Shuffle
void Shuffle(Int_t *, Int_t) const
Shuffle the Int_t index[n] in input.
Definition: TMultiLayerPerceptron.cxx:2121
TMultiLayerPerceptron::Draw
virtual void Draw(Option_t *option="")
Draws the network structure.
Definition: TMultiLayerPerceptron.cxx:2506
TMultiLayerPerceptron::SetEventWeight
void SetEventWeight(const char *)
Set the event weight.
Definition: TMultiLayerPerceptron.cxx:594
TMultiLayerPerceptron::GetBFGSH
bool GetBFGSH(TMatrixD &, TMatrixD &, TMatrixD &)
Computes the hessian matrix using the BFGS update algorithm.
Definition: TMultiLayerPerceptron.cxx:2387
TMultiLayerPerceptron::Result
Double_t Result(Int_t event, Int_t index=0) const
Computes the output for a given event.
Definition: TMultiLayerPerceptron.cxx:1015
TMultiLayerPerceptron::SetTestDataSet
void SetTestDataSet(TEventList *test)
Sets the Test dataset.
Definition: TMultiLayerPerceptron.cxx:621
TMultiLayerPerceptron::SetGammaDelta
void SetGammaDelta(TMatrixD &, TMatrixD &, Double_t *)
Sets the gamma and delta vectors Gamma is computed here, so ComputeDEDw cannot have been called bef...
Definition: TMultiLayerPerceptron.cxx:2413
TMultiLayerPerceptron::Train
void Train(Int_t nEpoch, Option_t *option="text", Double_t minE=0)
Train the network.
Definition: TMultiLayerPerceptron.cxx:770
Double_t
double Double_t
Definition: RtypesCore.h:59
TMultiLayerPerceptron::fTraining
TEventList * fTraining
! EventList defining the events in the training dataset
Definition: TMultiLayerPerceptron.h:136
TMultiLayerPerceptron::kSteepestDescent
@ kSteepestDescent
Definition: TMultiLayerPerceptron.h:30
TObject.h
TObjArray.h
TMultiLayerPerceptron::ConjugateGradientsDir
void ConjugateGradientsDir(Double_t *, Double_t)
Sets the search direction to conjugate gradient direction beta should be:
Definition: TMultiLayerPerceptron.cxx:2361
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TMultiLayerPerceptron::fDelta
Double_t fDelta
! Delta - used in stochastic minimisation - Default=0.
Definition: TMultiLayerPerceptron.h:143
TMultiLayerPerceptron::GetCrossEntropy
Double_t GetCrossEntropy() const
Cross entropy error for a softmax output neuron, for a given event.
Definition: TMultiLayerPerceptron.cxx:1124
TMultiLayerPerceptron::fCurrentTree
Int_t fCurrentTree
! index of the current tree in a chain
Definition: TMultiLayerPerceptron.h:124
TMultiLayerPerceptron::kRibierePolak
@ kRibierePolak
Definition: TMultiLayerPerceptron.h:31
TMultiLayerPerceptron::fLastLayer
TObjArray fLastLayer
Collection of the output neurons; subset of fNetwork.
Definition: TMultiLayerPerceptron.h:128
TMultiLayerPerceptron::fEta
Double_t fEta
! Eta - used in stochastic minimisation - Default=0.1
Definition: TMultiLayerPerceptron.h:141
TMultiLayerPerceptron::BuildNetwork
void BuildNetwork()
Instantiates the network from the description.
Definition: TMultiLayerPerceptron.cxx:1352
TMultiLayerPerceptron::fEpsilon
Double_t fEpsilon
! Epsilon - used in stochastic minimisation - Default=0.
Definition: TMultiLayerPerceptron.h:142
TMultiLayerPerceptron::GetEntry
void GetEntry(Int_t) const
Load an entry into the network.
Definition: TMultiLayerPerceptron.cxx:741
type
int type
Definition: TGX11.cxx:121
TMultiLayerPerceptron::BFGSDir
void BFGSDir(TMatrixD &, Double_t *)
Computes the direction for the BFGS algorithm as the product between the Hessian estimate (bfgsh) and...
Definition: TMultiLayerPerceptron.cxx:2476
TMultiLayerPerceptron::SetEpsilon
void SetEpsilon(Double_t eps)
Sets Epsilon - used in stochastic minimisation (look at the constructor for the complete description ...
Definition: TMultiLayerPerceptron.cxx:692
TMultiLayerPerceptron::BuildOneHiddenLayer
void BuildOneHiddenLayer(const TString &sNumNodes, Int_t &layer, Int_t &prevStart, Int_t &prevStop, Bool_t lastLayer)
Builds a hidden layer, updates the number of layers.
Definition: TMultiLayerPerceptron.cxx:1420
TMatrixD.h
TMultiLayerPerceptron::fEtaDecay
Double_t fEtaDecay
! EtaDecay - Eta *= EtaDecay at each epoch - Default=1.
Definition: TMultiLayerPerceptron.h:144
TMultiLayerPerceptron::fEventWeight
TTreeFormula * fEventWeight
! formula representing the event weight
Definition: TMultiLayerPerceptron.h:139
TMultiLayerPerceptron::SetTrainingDataSet
void SetTrainingDataSet(TEventList *train)
Sets the Training dataset.
Definition: TMultiLayerPerceptron.cxx:610
TMultiLayerPerceptron::SetData
void SetData(TTree *)
Set the data source.
Definition: TMultiLayerPerceptron.cxx:578
TMultiLayerPerceptron::MLP_Line
void MLP_Line(Double_t *, Double_t *, Double_t)
Sets the weights to a point along a line Weights are set to [origin + (dist * dir)].
Definition: TMultiLayerPerceptron.cxx:2213
TMultiLayerPerceptron::fWeight
TString fWeight
String containing the event weight.
Definition: TMultiLayerPerceptron.h:131
TMultiLayerPerceptron::BuildLastLayer
void BuildLastLayer(TString &, Int_t)
Builds the output layer Neurons are linear combinations of input, by default.
Definition: TMultiLayerPerceptron.cxx:1465
int