NeuralNetwork.h

Go to the documentation of this file.
00001 #ifndef CNEURALNETWORK_H
00002 #define CNEURALNETWORK_H
00003 #include <vector>
00004 #include <iostream>
00005 #include <boost/shared_ptr.hpp>
00006 #include "SigmoidalNeuron.h"
00007 #include "InputNeuron.h"
00008 #include "AdaptiveFilter.h"
00009 
00010 namespace gplib
00011   {
00012     /** \addtogroup neuralnet Neural Network filtering */
00013     /* @{ */
00014 
00015     //! The class NeuralNetwork manages the network output calculation, neuron storage and weight adaptation
00016     //! Derived from AdaptiveFilter so we can use the Filter functionality
00017     class NeuralNetwork: public AdaptiveFilter
00018       {
00019     public:
00020       typedef std::vector<boost::shared_ptr<GeneralNeuron> > tNeuralLayer;
00021       typedef std::vector<tNeuralLayer> tNeuralArray;
00022       typedef std::vector<SigmoidalNeuron::tneurontype> ttypeVector;
00023       typedef std::vector<ttypeVector> ttypeArray;
00024     private:
00025       //! Calculate the output for the whole network
00026       std::vector<double> &CalcOutput();
00027       //! In some cases (plotting etc.) we want all the network weights as a single vector
00028       gplib::rvec WeightsAsVector;
00029       //! The multiplier for the momentum term
00030       double alpha;
00031       //! The adaptation stepsize
00032       double mu;
00033       //! The storage of the individual neurons
00034       tNeuralArray Layers;
00035       //! The last input to the network, this is the place the input neurons point to
00036       std::vector<double> LocInput;
00037       //! The output of the network
00038       std::vector<double> LocOutput;
00039       //! The reference values for the current iteration
00040       std::vector<double> LocDesired;
00041       //! Adapt the network weights given the current values
00042       void AdaptWeights();
00043     public:
00044       //! Set the momentum multiplier
00045       void SetAlpha(const double a)
00046         {
00047           alpha = a;
00048         }
00049       //! Set the adaptation stepsize
00050       void SetMu(const double m)
00051         {
00052           mu = m;
00053         }
00054       //! Configure the layers of the network according to the types in typeArray
00055       void SetLayers(ttypeArray typeArray, bool cachedoutput = false);
00056       //! Initialize the weights with random values with the specified maxima
00057       void InitWeights(const double MaxWeight, const double MaxBias);
00058       //! Print the topology and weights of the network for plotting with the dot program
00059       void PrintTopology(std::string filename);
00060       //! Print the weights of the network to the specified output stream
00061       virtual void PrintWeights(std::ostream &output);
00062       //! Return the network weights as a single vector
00063       virtual const gplib::rvec &GetWeightsAsVector();
00064       //! Adapt the Filter with the current input and desired
00065       virtual void AdaptFilter(const gplib::rvec &Input,
00066           const gplib::rvec &Desired);
00067       //! Calculate the output with the given input
00068       virtual void CalcOutput(const gplib::rvec &Input, gplib::rvec &Output);
00069       //! The minium values for the network are the length of the input and output
00070       NeuralNetwork(const int inputsize, const int outputsize);
00071       //! Extended constructor with most of the necessary values
00072       NeuralNetwork(const int inputsize, const int outputsize,
00073           const double mu_, const ttypeArray &Layerssetup,
00074           const double maxinit, bool cachedoutput = false);
00075       virtual ~NeuralNetwork();
00076       };
00077   /* @} */
00078   }
00079 #endif // CNEURALNETWORK_H

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8