NeuralNetwork.h

00001 /*
00002  *====================================================================
00003  * HISTORY:
00004  * -------
00005  * $Log: NeuralNetwork.h,v $
00006  * Revision 1.1  2008/08/23 14:28:48  samit_ahlawat
00007  * Feed forward neural network library
00008  *
00009  * Revision 1.1  2007/11/06 10:06:30  samit_ahlawat
00010  * Checking in first version of basic neural net infrastructure
00011  *
00012  * 
00013  * ID:
00014  * --
00015  * static const char *CvsId = "@(#)$Id: NeuralNetwork.h,v 1.1 2008/08/23 14:28:48 samit_ahlawat Exp $";
00016  *====================================================================
00017  */
00018 
00019 #ifndef _AIMAGIK_NEURAL_NETWORK_H_
00020 #define _AIMAGIK_NEURAL_NETWORK_H_
00021 
00022 #include <vector>
00023 #include "Neuron.h"
00024 #include "NeuralNetworkBuilder.h"
00025 #include <iostream>
00026 #include "TestData.h"
00027 
00028 
00036 struct NeuronConnection
00037 {
00038     int* pNeurons;
00039 
00040     NeuronConnection() : pNeurons(NULL) {}
00041     NeuronConnection(int num) : pNeurons((int*) new int[num]) {}
00042     void SetNumNeurons(int num) { pNeurons = (int*) new int[num]; }
00043 
00044     ~NeuronConnection() { if (pNeurons) delete[] pNeurons; pNeurons = NULL; }
00045 };
00046 
00047 
00071 class NeuralNetwork
00072 {
00073         private:
00074         // This is the neuron network
00075                 std::vector<Neuron*>* pNetworkVec;
00076         // vector containing number of neurons at each level
00077                 std::vector<int>* pLevelVec;
00085         std::vector<NeuronConnection*>* pConnectionVec;
00089                 NeuralNetworkBuilder mBuilder;
00090         /* vector containing input to the network */
00091         std::vector<double>* pInputVec;
00092         /* vector containing activation (response) of different neurons
00093          * to provided input. The activations of neurons are needed, they
00094          * are computed once and stored in this vector, one activation
00095          * for each neuron. */
00096         std::vector<double>* pActivationVec;
00097 
00099         TestData* pData;
00100 
00101         public:
00102                 NeuralNetwork();
00103                 NeuralNetwork(const NeuralNetwork& network);
00104                 NeuralNetwork& operator= (const NeuralNetwork& network);
00105 
00109         void ConstructNetworkFromFile(const char* file) throw (std::invalid_argument);
00110 
00111         void DeserializeData(std::istream& strm) { mBuilder.DeserializeData(strm); }
00112 
00115                 void AddNeuronLevel(int numNeurons);
00117                 Neuron* GetNeuron(int indx) { return (*pNetworkVec)[indx]; }
00118 
00119                 int GetNumNeurons() const { return pNetworkVec->size(); }
00120 
00121         int GetNumInputNeurons() const { return pLevelVec->front(); }
00122 
00123         int GetNumOutputNeurons() const { return pLevelVec->back(); }
00124 
00125         int GetNumLevels() const { return pLevelVec->size(); }
00126 
00127         int GetNumNeuronsAtLevel(int level) const { return (*pLevelVec)[level]; }
00128 
00129         TestData* GetTestData() { return pData; }
00130 
00131         void AddData(std::vector<double>& input, std::vector<double>& output, int index) throw (std::logic_error);
00132 
00139         void DefineTrainingData(int inFeatures, int outFeatures, int numSamples);
00140 
00150         void AddSampleData(std::vector<double>& input, std::vector<double>& output, int index) throw (std::logic_error);
00151 
00152         /* Get a list of posterior neuron indices that connect (send
00153          * an input to) this neuron. */
00154         int* GetConnectingNeurons(int indx) const { return (*pConnectionVec)[indx]->pNeurons; }
00155 
00167         void ConnectNeuron(int neuronIndex, const std::vector<double>& weightVec, const std::vector<int>& neuronFromVec);
00168 
00169 
00170                 const NeuralNetworkBuilder& GetBuilder() const { return mBuilder; }
00171         
00182                 void FeedInput(const std::vector<double>& input, std::vector<double>& output);
00183 
00199                 void FeedInput(const double* input, double* output);        
00200 
00227                 void FeedInput(const double* input, double* output, double* net_input);        
00228 
00233                 bool DetectCycle();
00234 
00235         /* Calibrate the weights of feed forward neural network
00236          * using gradient descent approach.
00237          * @param
00238          * eeta: learning parameter
00239          * maxIter: maximum number of iterations (grad descent steps)
00240          * minResidual: Stop if the residual falls below this
00241          *              threshold residual
00242          * finalResidual: This is an output variable that holds 
00243          * the residual at the final iteration.
00244          * @return
00245          * true if the solution converged (residual fell below the
00246          * specified threshold). False otherwise.
00247          */
00248         bool CalibrateWeights(double eeta, int maxIter, double minResidual, double& finalResidual);
00249 
00250                 void ClearNetwork();
00251                 ~NeuralNetwork();
00252 };
00253 
00254 std::ostream& operator<< (std::ostream& os, const NeuralNetwork& network);
00255 std::istream& operator>> (std::istream& is, NeuralNetwork& network);
00256 
00257 
00258 #endif // _AIMAGIK_NEURAL_NETWORK_H_
00259 

Generated on Sun Aug 24 17:33:41 2008 by  doxygen 1.4.6