seismicnet.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 using namespace std;
00008 
00009 int main()
00010 {
00011         // first some global constants that can be changed to play with
00012         const int seglength = 3; // the length of the segment we want to work on
00013         const double maxinit = 0.01; // the maximum the neuron weights are initialized to
00014         const double maxbias = 0.01; // the maximum bias for each neuron
00015         const int hiddenlayers = 2; // the number of hiddenlayers
00016         const double mu = 0.1; // the stepsize for weight adaptation for each neuron;
00017         const int noiseseglength = 70000;
00018         const int plotintervall = 200;
00019         const double noisepower = 10;
00020         int currentindex = 0;
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 datafile, noisefile; // the file objects for the two inputfiles: the noise time series and the reference 
00024         ofstream outputfile,epsfile,weightfile; // the file objects for the two outputfiles: the cleaned file and the difference to the original
00025         UniformRNG Random;
00026         
00027         double currentinput, currentref; // the current input and reference values we're working on
00028         
00029         string datafilename, noisefilename; // the strings containing the filenames 
00030         string outputfilename, epsfilename, weightfilename;
00031         
00032         
00033         //cout << "Datafile:";
00034         //cin >> datafilename; // read in reference filename
00035         cout << "Datafile:";
00036         cin >> noisefilename; 
00037         outputfilename = noisefilename+".clean"; // set outputfilename for clean file
00038         epsfilename = noisefilename+".eps"; // and for difference file
00039         weightfilename = noisefilename+".weight";
00040         noisefile.open(noisefilename.c_str()); // reading
00041         //datafile.open(datafilename.c_str()); // reading
00042         epsfile.open(epsfilename.c_str()); // writing
00043         weightfile.open(weightfilename.c_str()); 
00044         outputfile.open(outputfilename.c_str()); // writing
00045         
00046         typeVector.assign(seglength,bipolar); // we want seglength number of bipolar neurons per hidden layer 
00047         for (int i = 0; i < hiddenlayers; ++i) //intialize the type array for the hidden layers
00048         {
00049                 typeArray.push_back(typeVector); // all layers are the same, so we copy the same vector there
00050         }
00051         typeVector.assign(1,identity); 
00052         typeArray.push_back(typeVector); // and then we add it to the type Array
00053         
00054         NeuralNetwork Network; //We declare a new variable for the network
00055         Network.Input.assign(seglength,0); // We have to allocate memory for the network input and set it to zero
00056         Network.SetLayers(typeArray); // We use the type array we created before to setup the neurons in the Network
00057         Network.InitWeights(maxinit,maxbias); // The Weights are initialized with the two maximum values
00058         Network.mu = mu; // The mu value is copied to the network properties
00059         for (int i = 0; i < seglength; ++i)  // We read in the first segment of data
00060         {
00061                 noisefile >> currentinput;
00062                 epsfile << currentinput;
00063                 Network.Input.at(i) = currentinput; // copy same value to Network input vector  
00064         }
00065         //datafile >> currentref; // read from file into variable
00066         Network.Desired.at(0) =  (0.5 - Random.GetNumber())*2 * noisepower;
00067         currentindex = seglength;
00068         Network.CalcOutput(); // We calculate the output for the first segment 
00069         Network.AdaptWeights(); // and adapt the weights
00070         // this is done outside the first loop, because we don't want to output this value
00071         
00072         while (noisefile.good() )
00073         {
00074                 //datafile >> currentref; // read in a new input value 
00075                 noisefile >> currentinput;
00076                 for (int i = 0; i < typeArray.size(); ++i)
00077                 {
00078                         cout << "Layer: " << i << endl;
00079                         for (int j = 0; j < typeArray.at(i).size(); ++j)
00080                         {
00081                                 cout << "Neuron: " << j << endl;
00082                                 cout << "Delta: " << Network.Layers.at(i).at(j)->Delta << endl;
00083                                 cout << "Net: " << Network.Layers.at(i).at(j)->Net << endl;
00084                                 cout << "Output: " << Network.Layers.at(i).at(j)->Output << endl;
00085                         }
00086                         cout << endl;
00087                 }
00088                 if (noisefile.good())
00089                 {
00090                         if ( !(currentindex % plotintervall))
00091                                 Network.WriteWeights(weightfile);
00092                         rotate(Network.Input.begin(),Network.Input.begin()+1,Network.Input.end()); // shift all elements so that the second becomes the first
00093                         Network.Input.back() = currentinput; // and copy the new values to the last position
00094                         Network.Desired.back() =  (0.5 - Random.GetNumber())*2 * noisepower; // currentref; // again set the reference
00095                         Network.CalcOutput(); // and calculate output 
00096                         if (currentindex < noiseseglength)
00097                                 Network.AdaptWeights(); // and adapt weights
00098                         outputfile << Network.Output.at(0) <<endl ; // finally write calculated output to file
00099                         //epsfile << (Network.Desired.at(0) - Network.Output.at(0)) << endl; // calculated difference and write to file as well
00100                         epsfile << (currentinput - Network.Desired.at(0)) << endl;
00101                         currentindex++;
00102                 }
00103         }
00104         
00105         noisefile.close();
00106         datafile.close();
00107         outputfile.close();
00108         epsfile.close();
00109         weightfile.close();
00110 }

Generated on Tue Aug 4 16:04:07 2009 for GPLIB++ by  doxygen 1.5.8