Robotsystem from Robot Group Leipzig: feedforwardtest.cpp Source File

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# feedforwardtest.cpp

Go to the documentation of this file.

    00001 #include<iostream>
    00002 #include<vector>
    00003 using namespace std;
    00004
    00005 #include "selforg/multilayerffnn.h"
    00006 #include "matrix.h"
    00007 using namespace matrix;
    00008
    00009 void test2x2(){
    00010   double data1[2] = {0,1};
    00011   Matrix input1(2,1,data1);
    00012   double data2[2] = {0,0.9};
    00013   Matrix output1(2,1,data2);
    00014   double data3[2] = {1,0};
    00015   Matrix input2(2,1,data3);
    00016   double data4[2] = {0.4,0.6};
    00017   Matrix output2(2,1,data4);
    00018
    00019   vector<Layer> layers;
    00020   layers.push_back(Layer(3, 0.1, FeedForwardNN::sigmoid, FeedForwardNN::dsigmoid));
    00021   layers.push_back(Layer(2));
    00022   MultiLayerFFNN net(0.1, layers);
    00023   net.init(2,2);
    00024
    00025   // train
    00026   for(int i=0; i < 500; i++){
    00027     net.learn(input1, output1);
    00028     net.learn(input2, output2);
    00029   }
    00030
    00031   // test
    00032   cout << "TEST" << endl;
    00033   cout << net.process(input1);
    00034   cout << net.process(input2);
    00035 }
    00036
    00037 void testnonlinear(){
    00038   double o0[1] = {0};
    00039   double o1[1] = {1};
    00040
    00041   double i0[2] = {0,0};
    00042   double i1[2] = {0,1};
    00043   double i2[2] = {-1,0};
    00044   double i3[2] = {-1,1};
    00045   double i4[2] = {1,1};
    00046
    00047   vector<Layer> layers;
    00048   layers.push_back(Layer(1, 0.1, FeedForwardNN::sigmoid, FeedForwardNN::dsigmoid));
    00049   layers.push_back(Layer(2));
    00050   MultiLayerFFNN net(0.1, layers);
    00051   net.init(2,1);
    00052
    00053   // train
    00054   for(int i=0; i < 500; i++){
    00055     net.learn(Matrix(2, 1, i0), Matrix(1, 1, o0));
    00056     net.learn(Matrix(2, 1, i1), Matrix(1, 1, o0));
    00057     net.learn(Matrix(2, 1, i2), Matrix(1, 1, o0));
    00058     net.learn(Matrix(2, 1, i3), Matrix(1, 1, o0));
    00059     net.learn(Matrix(2, 1, i4), Matrix(1, 1, o1));
    00060   }
    00061
    00062   // test
    00063   cout << "TEST" << endl;
    00064   cout << net.process(Matrix(2, 1, i0));
    00065   cout << net.process(Matrix(2, 1, i1));
    00066   cout << net.process(Matrix(2, 1, i2));
    00067   cout << net.process(Matrix(2, 1, i3));
    00068   cout << net.process(Matrix(2, 1, i4));
    00069
    00070 }
    00071
    00072 int main(){
    00073   test2x2();
    00074   testnonlinear();
    00075   return 0;
    00076 }
    00077
    00078