Register    Login    Forum    Search    FAQ

Board index » FANN » Enhancing the C library




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: creating training data from an array
 Post Posted: Tue May 22, 2007 10:35 pm 
Offline

Joined: Sat Mar 10, 2007 5:24 pm
Posts: 11
Hi everyone. As far as I can tell, there is no way in FANN to create training data from an array. I wrote some code, pasted below, that does this. It only works with doubles and not FANN_TYPE at the moment, but someone can probably easily fix it. I don't really know what I am doing, but this seems to work for my purposes. If someone more familiar with the code base would like to change it to work with FANN_TYPE and give it a quick test and commit it that would be great. I basically modified the fann read training data from file function. Use the code at your own risk, I thought it might be useful to people and that someone could use it to quickly add that functionality to the library.

- George


Code:

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;
  struct fann_train_data *data =
    (struct fann_train_data *) malloc(sizeof(struct fann_train_data));
  if(data == NULL) {
    fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
    return NULL;
  }
 
  fann_init_error_data((struct fann_error *) data);
 
  data->num_data = num_data;
  data->num_input = num_input;
  data->num_output = num_output;
  data->input = (double **) calloc(num_data, sizeof(double *));
  if(data->input == NULL) {
    fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
    fann_destroy_train(data);
    return NULL;
  }
 
  data->output = (double **) calloc(num_data, sizeof(double *));
  if(data->output == NULL) {
    fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
    fann_destroy_train(data);
    return NULL;
  }
 
  data_input = (double *) calloc(num_input * num_data, sizeof(double));
  if(data_input == NULL) {
    fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
    fann_destroy_train(data);
    return NULL;
  }
 
  data_output = (double *) calloc(num_output * num_data, sizeof(double));
  if(data_output == NULL) {
    fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
    fann_destroy_train(data);
    return NULL;
  }
 
  for(i = 0; i != num_data; i++) {
    data->input[i] = data_input;
    data_input += num_input;
   
    for(j = 0; j != num_input; j++) {
      data->input[i][j] = din[i*num_input+j];
    }
   
   
    data->output[i] = data_output;
    data_output += num_output;
   
    for(j = 0; j != num_output; j++) {
      data->output[i][j] = dout[i*num_output+j];
    }
  }
  return data;
}


Top 
 Profile  
 
 Post subject:
 Post Posted: Thu Jul 12, 2007 9:50 am 
Offline
User avatar

Joined: Fri Mar 30, 2007 3:37 pm
Posts: 15
Hmm, this looks quite nice. It's pratically the boilerplate code lots of people probably wind up writing anyhow when loading array data. I think adapting it to fann_type requires no more than serach&replace double with fann_type.

If all it takes for this stuff to be accepted into fann is a proper cvs patch I would be happy to make one.

Best

Daniel


Top 
 Profile  
 
 Post subject:
 Post Posted: Fri Jul 13, 2007 11:45 am 
Offline

Joined: Tue May 15, 2007 5:54 pm
Posts: 7
Location: paris
It solved my probleme thanks very much

_________________
No one ear your scream in a Banach Space


Top 
 Profile  
 
 Post subject:
 Post Posted: Fri Jul 13, 2007 8:30 pm 
Offline
User avatar

Joined: Tue Mar 06, 2007 7:24 pm
Posts: 264
Location: Copenhagen, Denmark
A CVS patch will be nice against the GSoC branch, but since it is just an add of a function it will not really be needed.

However, what will be needed, is a more suitable name for the function like e.g. fann_create_train_from_array and a proper documentation in natural docs format will also be needed, please see fann_train.h

_________________
Steffen Nissen - http://facebook.com/profile.php?id=595485027
Project Administrator - Fast Artificial Neural Network Library (FANN)
http://leenissen.dk/fann/


Top 
 Profile  
 
 Post subject:
 Post Posted: Tue Jul 31, 2007 11:54 am 
Offline

Joined: Thu Jul 26, 2007 6:14 pm
Posts: 10
I am having problem using this code. If I read from file it works though. However, I really cant afford to use files. See my original post here:

http://leenissen.dk/fann/forum/viewtopic.php?t=178

What have I overlooked?

Many thanks,

Dirk


Top 
 Profile  
 
 Post subject:
 Post Posted: Wed Aug 01, 2007 10:14 am 
Offline

Joined: Thu Jul 26, 2007 6:14 pm
Posts: 10
Problem solved...seemed to be a bug in doublefann.h
See: http://leenissen.dk/fann/forum/viewtopic.php?t=178

Cheers
Dirk


Top 
 Profile  
 
 Post subject:
 Post Posted: Wed Feb 27, 2008 5:12 pm 
Offline

Joined: Mon Feb 25, 2008 8:30 pm
Posts: 7
does exist a functional c# implemantation of this function... i dont wanne use the way with file... but can not port it alone :-(


Top 
 Profile  
 
 Post subject:
 Post Posted: Thu Jul 17, 2008 11:07 am 
Offline

Joined: Thu Jul 10, 2008 10:33 pm
Posts: 14
numpsy wrote:
does exist a functional c# implemantation of this function... i dont wanne use the way with file... but can not port it alone :-(


My C# solution for now:

Code:
FannNetBinding.Training.TrainCallback proc = delegate(uint num, uint num_input, uint numOutput, IntPtr input, IntPtr output)
{
   float[] indata = new float[numInput];
   // TODO: fill indata with real values
   Marshal.Copy(indata, 0, input, numInput)

   float[] outdata = new float[numOutput];
   // TODO: fill outdata with real values
   Marshal.Copy(outdata, 0, output, numOutput)

   return 0;
};
IntPtr data = FannNetBinding.Training.CreateFromCallback(4, 2, 1, proc);


It would be nice if some one could add this to the CVS:

Code:
/* Function: fann_create_train_from_data
   Creates the training data struct from a user data.

   Parameters:
     num_data      - The number of training data
     num_input     - The number of inputs per training data
     num_output    - The number of ouputs per training data
     input         - The user suplied data
     output         - The user suplied data

   See also:
     <fann_read_train_from_file>, <fann_train_on_data>, <fann_destroy_train>,
     <fann_save_train>

    This function appears in FANN >= 2.1.x
*/
FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_data(unsigned int num_data, unsigned int num_input, unsigned int num_output, fann_type** input, fann_type** output);

// and ...

/*
 * Creates an set of training data
 */
FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_data(unsigned int num_data, unsigned int num_input, unsigned int num_output, fann_type** input, fann_type** output)
{
   struct fann_train_data *data = fann_create_train(num_data, num_input, num_output);

   int i;
   for(i = 0; i < num_data; i++)
   {
      memcpy(data->input[i], input[i], sizeof(fann_type) * num_input);
      memcpy(data->output[i], output[i], sizeof(fann_type) * num_output);
   }

   return data;
}


Top 
 Profile  
 
 Post subject: Re: creating training data from an array
 Post Posted: Tue Mar 27, 2012 11:31 am 
Offline

Joined: Tue Mar 27, 2012 11:14 am
Posts: 2
It solved my probleme thanks very much

_________________
alışveriş


Top 
 Profile  
 
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 9 posts ] 

Board index » FANN » Enhancing the C library


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron