#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
using namespace ukf::parameter;
#define VERBOSE true
#define NB_INPUTS 2
#define NB_HIDDEN 2
#define NB_OUTPUTS 3
double transfer(double x)
{
return 2.0 / (1.0 + exp(-x))-1.0;
}
void my_func(gsl_vector * param, gsl_vector * input, gsl_vector * output)
{
double y[NB_HIDDEN];
double z=0.0;
for(int i = 0 ; i < NB_HIDDEN ; i++)
y[i] = 0.0;
int i_param = 0;
gsl_vector_set_zero(output);
for(int j = 0 ; j < NB_HIDDEN ; j++)
{
for(int k = 0 ; k < NB_INPUTS ; k++)
{
y[j] += gsl_vector_get(param, i_param) * gsl_vector_get(input, k);
i_param++;
}
y[j] += gsl_vector_get(param, i_param);
i_param++;
y[j] = transfer(y[j]);
}
for(int i = 0 ; i < NB_OUTPUTS ; i++)
{
z = 0.0;
for(int j = 0 ; j < NB_HIDDEN ; j++)
{
z += gsl_vector_get(param,i_param) * y[j];
i_param++;
}
z += gsl_vector_get(param, i_param);
i_param++;
z = transfer(z);
gsl_vector_set(output, i, z);
}
}
int main(int argc, char* argv[]) {
srand(time(NULL));
p.
n = (NB_INPUTS + 1)*NB_HIDDEN + (NB_HIDDEN+1) * NB_OUTPUTS;
for(
int i = 0 ; i < p.
n ; i++)
gsl_vector_set(s.
w,i,0.1*(2.0*rand()/
double(RAND_MAX-1)-1.0));
gsl_vector * xi = gsl_vector_alloc(NB_INPUTS);
gsl_vector * yi = gsl_vector_alloc(NB_OUTPUTS);
gsl_vector_set_zero(yi);
double x[4] = {-1.0,1.0,-1.0,1.0};
double y[4] = {-1.0,-1.0,1.0,1.0};
double z0[4] = {-1.0,1.0,1.0,-1.0};
double z1[4] = {-1.0, -1.0, -1.0, 1.0};
double z2[4] = {-1.0, 1.0, 1.0, 1.0};
double errorBound = 1e-3;
int nbStepsLimit = 500;
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] << " -> " << std::endl;
std::cout << " " << gsl_vector_get(yi,0) << " desired : " << z0[j] << std::endl;
std::cout << " " << gsl_vector_get(yi,1) << " desired : " << z1[j] << std::endl;
std::cout << " " << gsl_vector_get(yi,2) << " desired : " << z2[j] << std::endl;
error += pow(gsl_vector_get(yi,0) - z0[j],2.0) + pow(gsl_vector_get(yi,1) - z1[j],2.0) + pow(gsl_vector_get(yi,2) - z2[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]);
gsl_vector_set(yi,0,z0[j]);
gsl_vector_set(yi,1,z1[j]);
gsl_vector_set(yi,2,z2[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(gsl_vector_get(yi,0) - z0[j],2.0) + pow(gsl_vector_get(yi,1) - z1[j],2.0) + pow(gsl_vector_get(yi,2) - z2[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] << " -> " << std::endl;
std::cout << " " << gsl_vector_get(yi,0) << " desired : " << z0[j] << std::endl;
std::cout << " " << gsl_vector_get(yi,1) << " desired : " << z1[j] << std::endl;
std::cout << " " << gsl_vector_get(yi,2) << " desired : " << z2[j] << std::endl;
}
}