LCFIVertex  0.7.2
BackPropagationCGAlgorithm.h
1 #ifndef BACKPROPAGATIONCGALGORITHM_H
2 #define BACKPROPAGATIONCGALGORITHM_H
3 
4 #include "NeuralNetConfig.h"
5 
6 #include "NeuralNet.h"
7 #include "NeuralNetDataSet.h"
8 
9 #include <vector>
10 
11 #ifdef __CINT__
12 #include "InputNormaliser.h"
13 #else
14 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
15 namespace nnet
16 {
17 class InputNormaliser;
18 }
19 #endif
20 
21 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
22 namespace nnet
23 {
24 
25 class
26 #ifndef __CINT__
27 NEURALNETDLL
28 #endif
30 {
31 public:
32  typedef enum { FletcherReves, PolakRibiere, ConjugateDescent } BetaFunctionSelect;
33 
34 public:
37  double train(const int numberOfEpochs,const NeuralNetDataSet &dataSet,
38  const NeuralNet::InputNormalisationSelect normaliseTrainingData=NeuralNet::PassthroughNormalised);
39  double train(const int numberOfEpochs,const NeuralNetDataSet &dataSet,const std::vector<InputNormaliser *> &inputNormalisers);
40  void setBetaFunction(BetaFunctionSelect theFunction);
41  void setLinearSearchInitialStepLength(const double stepLength) {_linearSearchStepLength = stepLength;}
42  void setLinearSearchMu(const double mu) {_linearSearchMu = mu;}
43  void setLinearSearchSigma(const double sigma) {_linearSearchSigma = sigma;}
44  void setLinearSearchGamma(const double gamma) {_linearSearchGamma = gamma;}
45  void setLinearSearchMaxIterations(const int maxIterations) {_linearSearchMaxIterations = maxIterations;}
46  void setLinearSearchAbsGradientCutoff(const double cutoff) {_linearSearchAbsGradientCutoff = cutoff;}
47  void setEpochsBeforeGradientReset(const int numberOfEpochs) {_epochsBeforeGradientReset = numberOfEpochs;}
48  void setProgressPrintoutFrequency(const int frequency) {_progressPrintoutFrequency = frequency;}
49  std::vector<double> getTrainingErrorValuesPerEpoch() const {return _savedEpochErrorValues;}
50 
51 protected:
52  double trainWithDataSet(const int numberOfEpochs);
53  std::vector<double> layerOutput(const int layer) const;
54  void calculateLayerOutputs();
55  void calculateDerivativeOutputs();
56  void calculateErrorSignals();
57  void calculateDeDw();
58  void calculateRunningDeDw();
59  double error();
60  double newEpoch(bool &success,double &gradient);
61  double processDataSet();
62  double beta(const std::vector<double> &gk,const std::vector<double> &gkplus1,const std::vector<double> &dk);
63  double betaFR(const std::vector<double> &gk,const std::vector<double> &gkplus1);
64  double betaPR(const std::vector<double> &gk,const std::vector<double> &gkplus1);
65  double betaCD(const std::vector<double> &gk,const std::vector<double> &gkplus1,const std::vector<double> &dk);
66  double alpha(const std::vector<double> &x,const std::vector<double> &p,const std::vector<double> &g,const double F0,bool &converged);
67 
68 private:
69  typedef std::vector<std::vector<double> > NetMatrix;
70 
71 private:
72  NeuralNet &_theNetwork;
73  BetaFunctionSelect _theBetaFunction;
74  const std::vector<double> *_inputs,*_target;
75  NetMatrix _neuronErrorSignals;
76  NetMatrix _neuronOutputs;
77  NetMatrix _neuronDerivativeOutputs;
78  NetMatrix _dEdw;
79  const NeuralNetDataSet *_currentDataSet;
80  double _previousEpochError;
81  double _runningEpochErrorTotal;
82  int _numberOfTrainingEvents;
83  std::vector<double> _runningDeDwSum;
84  std::vector<double> _previousEpochDeDw;
85  double _linearSearchStepLength;
86  std::vector<double> _currentSearchDirection;
87  double _linearSearchMu;
88  double _linearSearchSigma;
89  double _linearSearchGamma;
90  double _linearSearchTolerance;
91  int _linearSearchMaxIterations;
92  int _epochsBeforeGradientReset;
93  int _numberOfEpochs;
94  int _progressPrintoutFrequency;
95  std::vector<double> _savedEpochErrorValues;
96  double _linearSearchAbsGradientCutoff;
97  double _previousEpochStepLength;
98  std::vector<double> _previousEpochDeltaWeights;
99 };
100 
101 }//namespace nnet
102 
103 #endif