ROOT  6.06/09
Reference Guide
MethodMLP.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Krzysztof Danielowski, Andreas Hoecker, Matt Jachowski, Kamil Kraszewski, Maciej Kruk, Peter Speckmayer, Joerg Stelzer, Eckhard von Toerne, Jan Therhaag, Jiahang Zhong
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodMLP *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * ANN Multilayer Perceptron class for the discrimination of signal *
12  * from background. BFGS implementation based on TMultiLayerPerceptron *
13  * class from ROOT (http://root.cern.ch). *
14  * *
15  * Authors (alphabetical): *
16  * Krzysztof Danielowski <danielow@cern.ch> - IFJ & AGH, Poland *
17  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
18  * Matt Jachowski <jachowski@stanford.edu> - Stanford University, USA *
19  * Kamil Kraszewski <kalq@cern.ch> - IFJ & UJ, Poland *
20  * Maciej Kruk <mkruk@cern.ch> - IFJ & AGH, Poland *
21  * Peter Speckmayer <peter.speckmayer@cern.ch> - CERN, Switzerland *
22  * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
23  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
24  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
25  * Jiahang Zhong <Jiahang.Zhong@cern.ch> - Academia Sinica, Taipei *
26  * *
27  * Copyright (c) 2005-2011: *
28  * CERN, Switzerland *
29  * U. of Victoria, Canada *
30  * MPI-K Heidelberg, Germany *
31  * U. of Bonn, Germany *
32  * *
33  * Redistribution and use in source and binary forms, with or without *
34  * modification, are permitted according to the terms listed in LICENSE *
35  * (http://tmva.sourceforge.net/LICENSE) *
36  **********************************************************************************/
37 
38 #ifndef ROOT_TMVA_MethodMLP
39 #define ROOT_TMVA_MethodMLP
40 
41 //////////////////////////////////////////////////////////////////////////
42 // //
43 // MethodMLP //
44 // //
45 // Multilayer Perceptron built off of MethodANNBase //
46 // //
47 //////////////////////////////////////////////////////////////////////////
48 
49 #include <vector>
50 #ifndef ROOT_TString
51 #include "TString.h"
52 #endif
53 #ifndef ROOT_TTree
54 #include "TTree.h"
55 #endif
56 #ifndef ROOT_TObjArray
57 #include "TObjArray.h"
58 #endif
59 #ifndef ROOT_TRandom3
60 #include "TRandom3.h"
61 #endif
62 #ifndef ROOT_TH1F
63 #include "TH1F.h"
64 #endif
65 #ifndef ROOT_TMatrixDfwd
66 #include "TMatrixDfwd.h"
67 #endif
68 
69 #ifndef ROOT_TMVA_IFitterTarget
70 #include "TMVA/IFitterTarget.h"
71 #endif
72 #ifndef ROOT_TMVA_MethodBase
73 #include "TMVA/MethodBase.h"
74 #endif
75 #ifndef ROOT_TMVA_MethodANNBase
76 #include "TMVA/MethodANNBase.h"
77 #endif
78 #ifndef ROOT_TMVA_TNeuron
79 #include "TMVA/TNeuron.h"
80 #endif
81 #ifndef ROOT_TMVA_TActivation
82 #include "TMVA/TActivation.h"
83 #endif
84 #ifndef ROOT_TMVA_ConvergenceTest
85 #include "TMVA/ConvergenceTest.h"
86 #endif
87 
88 #define MethodMLP_UseMinuit__
89 #undef MethodMLP_UseMinuit__
90 
91 namespace TMVA {
92 
93  class MethodMLP : public MethodANNBase, public IFitterTarget, public ConvergenceTest {
94 
95  public:
96 
97  // standard constructors
98  MethodMLP( const TString& jobName,
99  const TString& methodTitle,
100  DataSetInfo& theData,
101  const TString& theOption,
102  TDirectory* theTargetDir = 0 );
103 
104  MethodMLP( DataSetInfo& theData,
105  const TString& theWeightFile,
106  TDirectory* theTargetDir = 0 );
107 
108  virtual ~MethodMLP();
109 
110  virtual Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets );
111 
112  void Train() { Train(NumCycles()); }
113 
114  // for GA
115  Double_t ComputeEstimator ( std::vector<Double_t>& parameters );
116  Double_t EstimatorFunction( std::vector<Double_t>& parameters );
117 
118  enum ETrainingMethod { kBP=0, kBFGS, kGA };
120 
122  Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
123 
124  protected:
125 
126  // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
127  void MakeClassSpecific( std::ostream&, const TString& ) const;
128 
129  // get help message text
130  void GetHelpMessage() const;
131 
132 
133  private:
134 
135  // the option handling methods
136  void DeclareOptions();
137  void ProcessOptions();
138 
139  // general helper functions
140  void Train( Int_t nEpochs );
141  void Init();
142  void InitializeLearningRates(); // although this is only needed by backprop
143 
144  // used as a measure of success in all minimization techniques
146 
147  // BFGS functions
148  void BFGSMinimize( Int_t nEpochs );
149  void SetGammaDelta( TMatrixD &Gamma, TMatrixD &Delta, std::vector<Double_t> &Buffer );
150  void SteepestDir( TMatrixD &Dir );
151  Bool_t GetHessian( TMatrixD &Hessian, TMatrixD &Gamma, TMatrixD &Delta );
152  void SetDir( TMatrixD &Hessian, TMatrixD &Dir );
153  Double_t DerivDir( TMatrixD &Dir );
154  Bool_t LineSearch( TMatrixD &Dir, std::vector<Double_t> &Buffer, Double_t* dError=0 ); //zjh
155  void ComputeDEDw();
156  void SimulateEvent( const Event* ev );
157  void SetDirWeights( std::vector<Double_t> &Origin, TMatrixD &Dir, Double_t alpha );
158  Double_t GetError();
159  Double_t GetMSEErr( const Event* ev, UInt_t index = 0 ); //zjh
160  Double_t GetCEErr( const Event* ev, UInt_t index = 0 ); //zjh
161 
162  // backpropagation functions
163  void BackPropagationMinimize( Int_t nEpochs );
164  void TrainOneEpoch();
165  void Shuffle( Int_t* index, Int_t n );
166  void DecaySynapseWeights(Bool_t lateEpoch );
167  void TrainOneEvent( Int_t ievt);
168  Double_t GetDesiredOutput( const Event* ev );
169  void UpdateNetwork( Double_t desired, Double_t eventWeight=1.0 );
170  void UpdateNetwork(const std::vector<Float_t>& desired, Double_t eventWeight=1.0);
171  void CalculateNeuronDeltas();
172  void UpdateSynapses();
173  void AdjustSynapseWeights();
174 
175  // faster backpropagation
176  void TrainOneEventFast( Int_t ievt, Float_t*& branchVar, Int_t& type );
177 
178  // genetic algorithm functions
179  void GeneticMinimize();
180 
181 
182 #ifdef MethodMLP_UseMinuit__
183  // minuit functions -- commented out because they rely on a static pointer
184  void MinuitMinimize();
185  static MethodMLP* GetThisPtr();
186  static void IFCN( Int_t& npars, Double_t* grad, Double_t &f, Double_t* fitPars, Int_t ifl );
187  void FCN( Int_t& npars, Double_t* grad, Double_t &f, Double_t* fitPars, Int_t ifl );
188 #endif
189 
190  // general
191  bool fUseRegulator; // zjh
192  bool fCalculateErrors; // compute inverse hessian matrix at the end of the training
193  Double_t fPrior; // zjh
194  std::vector<Double_t> fPriorDev; // zjh
195  void GetApproxInvHessian ( TMatrixD& InvHessian, bool regulate=true ); //rank-1 approximation, neglect 2nd derivatives. //zjh
196  void UpdateRegulators(); // zjh
197  void UpdatePriors(); // zjh
199 
200  ETrainingMethod fTrainingMethod; // method of training, BP or GA
201  TString fTrainMethodS; // training method option param
202 
203  Float_t fSamplingFraction; // fraction of events which is sampled for training
204  Float_t fSamplingEpoch; // fraction of epochs where sampling is used
205  Float_t fSamplingWeight; // changing factor for event weights when sampling is turned on
206  Bool_t fSamplingTraining; // The training sample is sampled
207  Bool_t fSamplingTesting; // The testing sample is sampled
208 
209  // BFGS variables
210  Double_t fLastAlpha; // line search variable
211  Double_t fTau; // line search variable
212  Int_t fResetStep; // reset time (how often we clear hessian matrix)
213 
214  // backpropagation variable
215  Double_t fLearnRate; // learning rate for synapse weight adjustments
216  Double_t fDecayRate; // decay rate for above learning rate
217  EBPTrainingMode fBPMode; // backprop learning mode (sequential or batch)
218  TString fBpModeS; // backprop learning mode option string (sequential or batch)
219  Int_t fBatchSize; // batch size, only matters if in batch learning mode
220  Int_t fTestRate; // test for overtraining performed at each #th epochs
221  Bool_t fEpochMon; // create and fill epoch-wise monitoring histograms (makes outputfile big!)
222 
223  // genetic algorithm variables
224  Int_t fGA_nsteps; // GA settings: number of steps
225  Int_t fGA_preCalc; // GA settings: number of pre-calc steps
226  Int_t fGA_SC_steps; // GA settings: SC_steps
227  Int_t fGA_SC_rate; // GA settings: SC_rate
228  Double_t fGA_SC_factor; // GA settings: SC_factor
229 
230  // regression, storage of deviations
231  std::vector<std::pair<Float_t,Float_t> >* fDeviationsFromTargets; // deviation from the targets, event weight
232 
233  Float_t fWeightRange; // suppress outliers for the estimator calculation
234 
235 #ifdef MethodMLP_UseMinuit__
236  // minuit variables -- commented out because they rely on a static pointer
237  Int_t fNumberOfWeights; // Minuit: number of weights
238  static MethodMLP* fgThis; // Minuit: this pointer
239 #endif
240 
241  // debugging flags
242  static const Int_t fgPRINT_ESTIMATOR_INC = 10; // debug flags
243  static const Bool_t fgPRINT_SEQ = kFALSE; // debug flags
244  static const Bool_t fgPRINT_BATCH = kFALSE; // debug flags
245 
246  ClassDef(MethodMLP,0) // Multi-layer perceptron implemented specifically for TMVA
247  };
248 
249 } // namespace TMVA
250 
251 #endif
Float_t fSamplingFraction
Definition: MethodMLP.h:203
void GetHelpMessage() const
get help message text
Definition: MethodMLP.cxx:1660
Double_t GetMSEErr(const Event *ev, UInt_t index=0)
Definition: MethodMLP.cxx:966
virtual ~MethodMLP()
destructor nothing to be done
Definition: MethodMLP.cxx:127
void Init()
default initializations
Definition: MethodMLP.cxx:146
Double_t fLastAlpha
Definition: MethodMLP.h:210
static const Bool_t fgPRINT_SEQ
Definition: MethodMLP.h:243
Double_t GetError()
Definition: MethodMLP.cxx:929
float Float_t
Definition: RtypesCore.h:53
Int_t fGA_SC_rate
Definition: MethodMLP.h:227
Bool_t fSamplingTesting
Definition: MethodMLP.h:207
Int_t fResetStep
Definition: MethodMLP.h:212
void TrainOneEpoch()
train network over a single epoch/cyle of events
Definition: MethodMLP.cxx:1098
TString fTrainMethodS
Definition: MethodMLP.h:201
EAnalysisType
Definition: Types.h:124
void SteepestDir(TMatrixD &Dir)
Definition: MethodMLP.cxx:737
void SetDir(TMatrixD &Hessian, TMatrixD &Dir)
Definition: MethodMLP.cxx:771
Double_t fDecayRate
Definition: MethodMLP.h:216
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void TrainOneEventFast(Int_t ievt, Float_t *&branchVar, Int_t &type)
fast per-event training
Definition: MethodMLP.cxx:1177
void DeclareOptions()
define the options (their key words) that can be set in the option string know options: TrainingMetho...
Definition: MethodMLP.cxx:173
Int_t fGA_nsteps
Definition: MethodMLP.h:224
Double_t CalculateEstimator(Types::ETreeType treeType=Types::kTraining, Int_t iEpoch=-1)
calculate the estimator that training is attempting to minimize
Definition: MethodMLP.cxx:270
Double_t Gamma(Double_t z)
Computation of gamma(z) for all z.
Definition: TMath.cxx:352
void BFGSMinimize(Int_t nEpochs)
train network with BFGS algorithm
Definition: MethodMLP.cxx:459
bool HasInverseHessian()
Definition: MethodMLP.h:121
Int_t fGA_preCalc
Definition: MethodMLP.h:225
Float_t fSamplingEpoch
Definition: MethodMLP.h:204
void SetDirWeights(std::vector< Double_t > &Origin, TMatrixD &Dir, Double_t alpha)
Definition: MethodMLP.cxx:913
ETrainingMethod fTrainingMethod
Definition: MethodMLP.h:200
void Shuffle(Int_t *index, Int_t n)
Input: index: the array to shuffle n: the size of the array Output: index: the shuffled indexes This ...
Definition: MethodMLP.cxx:1145
#define ClassDef(name, id)
Definition: Rtypes.h:254
void(* FCN)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
Definition: testMinim.cxx:39
void GetApproxInvHessian(TMatrixD &InvHessian, bool regulate=true)
Definition: MethodMLP.cxx:1453
EBPTrainingMode fBPMode
Definition: MethodMLP.h:217
Double_t fGA_SC_factor
Definition: MethodMLP.h:228
void ComputeDEDw()
Definition: MethodMLP.cxx:660
bool fUseRegulator
Definition: MethodMLP.h:191
void TrainOneEvent(Int_t ievt)
train network over a single event this uses the new event model
Definition: MethodMLP.cxx:1213
Bool_t fEpochMon
Definition: MethodMLP.h:221
Int_t fUpdateLimit
Definition: MethodMLP.h:198
void UpdateSynapses()
update synapse error fields and adjust the weights (if in sequential mode)
Definition: MethodMLP.cxx:1357
std::vector< Double_t > fPriorDev
Definition: MethodMLP.h:194
Double_t GetCEErr(const Event *ev, UInt_t index=0)
Definition: MethodMLP.cxx:983
void GeneticMinimize()
create genetics class similar to GeneticCut give it vector of parameter ranges (parameters = weights)...
Definition: MethodMLP.cxx:1301
void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
Definition: MethodMLP.cxx:1649
void InitializeLearningRates()
initialize learning rates of synapses, used only by backpropagation
Definition: MethodMLP.cxx:256
void DecaySynapseWeights(Bool_t lateEpoch)
decay synapse weights in last 10 epochs, lower learning rate even more to find a good minimum ...
Definition: MethodMLP.cxx:1163
Double_t GetDesiredOutput(const Event *ev)
get the desired output of this event
Definition: MethodMLP.cxx:1232
Bool_t GetHessian(TMatrixD &Hessian, TMatrixD &Gamma, TMatrixD &Delta)
Definition: MethodMLP.cxx:750
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
get the mva value generated by the NN
Definition: MethodMLP.cxx:1494
void BackPropagationMinimize(Int_t nEpochs)
minimize estimator / train network with backpropagation algorithm
Definition: MethodMLP.cxx:1000
Float_t fWeightRange
Definition: MethodMLP.h:233
unsigned int UInt_t
Definition: RtypesCore.h:42
static const Int_t fgPRINT_ESTIMATOR_INC
Definition: MethodMLP.h:242
Bool_t LineSearch(TMatrixD &Dir, std::vector< Double_t > &Buffer, Double_t *dError=0)
Definition: MethodMLP.cxx:803
void UpdateNetwork(Double_t desired, Double_t eventWeight=1.0)
update the network based on how closely the output matched the desired output
Definition: MethodMLP.cxx:1242
static const Bool_t fgPRINT_BATCH
Definition: MethodMLP.h:244
TString fBpModeS
Definition: MethodMLP.h:218
void SetGammaDelta(TMatrixD &Gamma, TMatrixD &Delta, std::vector< Double_t > &Buffer)
Definition: MethodMLP.cxx:635
double f(double x)
void CalculateNeuronDeltas()
have each neuron calculate its delta by backpropagation
Definition: MethodMLP.cxx:1273
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:41
int type
Definition: TGX11.cxx:120
Double_t fLearnRate
Definition: MethodMLP.h:215
void SimulateEvent(const Event *ev)
Definition: MethodMLP.cxx:697
Double_t fPrior
Definition: MethodMLP.h:193
void AdjustSynapseWeights()
just adjust the synapse weights (should be called in batch mode)
Definition: MethodMLP.cxx:1379
Double_t fTau
Definition: MethodMLP.h:211
Int_t fBatchSize
Definition: MethodMLP.h:219
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
MLP can handle classification with 2 classes and regression with one regression-target.
Definition: MethodMLP.cxx:134
MethodMLP(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption, TDirectory *theTargetDir=0)
Abstract ClassifierFactory template that handles arbitrary types.
void ProcessOptions()
process user options
Definition: MethodMLP.cxx:225
Float_t fSamplingWeight
Definition: MethodMLP.h:205
bool fCalculateErrors
Definition: MethodMLP.h:192
Double_t EstimatorFunction(std::vector< Double_t > &parameters)
interface to the estimate
Definition: MethodMLP.cxx:1330
Double_t ComputeEstimator(std::vector< Double_t > &parameters)
this function is called by GeneticANN for GA optimization
Definition: MethodMLP.cxx:1338
std::vector< std::pair< Float_t, Float_t > > * fDeviationsFromTargets
Definition: MethodMLP.h:231
Double_t DerivDir(TMatrixD &Dir)
Definition: MethodMLP.cxx:788
void UpdateRegulators()
Definition: MethodMLP.cxx:1413
Int_t fGA_SC_steps
Definition: MethodMLP.h:226
const Int_t n
Definition: legend1.C:16
Bool_t fSamplingTraining
Definition: MethodMLP.h:206