Logo ROOT   6.08/07
Reference Guide
MethodANNBase.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Matt Jachowski, Jan Therhaag
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodANNBase *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Artificial neural network base class for the discrimination of signal *
12  * from background. *
13  * *
14  * Authors (alphabetical): *
15  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
16  * Matt Jachowski <jachowski@stanford.edu> - Stanford University, USA *
17  * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
18  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
19  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
20  * *
21  * Small changes (regression): *
22  * Krzysztof Danielowski <danielow@cern.ch> - IFJ PAN & AGH, Poland *
23  * Kamil Kraszewski <kalq@cern.ch> - IFJ PAN & UJ , Poland *
24  * Maciej Kruk <mkruk@cern.ch> - IFJ PAN & AGH, Poland *
25  * *
26  * Copyright (c) 2005-2011: *
27  * CERN, Switzerland *
28  * *
29  * Redistribution and use in source and binary forms, with or without *
30  * modification, are permitted according to the terms listed in LICENSE *
31  * (http://tmva.sourceforge.net/LICENSE) *
32  **********************************************************************************/
33 
34 #ifndef ROOT_TMVA_MethodANNBase
35 #define ROOT_TMVA_MethodANNBase
36 
37 //////////////////////////////////////////////////////////////////////////
38 // //
39 // MethodANNBase //
40 // //
41 // Base class for all TMVA methods using artificial neural networks //
42 // //
43 //////////////////////////////////////////////////////////////////////////
44 
45 #ifndef ROOT_TString
46 #include "TString.h"
47 #endif
48 #include <vector>
49 #ifndef ROOT_TTree
50 #include "TTree.h"
51 #endif
52 #ifndef ROOT_TObjArray
53 #include "TObjArray.h"
54 #endif
55 #ifndef ROOT_TRandom3
56 #include "TRandom3.h"
57 #endif
58 #ifndef ROOT_TMatrix
59 #include "TMatrix.h"
60 #endif
61 
62 #ifndef ROOT_TMVA_MethodBase
63 #include "TMVA/MethodBase.h"
64 #endif
65 #ifndef ROOT_TMVA_TActivation
66 #include "TMVA/TActivation.h"
67 #endif
68 #ifndef ROOT_TMVA_TNeuron
69 #include "TMVA/TNeuron.h"
70 #endif
71 #ifndef ROOT_TMVA_TNeuronInput
72 #include "TMVA/TNeuronInput.h"
73 #endif
74 
75 class TH1;
76 class TH1F;
77 
78 namespace TMVA {
79 
80  class MethodANNBase : public MethodBase {
81 
82  public:
83 
84  // constructors dictated by subclassing off of MethodBase
85  MethodANNBase( const TString& jobName,
86  Types::EMVA methodType,
87  const TString& methodTitle,
88  DataSetInfo& theData,
89  const TString& theOption );
90 
91  MethodANNBase( Types::EMVA methodType,
92  DataSetInfo& theData,
93  const TString& theWeightFile);
94 
95  virtual ~MethodANNBase();
96 
97  // this does the real initialization work
98  void InitANNBase();
99 
100  // setters for subclasses
101  void SetActivation(TActivation* activation) {
102  if (fActivation != nullptr) delete fActivation;
103  fActivation = activation;
104  }
105  void SetNeuronInputCalculator(TNeuronInput* inputCalculator) {
106  if (fInputCalculator != nullptr) delete fInputCalculator;
107  fInputCalculator = inputCalculator;
108  }
109 
110  // this will have to be overridden by every subclass
111  virtual void Train() = 0;
112 
113  // print network, for debugging
114  virtual void PrintNetwork() const;
115 
116 
117  // call this function like that:
118  // ...
119  // MethodMLP* mlp = dynamic_cast<MethodMLP*>(method);
120  // std::vector<float> layerValues;
121  // mlp->GetLayerActivation (2, std::back_inserter(layerValues));
122  // ... do now something with the layerValues
123  //
124  template <typename WriteIterator>
125  void GetLayerActivation (size_t layer, WriteIterator writeIterator);
126 
128 
129  // write weights to file
130  void AddWeightsXMLTo( void* parent ) const;
131  void ReadWeightsFromXML( void* wghtnode );
132 
133  // read weights from file
134  virtual void ReadWeightsFromStream( std::istream& istr );
135 
136  // calculate the MVA value
137  virtual Double_t GetMvaValue( Double_t* err = 0, Double_t* errUpper = 0 );
138 
139  virtual const std::vector<Float_t> &GetRegressionValues();
140 
141  virtual const std::vector<Float_t> &GetMulticlassValues();
142 
143  // write method specific histos to target file
144  virtual void WriteMonitoringHistosToFile() const;
145 
146  // ranking of input variables
147  const Ranking* CreateRanking();
148 
149  // the option handling methods
150  virtual void DeclareOptions();
151  virtual void ProcessOptions();
152 
153  Bool_t Debug() const;
154 
155  enum EEstimator { kMSE=0,kCE};
156 
157  TObjArray* fNetwork; // TObjArray of TObjArrays representing network
158 
159  protected:
160 
161  virtual void MakeClassSpecific( std::ostream&, const TString& ) const;
162 
163  std::vector<Int_t>* ParseLayoutString( TString layerSpec );
164  virtual void BuildNetwork( std::vector<Int_t>* layout, std::vector<Double_t>* weights=NULL,
165  Bool_t fromFile = kFALSE );
166  void ForceNetworkInputs( const Event* ev, Int_t ignoreIndex = -1 );
168 
169  // debugging utilities
170  void PrintMessage( TString message, Bool_t force = kFALSE ) const;
172  void WaitForKeyboard();
173 
174  // accessors
175  Int_t NumCycles() { return fNcycles; }
176  TNeuron* GetInputNeuron (Int_t index) { return (TNeuron*)fInputLayer->At(index); }
177  TNeuron* GetOutputNeuron(Int_t index = 0) { return fOutputNeurons.at(index); }
178 
179  // protected variables
180  TObjArray* fSynapses; // array of pointers to synapses, no structural data
181  TActivation* fActivation; // activation function to be used for hidden layers
182  TActivation* fOutput; // activation function to be used for output layers, depending on estimator
183  TActivation* fIdentity; // activation for input and output layers
184  TRandom3* frgen; // random number generator for various uses
185  TNeuronInput* fInputCalculator; // input calculator for all neurons
186 
187  std::vector<Int_t> fRegulatorIdx; //index to different priors from every synapses
188  std::vector<Double_t> fRegulators; //the priors as regulator
191 
192  // monitoring histograms
193  TH1F* fEstimatorHistTrain; // monitors convergence of training sample
194  TH1F* fEstimatorHistTest; // monitors convergence of independent test sample
195 
196  // monitoring histograms (not available for regression)
197  void CreateWeightMonitoringHists( const TString& bulkname, std::vector<TH1*>* hv = 0 ) const;
198  std::vector<TH1*> fEpochMonHistS; // epoch monitoring hitograms for signal
199  std::vector<TH1*> fEpochMonHistB; // epoch monitoring hitograms for background
200  std::vector<TH1*> fEpochMonHistW; // epoch monitoring hitograms for weights
201 
202 
203  // general
205  bool fUseRegulator; // zjh
206 
207  protected:
208  Int_t fRandomSeed; // random seed for initial synapse weights
209 
210  Int_t fNcycles; // number of epochs to train
211 
212  TString fNeuronType; // name of neuron activation function class
213  TString fNeuronInputType; // name of neuron input calculator class
214 
215 
216  private:
217 
218  // helper functions for building network
219  void BuildLayers(std::vector<Int_t>* layout, Bool_t from_file = false);
220  void BuildLayer(Int_t numNeurons, TObjArray* curLayer, TObjArray* prevLayer,
221  Int_t layerIndex, Int_t numLayers, Bool_t from_file = false);
222  void AddPreLinks(TNeuron* neuron, TObjArray* prevLayer);
223 
224  // helper functions for weight initialization
225  void InitWeights();
226  void ForceWeights(std::vector<Double_t>* weights);
227 
228  // helper functions for deleting network
229  void DeleteNetwork();
230  void DeleteNetworkLayer(TObjArray*& layer);
231 
232  // debugging utilities
233  void PrintLayer(TObjArray* layer) const;
234  void PrintNeuron(TNeuron* neuron) const;
235 
236  // private variables
237  TObjArray* fInputLayer; // cache this for fast access
238  std::vector<TNeuron*> fOutputNeurons; // cache this for fast access
239  TString fLayerSpec; // layout specification option
240 
241  // some static flags
242  static const Bool_t fgDEBUG = kTRUE; // debug flag
243 
244  ClassDef(MethodANNBase,0); // Base class for TMVA ANNs
245  };
246 
247 
248 
249  template <typename WriteIterator>
250  inline void MethodANNBase::GetLayerActivation (size_t layerNumber, WriteIterator writeIterator)
251  {
252  // get the activation values of the nodes in layer "layer"
253  // write the node activation values into the writeIterator
254  // assumes, that the network has been computed already (by calling
255  // "GetRegressionValues")
256 
257  if (layerNumber >= (size_t)fNetwork->GetEntriesFast())
258  return;
259 
260  TObjArray* layer = (TObjArray*)fNetwork->At(layerNumber);
261  UInt_t nNodes = layer->GetEntriesFast();
262  for (UInt_t iNode = 0; iNode < nNodes; iNode++)
263  {
264  (*writeIterator) = ((TNeuron*)layer->At(iNode))->GetActivationValue();
265  ++writeIterator;
266  }
267  }
268 
269 
270 } // namespace TMVA
271 
272 #endif
void WaitForKeyboard()
wait for keyboard input, for debugging
virtual void WriteMonitoringHistosToFile() const
write histograms to file
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
get the mva value generated by the NN
void BuildLayer(Int_t numNeurons, TObjArray *curLayer, TObjArray *prevLayer, Int_t layerIndex, Int_t numLayers, Bool_t from_file=false)
build a single layer with neurons and synapses connecting this layer to the previous layer ...
An array of TObjects.
Definition: TObjArray.h:39
Random number generator class based on M.
Definition: TRandom3.h:29
void SetActivation(TActivation *activation)
void ForceNetworkCalculations()
calculate input values to each neuron
void CreateWeightMonitoringHists(const TString &bulkname, std::vector< TH1 *> *hv=0) const
void DeleteNetwork()
delete/clear network
MethodANNBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
standard constructor Note: Right now it is an option to choose the neuron input function, but only the input function "sum" leads to weight convergence – otherwise the weights go to nan and lead to an ABORT.
TActivation * fActivation
void AddPreLinks(TNeuron *neuron, TObjArray *prevLayer)
add synapses connecting a neuron to its preceding layer
const Ranking * CreateRanking()
compute ranking of input variables by summing function of weights
virtual void ReadWeightsFromStream(std::istream &istr)
destroy/clear the network then read it back in from the weights file
virtual void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
TNeuronInput * fInputCalculator
TObjArray * fInputLayer
Basic string class.
Definition: TString.h:137
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void GetLayerActivation(size_t layer, WriteIterator writeIterator)
Double_t GetActivationValue() const
Definition: TNeuron.h:117
void AddWeightsXMLTo(void *parent) const
create XML description of ANN classifier
virtual void DeclareOptions()
define the options (their key words) that can be set in the option string here the options valid for ...
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
static const Bool_t fgDEBUG
void ForceWeights(std::vector< Double_t > *weights)
force the synapse weights
Bool_t Debug() const
who the hell makes such strange Debug flags that even use "global pointers"..
TActivation * fOutput
#define ClassDef(name, id)
Definition: Rtypes.h:254
std::vector< TH1 * > fEpochMonHistB
void PrintMessage(TString message, Bool_t force=kFALSE) const
print messages, turn off printing by setting verbose and debug flag appropriately ...
TObjArray * fNetwork
virtual void ProcessOptions()
do nothing specific at this moment
TActivation * fIdentity
virtual void BuildNetwork(std::vector< Int_t > *layout, std::vector< Double_t > *weights=NULL, Bool_t fromFile=kFALSE)
build network given a layout (number of neurons in each layer) and optional weights array ...
void SetNeuronInputCalculator(TNeuronInput *inputCalculator)
void ReadWeightsFromXML(void *wghtnode)
read MLP from xml weight file
TObjArray * fSynapses
virtual void PrintNetwork() const
print network representation, for debugging
unsigned int UInt_t
Definition: RtypesCore.h:42
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
std::vector< TH1 * > fEpochMonHistW
std::vector< Double_t > fRegulators
TNeuron * GetInputNeuron(Int_t index)
std::vector< Int_t > fRegulatorIdx
void InitWeights()
initialize the synapse weights randomly
std::vector< Int_t > * ParseLayoutString(TString layerSpec)
parse layout specification string and return a vector, each entry containing the number of neurons to...
double Double_t
Definition: RtypesCore.h:55
virtual const std::vector< Float_t > & GetMulticlassValues()
get the multiclass classification values generated by the NN
TNeuron * GetOutputNeuron(Int_t index=0)
void ForceNetworkInputs(const Event *ev, Int_t ignoreIndex=-1)
force the input values of the input neurons force the value for each input neuron ...
The TH1 histogram class.
Definition: TH1.h:80
std::vector< TNeuron * > fOutputNeurons
virtual ~MethodANNBase()
destructor
Double_t GetNetworkOutput()
Abstract ClassifierFactory template that handles arbitrary types.
virtual const std::vector< Float_t > & GetRegressionValues()
get the regression value generated by the NN
#define NULL
Definition: Rtypes.h:82
void PrintNeuron(TNeuron *neuron) const
print a neuron, for debugging
std::vector< TH1 * > fEpochMonHistS
void DeleteNetworkLayer(TObjArray *&layer)
delete a network layer
virtual void ReadWeightsFromStream(std::istream &)=0
const Bool_t kTRUE
Definition: Rtypes.h:91
void BuildLayers(std::vector< Int_t > *layout, Bool_t from_file=false)
build the network layers
virtual void Train()=0
void PrintLayer(TObjArray *layer) const
print a single layer, for debugging
void InitANNBase()
initialize ANNBase object