Dirk Gorissen
2007-07-30 14:48:43 UTC
Hello,
Ive been using ANN for a long time but always in a Matlab environment (I know next to nothing of C). Since I need to speed things up I wanted to try fann. I started off with a a simple data fitting problem (thats what I need ANNs for). But unfortunately I cant get it to work, though my code seems ok (see below and attached). The output when run is:
Code:
Dataset: 100 patterns, 1 inputs, 1 outputs
Max epochs 5000. Desired error: 0.0010000000.
Epochs 1. Current error: inf. Bit fail 47.
Epochs 1000. Current error: inf. Bit fail 100.
Epochs 2000. Current error: inf. Bit fail 100.
Epochs 3000. Current error: inf. Bit fail 100.
Epochs 4000. Current error: inf. Bit fail 100.
Epochs 5000. Current error: inf. Bit fail 100.
The network output for 0.500000 is: 0.000000
The network output for 0.830000 is: 0.000000
So, for some reason training is not doing anything... What am I doing wrong? Ive searched through google, the forum and the mailinglist archives but came up with nothing. So it seems it must be something trivial :/ Im using the latest version 2.1beta.
Many thanks,
Dirk
Ps: I got the "read_from_array" function from a different forum post, Im assuming it is correct.
Code (full code attached):
#include "doublefann.h"
#include "fann.h"
#include "math.h"
/*
* Reads training data from a double array
*/
struct fann_train_data *read_from_array(double *din, double *dout, unsigned int num_data,
unsigned int num_input, unsigned int num_output) {
unsigned int i, j;
fann_type *data_input, *data_output;
//...taken from: http://leenissen.dk/fann/forum/viewtopic.php?p=719&sid=1661ac359e28908e704231faa6310518
return data;
}
int main()
{
//init
const float desired_error = (const float) 0.001;
const unsigned int max_epochs = 5000;
const unsigned int epochs_between_reports = 1000;
struct fann_train_data *train_data;
//create a simple network
unsigned int layers[3] = {1, 4, 1};
struct fann *ann = fann_create_standard_array(3, layers);
fann_randomize_weights(ann, -1, 1);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_LINEAR);
fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
fann_set_train_error_function(ann, FANN_ERRORFUNC_LINEAR);
//generate some artificial data
int n = 100;
double din[n];
double dout[n];
double x = 0;
int i = 0;
int k = 0;
while(i < n){
x = x + (5*3.14)/n;
din[i] = x;
dout[i] = sin(x);
//printf("%i - dataset is: sin(%f) = %f\n",k,x,dout[i]);
++i;
++k;
}
train_data = read_from_array(din, dout, n, 1, 1);
int num = fann_length_train_data(train_data);
int numIn = fann_num_input_train_data(train_data);
int numOut = fann_num_output_train_data(train_data);
printf("Dataset: %i patterns, %i inputs, %i outputs\n",num,numIn,numOut);
//train the network
fann_train_on_data(ann, train_data, max_epochs, epochs_between_reports, desired_error);
//evaluate the trained network in a few places
//do we have to specify one value at a time??
double in = 0.5;
fann_type* out = fann_run(ann, &in);
printf("The network output for %f is: %f\n",in,out);
in = 0.83;
out = fann_run(ann, &in);
printf("The network output for %f is: %f\n",in,out);
//cleanup
fann_destroy_train(train_data);
fann_destroy(ann);
return 0;
}
Ive been using ANN for a long time but always in a Matlab environment (I know next to nothing of C). Since I need to speed things up I wanted to try fann. I started off with a a simple data fitting problem (thats what I need ANNs for). But unfortunately I cant get it to work, though my code seems ok (see below and attached). The output when run is:
Code:
Dataset: 100 patterns, 1 inputs, 1 outputs
Max epochs 5000. Desired error: 0.0010000000.
Epochs 1. Current error: inf. Bit fail 47.
Epochs 1000. Current error: inf. Bit fail 100.
Epochs 2000. Current error: inf. Bit fail 100.
Epochs 3000. Current error: inf. Bit fail 100.
Epochs 4000. Current error: inf. Bit fail 100.
Epochs 5000. Current error: inf. Bit fail 100.
The network output for 0.500000 is: 0.000000
The network output for 0.830000 is: 0.000000
So, for some reason training is not doing anything... What am I doing wrong? Ive searched through google, the forum and the mailinglist archives but came up with nothing. So it seems it must be something trivial :/ Im using the latest version 2.1beta.
Many thanks,
Dirk
Ps: I got the "read_from_array" function from a different forum post, Im assuming it is correct.
Code (full code attached):
#include "doublefann.h"
#include "fann.h"
#include "math.h"
/*
* Reads training data from a double array
*/
struct fann_train_data *read_from_array(double *din, double *dout, unsigned int num_data,
unsigned int num_input, unsigned int num_output) {
unsigned int i, j;
fann_type *data_input, *data_output;
//...taken from: http://leenissen.dk/fann/forum/viewtopic.php?p=719&sid=1661ac359e28908e704231faa6310518
return data;
}
int main()
{
//init
const float desired_error = (const float) 0.001;
const unsigned int max_epochs = 5000;
const unsigned int epochs_between_reports = 1000;
struct fann_train_data *train_data;
//create a simple network
unsigned int layers[3] = {1, 4, 1};
struct fann *ann = fann_create_standard_array(3, layers);
fann_randomize_weights(ann, -1, 1);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_LINEAR);
fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
fann_set_train_error_function(ann, FANN_ERRORFUNC_LINEAR);
//generate some artificial data
int n = 100;
double din[n];
double dout[n];
double x = 0;
int i = 0;
int k = 0;
while(i < n){
x = x + (5*3.14)/n;
din[i] = x;
dout[i] = sin(x);
//printf("%i - dataset is: sin(%f) = %f\n",k,x,dout[i]);
++i;
++k;
}
train_data = read_from_array(din, dout, n, 1, 1);
int num = fann_length_train_data(train_data);
int numIn = fann_num_input_train_data(train_data);
int numOut = fann_num_output_train_data(train_data);
printf("Dataset: %i patterns, %i inputs, %i outputs\n",num,numIn,numOut);
//train the network
fann_train_on_data(ann, train_data, max_epochs, epochs_between_reports, desired_error);
//evaluate the trained network in a few places
//do we have to specify one value at a time??
double in = 0.5;
fann_type* out = fann_run(ann, &in);
printf("The network output for %f is: %f\n",in,out);
in = 0.83;
out = fann_run(ann, &in);
printf("The network output for %f is: %f\n",in,out);
//cleanup
fann_destroy_train(train_data);
fann_destroy(ann);
return 0;
}
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer