learntest.cpp
Go to the documentation of this file.00001 #include "NeuralNetwork.h"
00002 #include <iostream>
00003 #include <vector>
00004 #include <fstream>
00005 #include <string>
00006 #include "UniformRNG.h"
00007
00008 using namespace std;
00009 using namespace gplib;
00010
00011 int main()
00012 {
00013
00014 const int seglength = 2;
00015 const double maxinit = 0.01;
00016 const double maxbias = 0.01;
00017 const int hiddenlayers = 1;
00018 const double mu = 1;
00019 const int trainsamples = 20;
00020 const int testsamples = 20;
00021 const int gap = trainsamples / 10;
00022
00023 int currentindex = 0;
00024 ttypeVector typeVector;
00025 ttypeArray typeArray;
00026
00027 UniformRNG Random;
00028
00029 double currentinput, currentref;
00030
00031 string logfilename("learn.log");
00032 ofstream logfile(logfilename.c_str());
00033
00034 typeVector.assign(4 * seglength, bipolar);
00035 for (int i = 0; i < hiddenlayers; ++i)
00036 {
00037 typeArray.push_back(typeVector);
00038 }
00039 typeVector.assign(1, identity);
00040 typeArray.push_back(typeVector);
00041
00042 NeuralNetwork Network;
00043 Network.Input.assign(seglength, 0);
00044 Network.SetLayers(typeArray);
00045 Network.InitWeights(maxinit, maxbias);
00046 Network.mu = mu;
00047
00048
00049 for (int i = 0; i < trainsamples; ++i)
00050 {
00051 double input1 = (0.5 - Random.GetNumber());
00052 double input2 = (0.5 - Random.GetNumber());
00053 double desired = exp(input1 + input2);
00054 Network.Input.at(0) = input1;
00055 Network.Input.at(1) = input2;
00056 Network.Desired.front() = desired;
00057 Network.AdaptWeights();
00058 Network.CalcOutput();
00059 for (int j = 0; j < typeArray.size(); ++j)
00060 {
00061 cout << "Layer: " << j << endl;
00062 for (int k = 0; k < typeArray.at(j).size(); ++k)
00063 {
00064 cout << "Neuron: " << k << endl;
00065 cout << "Delta: " << Network.Layers.at(j).at(k)->Delta << endl;
00066 cout << "Net: " << Network.Layers.at(j).at(k)->Net << endl;
00067 cout << "Output: " << Network.Layers.at(j).at(k)->Output
00068 << endl;
00069 }
00070 cout << endl;
00071 }
00072 logfile << i << " " << desired << " " << Network.Output.at(0) << " "
00073 << desired - Network.Output.at(0) << endl;
00074 }
00075
00076 for (int i = trainsamples + gap; i < trainsamples + gap + testsamples; ++i)
00077 {
00078 double input1 = (0.5 - Random.GetNumber());
00079 double input2 = (0.5 - Random.GetNumber());
00080 double desired = input1 + input2;
00081 Network.Input.at(0) = input1;
00082 Network.Input.at(1) = input2;
00083 Network.CalcOutput();
00084 for (int j = 0; j < typeArray.size(); ++j)
00085 {
00086 cout << "Layer: " << j << endl;
00087 for (int k = 0; k < typeArray.at(j).size(); ++k)
00088 {
00089 cout << "Neuron: " << k << endl;
00090 cout << "Delta: " << Network.Layers.at(j).at(k)->Delta << endl;
00091 cout << "Net: " << Network.Layers.at(j).at(k)->Net << endl;
00092 cout << "Output: " << Network.Layers.at(j).at(k)->Output
00093 << endl;
00094 }
00095 cout << endl;
00096 }
00097
00098 logfile << i << " " << desired << " " << Network.Output.at(0) << " "
00099 << desired - Network.Output.at(0) << endl;
00100 }
00101
00102 }