00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00075 std::vector<Neuron*>* pNetworkVec;
00076
00077 std::vector<int>* pLevelVec;
00085 std::vector<NeuronConnection*>* pConnectionVec;
00089 NeuralNetworkBuilder mBuilder;
00090
00091 std::vector<double>* pInputVec;
00092
00093
00094
00095
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
00153
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
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
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