#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#define VERBOSE true
using namespace ukf::parameter;
double transfer(double x)
{
return 1.0 / (1.0 + exp(-x));
}
double my_func(gsl_vector * param, gsl_vector * input)
{
double y0,y1,z;
y0 = 0.0;
y1 = 0.0;
for(int i = 0 ; i < 2 ; i++)
{
y0 += gsl_vector_get(param,i) * gsl_vector_get(input, i);
}
y0 = y0 + gsl_vector_get(param,2);
y0 = transfer(y0);
for(int i = 0 ; i < 2 ; i++)
{
y1 += gsl_vector_get(param,3+i) * gsl_vector_get(input, i);
}
y1 = y1 + gsl_vector_get(param,5);
y1 = transfer(y1);
z = gsl_vector_get(param,6) * y0 + gsl_vector_get(param,7)*y1 + gsl_vector_get(param,8);
z = transfer(z);
return z;
}
int main(int argc, char* argv[]) {
srand(time(NULL));
for(
int i = 0 ; i < p.
n ; i++)
gsl_vector_set(s.
w,i,0.01*(2.0*rand()/
double(RAND_MAX-1)-1.0));
gsl_vector * xi = gsl_vector_alloc(2);
double yi=0.0;
double x[4] = {0.0,1.0,0.0,1.0};
double y[4] = {0.0,0.0,1.0,1.0};
double z[4] = {0.0,1.0,1.0,0.0};
double errorBound = 1e-4;
int nbStepsLimit = 10000;
double error = 2*errorBound;;
error = 0.0;
std::cout << "###########\n Before learning : " << std::endl;
for(int j = 0 ; j < 4 ; j++)
{
gsl_vector_set(xi,0,x[j]);
gsl_vector_set(xi,1,y[j]);
std::cout << x[j] << " ; " << y[j] << " -> " << yi << " mean = " << yi << " desired : " << z[j] << std::endl;
error += pow(yi - z[j],2.0);
}
error = sqrt(error / 4.0);
std::cout << "RMS : " << error << std::endl;
std::cout << "###########\n " << std::endl;
int epoch = 0;
error = 2 * errorBound;
while( epoch <= nbStepsLimit && error > errorBound)
{
for(int j = 0 ; j < 4 ; j++)
{
gsl_vector_set(xi,0,x[j]);
gsl_vector_set(xi,1,y[j]);
}
error = 0.0;
for(int j = 0 ; j < 4 ; j++)
{
gsl_vector_set(xi,0,x[j]);
gsl_vector_set(xi,1,y[j]);
error += pow(yi - z[j],2.0);
}
error = sqrt(error / 4.0);
if(VERBOSE)
std::cout << "Epoch " << epoch << " error = " << error << std::endl;
epoch++;
}
std::cout << " Run on " << epoch << " epochs ; RMS = " << error << "\n" << std::endl ;
std::cout << "###########\n After learning : " << std::endl;
for(int j = 0 ; j < 4 ; j++)
{
gsl_vector_set(xi,0,x[j]);
gsl_vector_set(xi,1,y[j]);
std::cout << x[j] << " ; " << y[j] << " -> " << yi << " desired : " << z[j] << std::endl;
}
std::cout << std::endl;
std::cout << "Parameters : " << std::endl;
for(int i = 0 ; i < 2 ; i++)
{
std::cout <<
"x[" << i <<
"] -- (" << gsl_vector_get(s.
w,i) <<
") --> y[0] " << std::endl;
}
std::cout <<
"Bias y[0] : " << gsl_vector_get(s.
w,2) << std::endl;
std::cout << std::endl;
for(int i = 0 ; i < 2 ; i++)
{
std::cout <<
"x[" << i <<
"] -- (" << gsl_vector_get(s.
w,3+i) <<
") --> y[1] " << std::endl;
}
std::cout <<
"Bias y[1] : " << gsl_vector_get(s.
w,5) << std::endl;
std::cout << std::endl;
std::cout <<
"y[0] -- (" << gsl_vector_get(s.
w,6) <<
") --> z " << std::endl;
std::cout <<
"y[1] -- (" << gsl_vector_get(s.
w,7) <<
") --> z " << std::endl;
std::cout <<
"Bias z : " << gsl_vector_get(s.
w,8) << std::endl;
std::cout << "########### " << std::endl;
std::cout << "Generating output image in example-001.ppm" << std::endl;
double x_min, x_max, y_min, y_max;
x_min = -1.0;
x_max = 1.0;
y_min = -1.0;
y_max = 1.0;
int N = 100;
int color;
std::ofstream image("example-001.ppm");
image << "P3" << std::endl << "# example-001.ppm" << std::endl;
image << N << " " << N << std::endl;
image << "255" << std::endl;
for(int i = 0 ; i < N ; i++)
{
for(int j = 0 ; j < N ; j++)
{
gsl_vector_set(xi, 0, x_min + j*(x_max - x_min)/double(N-1));
gsl_vector_set(xi, 1, y_min + (N-1-i)*(y_max - y_min)/double(N-1));
color = int(255.0*(1.0 + yi)/2.0);
image << color << " " << color << " " << color << " " ;
}
image << std::endl;
}
image.close();
}