GPLIB++
simplenet.cpp
Go to the documentation of this file.
1 #include "NeuralNetwork.h"
2 #include <iostream>
3 #include <vector>
4 #include <fstream>
5 #include <string>
6 #include "CUniformRNG.h"
7 
8 using namespace std;
9 using namespace gplib;
10 
11 int main()
12  {
13  // first some global constants that can be changed to play with
14  const int seglength = 100; // the length of the segment we want to work on
15  const double maxinit = 0.01; // the maximum the neuron weights are initialized to
16  const double maxbias = 0.01; // the maximum bias for each neuron
17  const int maxit = 1000; // maximum number of iterations = maximum output length
18  const int hiddenlayers = 2; // the number of hiddenlayers
19  const double mu = 0.1; // the stepsize for weight adaptation for each neuron;
20 
21  ttypeVector typeVector; // a vector that contains the type for each neuron in a layer
22  ttypeArray typeArray; // an array of typevectors that contains the neuron type for the whole network;
23  ifstream inputfile, reffile; // the file objects for the two inputfiles: the noise time series and the reference
24  ofstream outputfile, epsfile; // the file objects for the two outputfiles: the cleaned file and the difference to the original
25 
26  double currentinput, currentref; // the current input and reference values we're working on
27 
28  string inputfilename, reffilename; // the strings containing the filenames
29  string outputfilename, epsfilename;
30 
31  cout << "Inputfile: ";
32  cin >> inputfilename; // read in input filename
33  cout << "Referencefile:";
34  cin >> reffilename; // read in reference filename
35  outputfilename = inputfilename + ".clean"; // set outputfilename for clean file
36  epsfilename = inputfilename + ".eps"; // and for difference file
37  inputfile.open(inputfilename.c_str()); // open the files for reading
38  reffile.open(reffilename.c_str()); // reading
39  epsfile.open(epsfilename.c_str()); // writing
40  outputfile.open(outputfilename.c_str()); // writing
41 
42  typeVector.assign(seglength, bipolar); // we want seglength number of bipolar neurons per hidden layer
43  for (int i = 0; i < hiddenlayers; ++i) //intialize the type array for the hidden layers
44  {
45  typeArray.push_back(typeVector); // all layers are the same, so we copy the same vector there
46  }
47  // We want to combine all values into one output values, also it's range should not be restricted
48  typeVector.assign(1, identity); // the last layer therefore contains a single neuron with the identity activation function
49  typeArray.push_back(typeVector); // and then we add it to the type Array
50 
51  NeuralNetwork Network; //We declare a new variable for the network
52  Network.Input.assign(seglength, 0); // We have to allocate memory for the network input and set it to zero
53  Network.SetLayers(typeArray); // We use the type array we created before to setup the neurons in the Network
54  Network.InitWeights(maxinit, maxbias); // The Weights are initialized with the two maximum values
55  Network.mu = mu; // The mu value is copied to the network properties
56  for (int i = 0; i < seglength; ++i) // We read in the first segment of data
57  {
58  inputfile >> currentinput; // read from file into variable
59  Network.Input.at(i) = currentinput; // copy same value to Network input vector
60  }
61  reffile >> currentref;
62  Network.Desired.at(0) = currentref; //For the reference we only need a single value, we take the beginning of the segment (can be changed)
63  Network.CalcOutput(); // We calculate the output for the first segment
64  Network.AdaptWeights(); // and adapt the weights
65  // this is done outside the first loop, because we don't want to output this value
66 
67  for (int iterations = 0; iterations < maxit; ++iterations) // now we do the remaining time seriesup to maxit
68  {
69 
70  inputfile >> currentinput; // read in a new input value
71  reffile >> currentref; // and reference value
72 
73  rotate(Network.Input.begin(), Network.Input.begin() + 1,
74  Network.Input.end()); // shift all elements so that the second becomes the first
75  Network.Input.back() = currentinput; // and copy the new values to the last position
76 
77  Network.Desired.at(0) = currentref; // again set the reference
78  Network.CalcOutput(); // and calculate output
79  Network.AdaptWeights(); // and adapt weights
80 
81  outputfile << Network.Output.at(0) << endl; // finally write calculated output to file
82  epsfile << (Network.Desired.at(0) - Network.Output.at(0)) << endl; // calculated difference and write to file as well
83  }
84  inputfile.close();
85  reffile.close();
86  outputfile.close();
87  epsfile.close();
88 
89  }
void InitWeights(const double MaxWeight, const double MaxBias)
Initialize the weights with random values with the specified maxima.
void SetLayers(ttypeArray typeArray, bool cachedoutput=false)
Configure the layers of the network according to the types in typeArray.
const int iterations
Definition: perftest.cpp:15
int main()
Definition: simplenet.cpp:11