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     // first some global constants that can be changed to play with
00014     const int seglength = 2; // the length of the segment we want to work on
00015     const double maxinit = 0.01; // the maximum the neuron weights are initialized to
00016     const double maxbias = 0.01; // the maximum bias for each neuron
00017     const int hiddenlayers = 1; // the number of hiddenlayers
00018     const double mu = 1; // the stepsize for weight adaptation for each neuron;
00019     const int trainsamples = 20;
00020     const int testsamples = 20;
00021     const int gap = trainsamples / 10;
00022 
00023     int currentindex = 0;
00024     ttypeVector typeVector; // a vector that contains the type for each neuron in a layer
00025     ttypeArray typeArray; // an array of typevectors that contains the neuron type for the whole network;
00026 
00027     UniformRNG Random;
00028 
00029     double currentinput, currentref; // the current input and reference values we're working on
00030 
00031     string logfilename("learn.log");
00032     ofstream logfile(logfilename.c_str());
00033 
00034     typeVector.assign(4 * seglength, bipolar); // we want 4* seglength number of bipolar neurons per hidden layer
00035     for (int i = 0; i < hiddenlayers; ++i) //intialize the type array for the hidden layers
00036       {
00037         typeArray.push_back(typeVector); // all layers are the same, so we copy the same vector there
00038       }
00039     typeVector.assign(1, identity);
00040     typeArray.push_back(typeVector); // and then we add it to the type Array
00041 
00042     NeuralNetwork Network; //We declare a new variable for the network
00043     Network.Input.assign(seglength, 0); // We have to allocate memory for the network input and set it to zero
00044     Network.SetLayers(typeArray); // We use the type array we created before to setup the neurons in the Network
00045     Network.InitWeights(maxinit, maxbias); // The Weights are initialized with the two maximum values
00046     Network.mu = mu; // The mu value is copied to the network properties
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         //Network.AdaptWeights();
00098         logfile << i << " " << desired << " " << Network.Output.at(0) << " "
00099             << desired - Network.Output.at(0) << endl;
00100       }
00101 
00102   }

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