Home | Develop | Download | Contact
example_training_save.c
1 /*
2  * example_training_save.c
3  *
4  * Copyright 2011 Fernando Pujaico Rivera <fernando.pujaico.rivera@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  */
22 
32 #include <stdlib.h>
33 #include <math.h>
34 
35 #include <pds/pdsra.h>
36 #include <pds/pdsnn.h>
37 
38 // ./test/example_training_save ../test/data.mlayer
39 int main(int argc, char** argv)
40 {
41  PdsNnNatural i,id,IterN,IterP;
42  PdsNnNatural Ntraining=128;
43 
44  // Creating the neural network
45  PdsNnNatural Larray[]={64,128,64,48,14,7};
46  PdsNnNatural Nel=6;
47  PdsNeuronML *MLayer=pds_neuronml_new_from_array(Larray,Nel);
48  if(MLayer==NULL) return EXIT_FAILURE;
49 
50  // Configuring the neural network
51  PdsNnReal Alpha=0.6; pds_neuronml_set_alpha(MLayer,Alpha);
52  PdsNnReal Error=0.1; pds_neuronml_set_max_error(MLayer,Error);
53  PdsNnNatural MaxIter=8192; pds_neuronml_set_max_iterations(MLayer,MaxIter);
54 
55  // Creating temporal vectors X and Y.
56  PdsVector *X = pds_vector_new(Larray[0]);
57  PdsVector *Y = pds_vector_new(Larray[Nel-1]);
58  if( (X==NULL)||(Y==NULL) ) return EXIT_FAILURE;
59 
60  // Training the neural network
61  id=0;
62  for(i=0;id<Ntraining;i++)
63  {
64  printf("\nTraining %4d: id=%4d\n",i,id);
65 
66  // Training with negative data.
68  pds_generate_data_output_negative(Y); // Loading randomly a negative data vectors {X,Y}
69 
70  pds_neuronml_training(MLayer,X,Y);
71  IterN=pds_neuronml_training_results_printf(MLayer,"-1");
72 
73  // Training with positive data.
74  pds_generate_data_input_func2pi(X,sin,6,0.05);
75  pds_generate_data_output_positive(Y); // Loading randomly a positive data vectors {X,Y}
76 
77  pds_neuronml_training(MLayer,X,Y);
78  IterP=pds_neuronml_training_results_printf(MLayer,"+1");
79 
80  if( (IterP==0)&&(IterN==0) ) id++;
81  else id=0;
82  }
83 
84  if(argc>1)
85  {
86  pds_neuronml_save_data(MLayer,argv[1]);
87  printf("\nSaving neural network in %s\n\n",argv[1]);
88  }
89  else
90  {
91  pds_neuronml_save_data(MLayer,"data.mlayer");
92  printf("\nSaving neural network in data.mlayer\n\n");
93  }
94 
95  pds_neuronml_free(MLayer);
96 
97  pds_vector_free(X);
98  pds_vector_free(Y);
99 
100  return EXIT_SUCCESS;
101 }
102 
int pds_neuronml_set_max_error(PdsNeuronML *NeuronML, PdsNnReal MaxError)
Coloca el maximo margen error RMS aceptado en la salida de la estructura PdsNeuronML.
PdsNeuronML * pds_neuronml_new_from_array(const PdsNnNatural *L, PdsNnNatural Nel)
Crea una estructura de tipo PdsNeuronML.
int pds_neuronml_set_alpha(PdsNeuronML *NeuronML, PdsNnReal Alpha)
Coloca el valor del factor de aprendizaje Alpha.
La estructura tipo PdsNeuronML . Esta estructura genera una red neuronal multi capa. Para usar incluir pds/pdsnn.h.
Definition: pdsneuronml.h:74
void pds_neuronml_free(PdsNeuronML *NeuronML)
Libera una neurona de tipo puntero PdsNeuronML.
int pds_generate_data_input_rand(PdsVector *X, PdsNnReal A)
int pds_generate_data_output_negative(PdsVector *Y)
Genera un vector con -1 en todos los elementos del vector Y.
int pds_neuronml_set_max_iterations(PdsNeuronML *NeuronML, PdsNnNatural MaxIter)
Coloca el máximo numero de iteraciones aceptado, cualquier valor mayor provoca que se detenga el algo...
unsigned int PdsNnNatural
Definition: pdsnnglobal.h:62
float PdsNnReal
Definition: pdsnnglobal.h:52
int pds_generate_data_output_positive(PdsVector *Y)
Genera un vector con +1 en todos los elementos del vector Y.
int pds_neuronml_save_data(const PdsNeuronML *NeuronML, const char *mlayer_filename)
Salva una estructura de tipo PdsNeuronML en un archivo de texto.
int pds_neuronml_training(PdsNeuronML *NeuronML, const PdsVector *Input, PdsVector *Output)
Entrena la red neuronal multicapa NeuronML. Usa el valor de Output como entrenamiento, para finalizar carga el nuevo valor de salida en Output.
int pds_generate_data_input_func2pi(PdsVector *X, double(*func)(double), PdsNnReal C, PdsNnReal OffsetError)
Genera un vector con C ciclos de la función func() con un error de offset de valor OffsetError...
PdsNnNatural pds_neuronml_training_results_printf(PdsNeuronML *MLayer, const char *Type)
Muestra los resultados del entrenamiento de la red neuronal multicapa NeuronML.

Enlaces de interés

HomePage Bazaar Download Bug report Ayuda Developer Feed