GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NeuralNetworkFnTest.C
Go to the documentation of this file.
1 /* Copyright 2000-2013 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 
18 #include <Eigen/Dense>
19 #include <fstream>
20 #include <iostream>
21 using namespace std;
22 
23 #include "NeuralNetwork.H"
24 #include "gtkInterface.H"
25 #include "Buttons.H"
26 #include "Plot.H"
27 
28 // setup the possible plot colours ...
29 int ColourLineSpec::colourCnt=10; // match this number with the number of colours below
30 const char *ColourLineSpec::shortColours[]= {(char *)"y",(char *)"m",(char *)"c",(char *)"r",(char *)"g",(char *)"b",(char *)"w",(char *)"k",(char *)"o",(char *)"a"}; // standard colours
31 const char *ColourLineSpec::X11Colours[]= {(char *)"yellow", (char *)"magenta", (char *)"cyan", (char *)"red", (char *)"green", (char *)"blue", (char *)"white", (char *)"black", (char *)"orange", (char *)"gray"};
32 
33 // Quit button callback
34 static void quit(void *wid, gpointer data) {
35  gtk_main_quit();
36 }
37 
38 int main(int argc, char *argv[]){
39  // To construct a network, we load some weights and biases from file and create a layer using them
40  // This is done for each layer.
41  // Finally a NeuralNetwork is created to execute each layer
42 
43  // load the input layer weights and biases
44  Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> weights(21,21);
45  Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> bias(21,1);
46 
47  Eigen::Matrix<float, Eigen::Dynamic, 1> input(21,1);
48  input=Eigen::Matrix<float, Eigen::Dynamic, 1>::Zero(21,1);
49 
50 //cout<<"weights "<<endl;
51 //cout<<weights<<endl;
52 //cout<<"bias "<<endl;
53 //cout<<bias<<endl;
54 //cout<<"input "<<endl;
55 //cout<<input<<endl;
56  // Begin constructing the neural network topology
57  vector<NeuralLayer<float> *> networkLayers;
58  bias<<-1.,-.9,-.8,-.7,-.6,-.5,-.4,-.3,-.2,-.1,0.,.1,.2,.3,.4,.5,.6,.7,.8,.9,1.;
59  bias*=10.;
60  networkLayers.push_back(new SigmoidLayer<float>(weights, bias));
61  networkLayers.push_back(new TanhLayer<float>(weights, bias));
62  //networkLayers.push_back(new PosLayer<float>(weights, bias));
63 
65  nn.activate(networkLayers, input);
66 // cout<<networkLayers[0]->output<<endl;
67  Eigen::Matrix<float, Eigen::Dynamic, 1> biasSig=bias;
68  Eigen::Matrix<float, Eigen::Dynamic, 1> outputSig=networkLayers[0]->output;
69  Eigen::Matrix<float, Eigen::Dynamic, 1> outputTanh=networkLayers[1]->output;
70  //Eigen::Matrix<float, Eigen::Dynamic, 1> outputPos=networkLayers[2]->output;
71 
72  // clean up
73  for (vector<NeuralLayer<float> *>::iterator nl=networkLayers.begin(); nl!=networkLayers.end(); ++nl)
74  delete (*nl);
75 
76  // plot the results
77 
78  gtk_init( &argc, &argv ); // start the GUI
79 
80  Plot figure;
81  figure.plot(biasSig.data(), outputSig.data(), outputSig.rows(),"b");
82  figure.hold(true);
83  figure.plot(biasSig.data(), outputTanh.data(), outputTanh.rows(),"r");
84 
85  figure.limits(); // autoscale
86  figure.hold(false);
87 
88 
89  GtkInterface topWindow;
90 
91  VBox vBox;
92  vBox<<BoxIS(true, true, true)<<figure.getWidget()<<BoxIS(false, false, false)<<(Buttons()<<BUTTONLABELSTRUCT((char*)"Quit", quit, NULL));
93  topWindow<< vBox.show(); // add it to the top window
94 
95  gtk_main();
96 
97  return 0;
98 }