GPLIB++
NeuralNetwork.h
Go to the documentation of this file.
1 #ifndef CNEURALNETWORK_H
2 #define CNEURALNETWORK_H
3 #include <vector>
4 #include <iostream>
5 #include <boost/shared_ptr.hpp>
6 #include "SigmoidalNeuron.h"
7 #include "InputNeuron.h"
8 #include "AdaptiveFilter.h"
9 
10 namespace gplib
11  {
12  /** \addtogroup neuralnet Neural Network filtering */
13  /* @{ */
14 
15  //! The class NeuralNetwork manages the network output calculation, neuron storage and weight adaptation
16  //! Derived from AdaptiveFilter so we can use the Filter functionality
18  {
19  public:
20  typedef std::vector<boost::shared_ptr<GeneralNeuron> > tNeuralLayer;
21  typedef std::vector<tNeuralLayer> tNeuralArray;
22  typedef std::vector<SigmoidalNeuron::tneurontype> ttypeVector;
23  typedef std::vector<ttypeVector> ttypeArray;
24  private:
25  //! Calculate the output for the whole network
26  std::vector<double> &CalcOutput();
27  //! In some cases (plotting etc.) we want all the network weights as a single vector
28  gplib::rvec WeightsAsVector;
29  //! The multiplier for the momentum term
30  double alpha;
31  //! The adaptation stepsize
32  double mu;
33  //! The storage of the individual neurons
34  tNeuralArray Layers;
35  //! The last input to the network, this is the place the input neurons point to
36  std::vector<double> LocInput;
37  //! The output of the network
38  std::vector<double> LocOutput;
39  //! The reference values for the current iteration
40  std::vector<double> LocDesired;
41  //! Adapt the network weights given the current values
42  void AdaptWeights();
43  public:
44  //! Set the momentum multiplier
45  void SetAlpha(const double a)
46  {
47  alpha = a;
48  }
49  //! Set the adaptation stepsize
50  void SetMu(const double m)
51  {
52  mu = m;
53  }
54  //! Configure the layers of the network according to the types in typeArray
55  void SetLayers(ttypeArray typeArray, bool cachedoutput = false);
56  //! Initialize the weights with random values with the specified maxima
57  void InitWeights(const double MaxWeight, const double MaxBias);
58  //! Print the topology and weights of the network for plotting with the dot program
59  void PrintTopology(std::string filename);
60  //! Print the weights of the network to the specified output stream
61  virtual void PrintWeights(std::ostream &output);
62  //! Return the network weights as a single vector
63  virtual const gplib::rvec &GetWeightsAsVector();
64  //! Adapt the Filter with the current input and desired
65  virtual void AdaptFilter(const gplib::rvec &Input,
66  const gplib::rvec &Desired);
67  //! Calculate the output with the given input
68  virtual void CalcOutput(const gplib::rvec &Input, gplib::rvec &Output);
69  //! The minium values for the network are the length of the input and output
70  NeuralNetwork(const int inputsize, const int outputsize);
71  //! Extended constructor with most of the necessary values
72  NeuralNetwork(const int inputsize, const int outputsize,
73  const double mu_, const ttypeArray &Layerssetup,
74  const double maxinit, bool cachedoutput = false);
75  virtual ~NeuralNetwork();
76  };
77  /* @} */
78  }
79 #endif // CNEURALNETWORK_H
void InitWeights(const double MaxWeight, const double MaxBias)
Initialize the weights with random values with the specified maxima.
std::vector< boost::shared_ptr< GeneralNeuron > > tNeuralLayer
Definition: NeuralNetwork.h:20
void SetLayers(ttypeArray typeArray, bool cachedoutput=false)
Configure the layers of the network according to the types in typeArray.
virtual void PrintWeights(std::ostream &output)
Print the weights of the network to the specified output stream.
void SetAlpha(const double a)
Set the momentum multiplier.
Definition: NeuralNetwork.h:45
std::vector< ttypeVector > ttypeArray
Definition: NeuralNetwork.h:23
virtual void AdaptFilter(const gplib::rvec &Input, const gplib::rvec &Desired)
Adapt the Filter with the current input and desired.
void SetMu(const double m)
Set the adaptation stepsize.
Definition: NeuralNetwork.h:50
A generic base class for all types of adaptive filters.
std::vector< tNeuralLayer > tNeuralArray
Definition: NeuralNetwork.h:21
std::vector< SigmoidalNeuron::tneurontype > ttypeVector
Definition: NeuralNetwork.h:22
NeuralNetwork(const int inputsize, const int outputsize)
The minium values for the network are the length of the input and output.
virtual const gplib::rvec & GetWeightsAsVector()
Return the network weights as a single vector.
void PrintTopology(std::string filename)
Print the topology and weights of the network for plotting with the dot program.