ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  TDirectory* theTargetDir );
91 
92  MethodANNBase( Types::EMVA methodType,
93  DataSetInfo& theData,
94  const TString& theWeightFile,
95  TDirectory* theTargetDir );
96 
97  virtual ~MethodANNBase();
98 
99  // this does the real initialization work
100  void InitANNBase();
101 
102  // setters for subclasses
103  void SetActivation(TActivation* activation) {
104  if (fActivation != NULL) delete fActivation; fActivation = activation;
105  }
106  void SetNeuronInputCalculator(TNeuronInput* inputCalculator) {
107  if (fInputCalculator != NULL) delete fInputCalculator;
108  fInputCalculator = inputCalculator;
109  }
110 
111  // this will have to be overridden by every subclass
112  virtual void Train() = 0;
113 
114  // print network, for debugging
115  virtual void PrintNetwork() const;
116 
117 
118  // call this function like that:
119  // ...
120  // MethodMLP* mlp = dynamic_cast<MethodMLP*>(method);
121  // std::vector<float> layerValues;
122  // mlp->GetLayerActivation (2, std::back_inserter(layerValues));
123  // ... do now something with the layerValues
124  //
125  template <typename WriteIterator>
126  void GetLayerActivation (size_t layer, WriteIterator writeIterator);
127 
129 
130  // write weights to file
131  void AddWeightsXMLTo( void* parent ) const;
132  void ReadWeightsFromXML( void* wghtnode );
133 
134  // read weights from file
135  virtual void ReadWeightsFromStream( std::istream& istr );
136 
137  // calculate the MVA value
138  virtual Double_t GetMvaValue( Double_t* err = 0, Double_t* errUpper = 0 );
139 
140  virtual const std::vector<Float_t> &GetRegressionValues();
141 
142  virtual const std::vector<Float_t> &GetMulticlassValues();
143 
144  // write method specific histos to target file
145  virtual void WriteMonitoringHistosToFile() const;
146 
147  // ranking of input variables
148  const Ranking* CreateRanking();
149 
150  // the option handling methods
151  virtual void DeclareOptions();
152  virtual void ProcessOptions();
153 
154  Bool_t Debug() const;
155 
156  enum EEstimator { kMSE=0,kCE};
157 
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* fNetwork; // TObjArray of TObjArrays representing network
181  TObjArray* fSynapses; // array of pointers to synapses, no structural data
182  TActivation* fActivation; // activation function to be used for hidden layers
183  TActivation* fOutput; // activation function to be used for output layers, depending on estimator
184  TActivation* fIdentity; // activation for input and output layers
185  TRandom3* frgen; // random number generator for various uses
186  TNeuronInput* fInputCalculator; // input calculator for all neurons
187 
188  std::vector<Int_t> fRegulatorIdx; //index to different priors from every synapses
189  std::vector<Double_t> fRegulators; //the priors as regulator
192 
193  // monitoring histograms
194  TH1F* fEstimatorHistTrain; // monitors convergence of training sample
195  TH1F* fEstimatorHistTest; // monitors convergence of independent test sample
196 
197  // monitoring histograms (not available for regression)
198  void CreateWeightMonitoringHists( const TString& bulkname, std::vector<TH1*>* hv = 0 ) const;
199  std::vector<TH1*> fEpochMonHistS; // epoch monitoring hitograms for signal
200  std::vector<TH1*> fEpochMonHistB; // epoch monitoring hitograms for background
201  std::vector<TH1*> fEpochMonHistW; // epoch monitoring hitograms for weights
202 
203 
204  // general
206  bool fUseRegulator; // zjh
207 
208  protected:
209  Int_t fRandomSeed; // random seed for initial synapse weights
210 
211  Int_t fNcycles; // number of epochs to train
212 
213  TString fNeuronType; // name of neuron activation function class
214  TString fNeuronInputType; // name of neuron input calculator class
215 
216 
217  private:
218 
219  // helper functions for building network
220  void BuildLayers(std::vector<Int_t>* layout, Bool_t from_file = false);
221  void BuildLayer(Int_t numNeurons, TObjArray* curLayer, TObjArray* prevLayer,
222  Int_t layerIndex, Int_t numLayers, Bool_t from_file = false);
223  void AddPreLinks(TNeuron* neuron, TObjArray* prevLayer);
224 
225  // helper functions for weight initialization
226  void InitWeights();
227  void ForceWeights(std::vector<Double_t>* weights);
228 
229  // helper functions for deleting network
230  void DeleteNetwork();
231  void DeleteNetworkLayer(TObjArray*& layer);
232 
233  // debugging utilities
234  void PrintLayer(TObjArray* layer) const;
235  void PrintNeuron(TNeuron* neuron) const;
236 
237  // private variables
238  TObjArray* fInputLayer; // cache this for fast access
239  std::vector<TNeuron*> fOutputNeurons; // cache this for fast access
240  TString fLayerSpec; // layout specification option
241 
242  // some static flags
243  static const Bool_t fgDEBUG = kTRUE; // debug flag
244 
245  ClassDef(MethodANNBase,0) // Base class for TMVA ANNs
246  };
247 
248 
249 
250  template <typename WriteIterator>
251  inline void MethodANNBase::GetLayerActivation (size_t layerNumber, WriteIterator writeIterator)
252  {
253  // get the activation values of the nodes in layer "layer"
254  // write the node activation values into the writeIterator
255  // assumes, that the network has been computed already (by calling
256  // "GetRegressionValues")
257 
258  if (layerNumber >= (size_t)fNetwork->GetEntriesFast())
259  return;
260 
261  TObjArray* layer = (TObjArray*)fNetwork->At(layerNumber);
262  UInt_t nNodes = layer->GetEntriesFast();
263  for (UInt_t iNode = 0; iNode < nNodes; iNode++)
264  {
265  (*writeIterator) = ((TNeuron*)layer->At(iNode))->GetActivationValue();
266  ++writeIterator;
267  }
268  }
269 
270 
271 } // namespace TMVA
272 
273 #endif
void WaitForKeyboard()
wait for keyboard input, for debugging
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 ...
void AddWeightsXMLTo(void *parent) const
create XML description of ANN classifier
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 DeleteNetwork()
delete/clear network
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
TNeuronInput * fInputCalculator
TObjArray * fInputLayer
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
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)
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
virtual void DeclareOptions()
define the options (their key words) that can be set in the option string here the options valid for ...
static const Bool_t fgDEBUG
void ForceWeights(std::vector< Double_t > *weights)
force the synapse weights
TActivation * fOutput
#define ClassDef(name, id)
Definition: Rtypes.h:254
std::vector< TH1 * > fEpochMonHistB
void PrintLayer(TObjArray *layer) const
print a single layer, for debugging
TObjArray * fNetwork
void PrintMessage(TString message, Bool_t force=kFALSE) const
print messages, turn off printing by setting verbose and debug flag appropriately ...
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
Double_t GetActivationValue() const
Definition: TNeuron.h:117
virtual void WriteMonitoringHistosToFile() const
write histograms to file
Bool_t Debug() const
who the hell makes such strange Debug flags that even use "global pointers"..
unsigned int UInt_t
Definition: RtypesCore.h:42
std::vector< TH1 * > fEpochMonHistW
std::vector< Double_t > fRegulators
TNeuron * GetInputNeuron(Int_t index)
void CreateWeightMonitoringHists(const TString &bulkname, std::vector< TH1 * > *hv=0) const
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)
Describe directory structure in memory.
Definition: TDirectory.h:44
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
virtual void PrintNetwork() const
print network representation, for debugging
Double_t GetNetworkOutput()
MethodANNBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &theData, const TString &theOption, TDirectory *theTargetDir)
virtual const std::vector< Float_t > & GetRegressionValues()
get the regression value generated by the NN
#define NULL
Definition: Rtypes.h:82
std::vector< TH1 * > fEpochMonHistS
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
string message
Definition: ROOT.py:94
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 MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
void PrintNeuron(TNeuron *neuron) const
print a neuron, for debugging
virtual void Train()=0
void InitANNBase()
initialize ANNBase object