ROOT  6.06/08
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  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 != nullptr) delete fActivation;
105  fActivation = activation;
106  }
107  void SetNeuronInputCalculator(TNeuronInput* inputCalculator) {
108  if (fInputCalculator != nullptr) delete fInputCalculator;
109  fInputCalculator = inputCalculator;
110  }
111 
112  // this will have to be overridden by every subclass
113  virtual void Train() = 0;
114 
115  // print network, for debugging
116  virtual void PrintNetwork() const;
117 
118 
119  // call this function like that:
120  // ...
121  // MethodMLP* mlp = dynamic_cast<MethodMLP*>(method);
122  // std::vector<float> layerValues;
123  // mlp->GetLayerActivation (2, std::back_inserter(layerValues));
124  // ... do now something with the layerValues
125  //
126  template <typename WriteIterator>
127  void GetLayerActivation (size_t layer, WriteIterator writeIterator);
128 
130 
131  // write weights to file
132  void AddWeightsXMLTo( void* parent ) const;
133  void ReadWeightsFromXML( void* wghtnode );
134 
135  // read weights from file
136  virtual void ReadWeightsFromStream( std::istream& istr );
137 
138  // calculate the MVA value
139  virtual Double_t GetMvaValue( Double_t* err = 0, Double_t* errUpper = 0 );
140 
141  virtual const std::vector<Float_t> &GetRegressionValues();
142 
143  virtual const std::vector<Float_t> &GetMulticlassValues();
144 
145  // write method specific histos to target file
146  virtual void WriteMonitoringHistosToFile() const;
147 
148  // ranking of input variables
149  const Ranking* CreateRanking();
150 
151  // the option handling methods
152  virtual void DeclareOptions();
153  virtual void ProcessOptions();
154 
155  Bool_t Debug() const;
156 
157  enum EEstimator { kMSE=0,kCE};
158 
159 
160  protected:
161 
162  virtual void MakeClassSpecific( std::ostream&, const TString& ) const;
163 
164  std::vector<Int_t>* ParseLayoutString( TString layerSpec );
165  virtual void BuildNetwork( std::vector<Int_t>* layout, std::vector<Double_t>* weights=NULL,
166  Bool_t fromFile = kFALSE );
167  void ForceNetworkInputs( const Event* ev, Int_t ignoreIndex = -1 );
169 
170  // debugging utilities
171  void PrintMessage( TString message, Bool_t force = kFALSE ) const;
173  void WaitForKeyboard();
174 
175  // accessors
176  Int_t NumCycles() { return fNcycles; }
177  TNeuron* GetInputNeuron (Int_t index) { return (TNeuron*)fInputLayer->At(index); }
178  TNeuron* GetOutputNeuron(Int_t index = 0) { return fOutputNeurons.at(index); }
179 
180  // protected variables
181  TObjArray* fNetwork; // TObjArray of TObjArrays representing network
182  TObjArray* fSynapses; // array of pointers to synapses, no structural data
183  TActivation* fActivation; // activation function to be used for hidden layers
184  TActivation* fOutput; // activation function to be used for output layers, depending on estimator
185  TActivation* fIdentity; // activation for input and output layers
186  TRandom3* frgen; // random number generator for various uses
187  TNeuronInput* fInputCalculator; // input calculator for all neurons
188 
189  std::vector<Int_t> fRegulatorIdx; //index to different priors from every synapses
190  std::vector<Double_t> fRegulators; //the priors as regulator
193 
194  // monitoring histograms
195  TH1F* fEstimatorHistTrain; // monitors convergence of training sample
196  TH1F* fEstimatorHistTest; // monitors convergence of independent test sample
197 
198  // monitoring histograms (not available for regression)
199  void CreateWeightMonitoringHists( const TString& bulkname, std::vector<TH1*>* hv = 0 ) const;
200  std::vector<TH1*> fEpochMonHistS; // epoch monitoring hitograms for signal
201  std::vector<TH1*> fEpochMonHistB; // epoch monitoring hitograms for background
202  std::vector<TH1*> fEpochMonHistW; // epoch monitoring hitograms for weights
203 
204 
205  // general
207  bool fUseRegulator; // zjh
208 
209  protected:
210  Int_t fRandomSeed; // random seed for initial synapse weights
211 
212  Int_t fNcycles; // number of epochs to train
213 
214  TString fNeuronType; // name of neuron activation function class
215  TString fNeuronInputType; // name of neuron input calculator class
216 
217 
218  private:
219 
220  // helper functions for building network
221  void BuildLayers(std::vector<Int_t>* layout, Bool_t from_file = false);
222  void BuildLayer(Int_t numNeurons, TObjArray* curLayer, TObjArray* prevLayer,
223  Int_t layerIndex, Int_t numLayers, Bool_t from_file = false);
224  void AddPreLinks(TNeuron* neuron, TObjArray* prevLayer);
225 
226  // helper functions for weight initialization
227  void InitWeights();
228  void ForceWeights(std::vector<Double_t>* weights);
229 
230  // helper functions for deleting network
231  void DeleteNetwork();
232  void DeleteNetworkLayer(TObjArray*& layer);
233 
234  // debugging utilities
235  void PrintLayer(TObjArray* layer) const;
236  void PrintNeuron(TNeuron* neuron) const;
237 
238  // private variables
239  TObjArray* fInputLayer; // cache this for fast access
240  std::vector<TNeuron*> fOutputNeurons; // cache this for fast access
241  TString fLayerSpec; // layout specification option
242 
243  // some static flags
244  static const Bool_t fgDEBUG = kTRUE; // debug flag
245 
246  ClassDef(MethodANNBase,0) // Base class for TMVA ANNs
247  };
248 
249 
250 
251  template <typename WriteIterator>
252  inline void MethodANNBase::GetLayerActivation (size_t layerNumber, WriteIterator writeIterator)
253  {
254  // get the activation values of the nodes in layer "layer"
255  // write the node activation values into the writeIterator
256  // assumes, that the network has been computed already (by calling
257  // "GetRegressionValues")
258 
259  if (layerNumber >= (size_t)fNetwork->GetEntriesFast())
260  return;
261 
262  TObjArray* layer = (TObjArray*)fNetwork->At(layerNumber);
263  UInt_t nNodes = layer->GetEntriesFast();
264  for (UInt_t iNode = 0; iNode < nNodes; iNode++)
265  {
266  (*writeIterator) = ((TNeuron*)layer->At(iNode))->GetActivationValue();
267  ++writeIterator;
268  }
269  }
270 
271 
272 } // namespace TMVA
273 
274 #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
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
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)
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
message
Definition: ROOT.py:94
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)
Describe directory structure in memory.
Definition: TDirectory.h:41
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.
MethodANNBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &theData, const TString &theOption, TDirectory *theTargetDir)
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.
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