LCFIVertex  0.7.2
NeuralNet.h
1 #ifndef NEURALNET_H
2 #define NEURALNET_H
3 
4 #include "NeuralNetConfig.h"
5 
6 #include <vector>
7 #include <iostream>
8 #include <string>
9 #include <utility>
10 
11 #ifdef __CINT__
12 #include "NeuronLayer.h"
13 #include "NeuronBuilder.h"
14 #include "InputNormaliser.h"
15 #else
16 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
18 namespace nnet
19 {
20 class NeuronLayer;
21 class NeuronBuilder;
22 class InputNormaliser;
23 }
24 #endif
25 
26 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
27 namespace nnet
28 {
29 
30 class
31 #ifndef __CINT__
32 NEURALNETDLL
33 #endif
35 {
36 public:
37  typedef enum {PassthroughNormalised,GaussianNormalised} InputNormalisationSelect;
38  typedef enum {XML,PlainText} SerialisationMode;
39 
40 public:
41  NeuralNet(const int numberOfInputs,const std::vector<int> &numberOfNeuronsPerLayer,NeuronBuilder *theNeuronBuilder,bool initialiseRandomSeed=true);
42  NeuralNet(const int numberOfInputs,const std::vector<std::vector<std::string> > &namedNeuronsPerLayer,bool initialiseRandomSeed=true);
43  NeuralNet(const NeuralNet &other);
44  NeuralNet(const std::string &xmlfile,std::vector<NeuronBuilder *> &theNeuronBuilders,const SerialisationMode readMode=XML);
45  NeuralNet(const char *xmlfile,std::vector<NeuronBuilder *> &theNeuronBuilders,const SerialisationMode readMode=XML);
46  NeuralNet(const std::string &xmlfile,const SerialisationMode readMode=XML);
47  NeuralNet(const char *xmlfile,const SerialisationMode readMode=XML);
48  ~NeuralNet(void);
49  void serialise(std::ostream &os) const;
50  std::vector<double> output(const std::vector<double> &inputValues) const;
51  int numberOfWeights() const;
52  int numberOfLayers() const {return _numberOfLayers;}
53  int numberOfInputs() const {return _numberOfInputs;}
54  std::vector<double> weights() const;
55  void setWeights(const std::vector<double> &newWeights);
56  NeuronLayer *layer(const int i) const;
57  void setTargetNormalisationOffsets(const std::vector<double> &offsets);
58  void setTargetNormalisationRanges(const std::vector<double> &ranges);
59  std::vector<double> targetNormalisationOffsets() const {return _targetNormalisationOffsets;}
60  std::vector<double> targetNormalisationRanges() const {return _targetNormalisationRanges;}
61  std::vector<std::pair<double,double> > networkOutputRange() const;
62  void setSerialisationPrecision(const int precision) {_serialisationPrecision = precision;}
63  int getSerialisationPrecision() const {return _serialisationPrecision;}
64  void setInputNormalisers(const std::vector<InputNormaliser *> &theNormalisers);
65  std::vector<InputNormaliser *> inputNormalisers() const {return _inputNormalisers;}
66  SerialisationMode getSerialisationMode() const {return _serialisationMode;}
67  void setSerialisationMode(const SerialisationMode &mode) {_serialisationMode = mode;}
68 
69 protected:
70  void constructLayers(const int numberOfInputs,const int numberOfLayers,const std::vector<int> &numberOfNeuronsPerLayer,bool initialiseRandomSeed);
71  void constructLayers(const std::vector<std::vector<std::string> > &namedLayers,bool initialiseRandomSeed);
72  void clear();
73  void buildFromUrl(const std::string &url,const std::vector<NeuronBuilder *> &theNeuronBuilders);
74  void buildFromUrl(const std::string &url);
75  void buildFromXML(const std::string &url,const std::vector<NeuronBuilder *> &theNeuronBuilders);
76  void buildFromXML(const std::string &url);
77  void buildFromPlainText(const std::string &url,const std::vector<NeuronBuilder *> &theNeuronBuilders);
78  void buildFromPlainText(const std::string &url);
79 
80 private:
81  int _numberOfLayers;
82  int _numberOfInputs;
83  std::vector<NeuronLayer *> _theLayers;
84  const NeuronBuilder *_theNeuronBuilder;
85  std::vector<double> _targetNormalisationOffsets;
86  std::vector<double> _targetNormalisationRanges;
87  std::vector<InputNormaliser *> _inputNormalisers;
88  SerialisationMode _serialisationMode;
89 #ifdef __CINT__
90  int
91 #else
92  std::streamsize
93 #endif
94  _serialisationPrecision;
95 };
96 
97 // Non member stream output operator
98 #ifndef __CINT__
99 NEURALNETDLL
100 #endif
101 std::ostream &operator<<(std::ostream &os,const NeuralNet &nn);
102 
103 }//namespace nnet
104 
105 #endif