simplenet.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 "CUniformRNG.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 = 100; // 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 maxit = 1000; // maximum number of iterations = maximum output length
00018     const int hiddenlayers = 2; // the number of hiddenlayers
00019     const double mu = 0.1; // the stepsize for weight adaptation for each neuron;
00020 
00021     ttypeVector typeVector; // a vector that contains the type for each neuron in a layer
00022     ttypeArray typeArray; // an array of typevectors that contains the neuron type for the whole network;
00023     ifstream inputfile, reffile; // the file objects for the two inputfiles: the noise time series and the reference
00024     ofstream outputfile, epsfile; // the file objects for the two outputfiles: the cleaned file and the difference to the original
00025 
00026     double currentinput, currentref; // the current input and reference values we're working on
00027 
00028     string inputfilename, reffilename; // the strings containing the filenames
00029     string outputfilename, epsfilename;
00030 
00031     cout << "Inputfile: ";
00032     cin >> inputfilename; // read in input filename
00033     cout << "Referencefile:";
00034     cin >> reffilename; // read in reference filename
00035     outputfilename = inputfilename + ".clean"; // set outputfilename for clean file
00036     epsfilename = inputfilename + ".eps"; // and for difference file
00037     inputfile.open(inputfilename.c_str()); // open the files for reading
00038     reffile.open(reffilename.c_str()); // reading
00039     epsfile.open(epsfilename.c_str()); // writing
00040     outputfile.open(outputfilename.c_str()); // writing
00041 
00042     typeVector.assign(seglength, bipolar); // we want seglength number of bipolar neurons per hidden layer
00043     for (int i = 0; i < hiddenlayers; ++i) //intialize the type array for the hidden layers
00044       {
00045         typeArray.push_back(typeVector); // all layers are the same, so we copy the same vector there
00046       }
00047     // We want to combine all values into one output values, also it's range should not be restricted
00048     typeVector.assign(1, identity); // the last layer therefore contains a single neuron with the identity activation function
00049     typeArray.push_back(typeVector); // and then we add it to the type Array
00050 
00051     NeuralNetwork Network; //We declare a new variable for the network
00052     Network.Input.assign(seglength, 0); // We have to allocate memory for the network input and set it to zero
00053     Network.SetLayers(typeArray); // We use the type array we created before to setup the neurons in the Network
00054     Network.InitWeights(maxinit, maxbias); // The Weights are initialized with the two maximum values
00055     Network.mu = mu; // The mu value is copied to the network properties
00056     for (int i = 0; i < seglength; ++i) // We read in the first segment of data
00057       {
00058         inputfile >> currentinput; // read from file into variable
00059         Network.Input.at(i) = currentinput; // copy same value to Network input vector
00060       }
00061     reffile >> currentref;
00062     Network.Desired.at(0) = currentref; //For the reference we only need a single value, we take the beginning of the segment (can be changed)
00063     Network.CalcOutput(); // We calculate the output for the first segment
00064     Network.AdaptWeights(); // and adapt the weights
00065     // this is done outside the first loop, because we don't want to output this value
00066 
00067     for (int iterations = 0; iterations < maxit; ++iterations) // now we do the remaining time seriesup to maxit
00068       {
00069 
00070         inputfile >> currentinput; // read in a new input value
00071         reffile >> currentref; // and reference value
00072 
00073         rotate(Network.Input.begin(), Network.Input.begin() + 1,
00074             Network.Input.end()); // shift all elements so that the second becomes the first
00075         Network.Input.back() = currentinput; // and copy the new values to the last position
00076 
00077         Network.Desired.at(0) = currentref; // again set the reference
00078         Network.CalcOutput(); // and calculate output
00079         Network.AdaptWeights(); // and adapt weights
00080 
00081         outputfile << Network.Output.at(0) << endl; // finally write calculated output to file
00082         epsfile << (Network.Desired.at(0) - Network.Output.at(0)) << endl; // calculated difference and write to file as well
00083       }
00084     inputfile.close();
00085     reffile.close();
00086     outputfile.close();
00087     epsfile.close();
00088 
00089   }

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