TMultiLayerPerceptron.h

Go to the documentation of this file.
00001 // @(#)root/mlp:$Id: TMultiLayerPerceptron.h 36832 2010-11-22 08:53:49Z brun $
00002 // Author: Christophe.Delaere@cern.ch   20/07/03
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TMultiLayerPerceptron
00013 #define ROOT_TMultiLayerPerceptron
00014 
00015 #ifndef ROOT_TObject
00016 #include "TObject.h"
00017 #endif
00018 #ifndef ROOT_TString
00019 #include "TString.h"
00020 #endif
00021 #ifndef ROOT_TObjArray
00022 #include "TObjArray.h"
00023 #endif
00024 #ifndef ROOT_TMatrixD
00025 #include "TMatrixD.h"
00026 #endif
00027 #ifndef ROOT_TNeuron
00028 #include "TNeuron.h"
00029 #endif
00030 
00031 class TTree;
00032 class TEventList;
00033 class TTreeFormula;
00034 class TTreeFormulaManager;
00035 
00036 //____________________________________________________________________
00037 //
00038 // TMultiLayerPerceptron
00039 //
00040 // This class decribes a Neural network.
00041 // There are facilities to train the network and use the output.
00042 //
00043 // The input layer is made of inactive neurons (returning the
00044 // normalized input), hidden layers are made of sigmoids and output
00045 // neurons are linear.
00046 //
00047 // The basic input is a TTree and two (training and test) TEventLists.
00048 // For classification jobs, a branch (maybe in a TFriend) must contain
00049 // the expected output.
00050 // 6 learning methods are available: kStochastic, kBatch,
00051 // kSteepestDescent, kRibierePolak, kFletcherReeves and kBFGS.
00052 //
00053 // This implementation is *inspired* from the mlpfit package from
00054 // J.Schwindling et al.
00055 //
00056 //____________________________________________________________________
00057 
00058 class TMultiLayerPerceptron : public TObject {
00059  friend class TMLPAnalyzer;
00060 
00061  public:
00062    enum ELearningMethod { kStochastic, kBatch, kSteepestDescent,
00063                           kRibierePolak, kFletcherReeves, kBFGS };
00064    enum EDataSet { kTraining, kTest };
00065    TMultiLayerPerceptron();
00066    TMultiLayerPerceptron(const char* layout, TTree* data = 0,
00067                          const char* training = "Entry$%2==0",
00068                          const char* test = "",
00069                          TNeuron::ENeuronType type = TNeuron::kSigmoid,
00070                          const char* extF = "", const char* extD  = "");
00071    TMultiLayerPerceptron(const char* layout,
00072                          const char* weight, TTree* data = 0,
00073                          const char* training = "Entry$%2==0",
00074                          const char* test = "",
00075                          TNeuron::ENeuronType type = TNeuron::kSigmoid,
00076                          const char* extF = "", const char* extD  = "");
00077    TMultiLayerPerceptron(const char* layout, TTree* data,
00078                          TEventList* training,
00079                          TEventList* test,
00080                          TNeuron::ENeuronType type = TNeuron::kSigmoid,
00081                          const char* extF = "", const char* extD  = "");
00082    TMultiLayerPerceptron(const char* layout,
00083                          const char* weight, TTree* data,
00084                          TEventList* training,
00085                          TEventList* test,
00086                          TNeuron::ENeuronType type = TNeuron::kSigmoid,
00087                          const char* extF = "", const char* extD  = "");
00088    virtual ~TMultiLayerPerceptron();
00089    void SetData(TTree*);
00090    void SetTrainingDataSet(TEventList* train);
00091    void SetTestDataSet(TEventList* test);
00092    void SetTrainingDataSet(const char* train);
00093    void SetTestDataSet(const char* test);
00094    void SetLearningMethod(TMultiLayerPerceptron::ELearningMethod method);
00095    void SetEventWeight(const char*);
00096    void Train(Int_t nEpoch, Option_t* option = "text", Double_t minE=0);
00097    Double_t Result(Int_t event, Int_t index = 0) const;
00098    Double_t GetError(Int_t event) const;
00099    Double_t GetError(TMultiLayerPerceptron::EDataSet set) const;
00100    void ComputeDEDw() const;
00101    void Randomize() const;
00102    void SetEta(Double_t eta);
00103    void SetEpsilon(Double_t eps);
00104    void SetDelta(Double_t delta);
00105    void SetEtaDecay(Double_t ed);
00106    void SetTau(Double_t tau);
00107    void SetReset(Int_t reset);
00108    inline Double_t GetEta()      const { return fEta; }
00109    inline Double_t GetEpsilon()  const { return fEpsilon; }
00110    inline Double_t GetDelta()    const { return fDelta; }
00111    inline Double_t GetEtaDecay() const { return fEtaDecay; }
00112    inline Double_t GetTau()      const { return fTau; }
00113    inline Int_t GetReset()       const { return fReset; }
00114    inline TString GetStructure() const { return fStructure; }
00115    inline TNeuron::ENeuronType GetType() const { return fType; }
00116    void DrawResult(Int_t index = 0, Option_t* option = "test") const;
00117    void DumpWeights(Option_t* filename = "-") const;
00118    void LoadWeights(Option_t* filename = "");
00119    Double_t Evaluate(Int_t index, Double_t* params) const;
00120    void Export(Option_t* filename = "NNfunction", Option_t* language = "C++") const;
00121    virtual void Draw(Option_t *option="");
00122 
00123  protected:
00124    void AttachData();
00125    void BuildNetwork();
00126    void GetEntry(Int_t) const;
00127    // it's a choice not to force learning function being const, even if possible
00128    void MLP_Stochastic(Double_t*);
00129    void MLP_Batch(Double_t*);
00130    Bool_t LineSearch(Double_t*, Double_t*);
00131    void SteepestDir(Double_t*);
00132    void ConjugateGradientsDir(Double_t*, Double_t);
00133    void SetGammaDelta(TMatrixD&, TMatrixD&, Double_t*);
00134    bool GetBFGSH(TMatrixD&, TMatrixD &, TMatrixD&);
00135    void BFGSDir(TMatrixD&, Double_t*);
00136    Double_t DerivDir(Double_t*);
00137    Double_t GetCrossEntropyBinary() const;
00138    Double_t GetCrossEntropy() const;
00139    Double_t GetSumSquareError() const;
00140 
00141  private:
00142    TMultiLayerPerceptron(const TMultiLayerPerceptron&); // Not implemented
00143    TMultiLayerPerceptron& operator=(const TMultiLayerPerceptron&); // Not implemented
00144    void ExpandStructure();
00145    void BuildFirstLayer(TString&);
00146    void BuildHiddenLayers(TString&);
00147    void BuildOneHiddenLayer(const TString& sNumNodes, Int_t& layer,
00148                             Int_t& prevStart, Int_t& prevStop,
00149                             Bool_t lastLayer);
00150    void BuildLastLayer(TString&, Int_t);
00151    void Shuffle(Int_t*, Int_t) const;
00152    void MLP_Line(Double_t*, Double_t*, Double_t);
00153 
00154    TTree* fData;                   //! pointer to the tree used as datasource
00155    Int_t fCurrentTree;             //! index of the current tree in a chain
00156    Double_t fCurrentTreeWeight;    //! weight of the current tree in a chain
00157    TObjArray fNetwork;             // Collection of all the neurons in the network
00158    TObjArray fFirstLayer;          // Collection of the input neurons; subset of fNetwork
00159    TObjArray fLastLayer;           // Collection of the output neurons; subset of fNetwork
00160    TObjArray fSynapses;            // Collection of all the synapses in the network
00161    TString fStructure;             // String containing the network structure
00162    TString fWeight;                // String containing the event weight
00163    TNeuron::ENeuronType fType;     // Type of hidden neurons
00164    TNeuron::ENeuronType fOutType;  // Type of output neurons
00165    TString fextF;                  // String containing the function name
00166    TString fextD;                  // String containing the derivative name
00167    TEventList *fTraining;          //! EventList defining the events in the training dataset
00168    TEventList *fTest;              //! EventList defining the events in the test dataset
00169    ELearningMethod fLearningMethod; //! The Learning Method
00170    TTreeFormula* fEventWeight;     //! formula representing the event weight
00171    TTreeFormulaManager* fManager;  //! TTreeFormulaManager for the weight and neurons
00172    Double_t fEta;                  //! Eta - used in stochastic minimisation - Default=0.1
00173    Double_t fEpsilon;              //! Epsilon - used in stochastic minimisation - Default=0.
00174    Double_t fDelta;                //! Delta - used in stochastic minimisation - Default=0.
00175    Double_t fEtaDecay;             //! EtaDecay - Eta *= EtaDecay at each epoch - Default=1.
00176    Double_t fTau;                  //! Tau - used in line search - Default=3.
00177    Double_t fLastAlpha;            //! internal parameter used in line search
00178    Int_t fReset;                   //! number of epochs between two resets of the search direction to the steepest descent - Default=50
00179    Bool_t fTrainingOwner;          //! internal flag whether one has to delete fTraining or not
00180    Bool_t fTestOwner;              //! internal flag whether one has to delete fTest or not
00181    ClassDef(TMultiLayerPerceptron, 4)   // a Neural Network
00182 };
00183 
00184 #endif

Generated on Tue Jul 5 14:27:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1