Home | Develop | Download | Contact
testprog1.c
1 /*
2  * testprog1.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 
33 #include <stdlib.h>
34 #include <math.h>
35 #include <pds/pdsra.h>
36 #include <pds/pdsrv.h>
37 #include <pds/pdsnn.h>
38 
39 #include "octplot.h"
40 #include "data_work.h"
41 
42 int main(int argc, char** argv)
43 {
44  PdsNeuronML *MLayer=NULL;
45  PdsVector *L=NULL;
46  PdsVector *X=NULL;
47  PdsVector *Y=NULL;
48  PdsUniform *RV=NULL;
49 
50  PdsNnNatural i,id,itera,iterb;
51  PdsNnNatural Ntraining=128;
52  PdsNnNatural Ntests=4;
53  PdsNnNatural MaxIter;
54  PdsNnReal Error;
55  PdsNnReal Alpha;
56 
57  // Creo una Random Variable
58  RV=pds_uniform_new(-1,1);
59 
60  // Creo el vector que indicará la cantidad de electrones por capa.
61  // 5 Capas.
62  PdsRaReal Larray[]={32,64,42,28,14,7};
63  L=pds_vector_new_array1d(Larray,6);
64 
65  // Creo El vector de entrada X.
66  X=pds_vector_new(Larray[0]);
67  // Creo El vector de entrada Y.
68  Y=pds_vector_new(Larray[5]);
69 
71 
72  MLayer=pds_neuronml_new(L);
74  pds_neuronml_init_u_uniform(MLayer,RV);
75  Alpha=0.6; pds_neuronml_set_alpha(MLayer,Alpha);
76  Error=0.1; pds_neuronml_set_max_error(MLayer,Error);
77  MaxIter=8192; pds_neuronml_set_max_iterations(MLayer,MaxIter);
78 
79  // Entrenando
80  id=0;
81  for(i=0;id<Ntraining;i++)
82  {
83  printf("\nEntrenamiento %4d: id=%4d\n",i,id);
84 
85  // Entrenando de forma negativa.
86  function_data_work_init_data0(X,Y);
87  pds_neuronml_training(MLayer,X,Y);
88  itera=pds_neuronml_training_results_printf(MLayer,"-1");
89 
90  // Entrenando de forma positiva.
91  function_data_work_init_data1(X,Y);
92  pds_neuronml_training(MLayer,X,Y);
93  iterb=pds_neuronml_training_results_printf(MLayer,"+1");
94 
95  if( (itera==0)&&(iterb==0) ) id++;
96  else id=0;
97 
98  }
99 
100  printf("\n\n");
101  // Iterando
102  for(i=0;i<Ntests;i++)
103  {
104  function_data_work_init_data1(X,Y);
105  pds_neuronml_iterate(MLayer,X,Y);
107  function_octplot_vector_in_png(X,"n","X[n]","octfilegood.m","xgood.png");
108  }
109 
110 
111  printf("\n");
112  for(i=0;i<Ntests;i++)
113  {
114  function_data_work_init_data0(X,Y);
115  pds_neuronml_iterate(MLayer,X,Y);
117  function_octplot_vector_in_png(X,"n","X[n]","octfilebad.m","xbad.png");
118  }
119 
120  pds_neuronml_free(MLayer);
121 
122  printf("\n");
123 
124  pds_vector_free(X);
125  pds_vector_free(Y);
126  pds_vector_free(L);
127 
128  return EXIT_SUCCESS;
129 }
130 
PdsNnBool pds_neuronml_iterate_results_printf(const PdsNeuronML *MLayer, const char *Type)
Muestra los resultados de la iteración de la red neuronal multicapa NeuronML.
int pds_neuronml_set_max_error(PdsNeuronML *NeuronML, PdsNnReal MaxError)
Coloca el maximo margen error RMS aceptado en la salida de la estructura PdsNeuronML.
int pds_neuronml_init_weight_uniform(PdsNeuronML *NeuronML, PdsUniform *RV)
Inicializa todos los pesos usando una variable aleatoria uniforme.
PdsNeuronML * pds_neuronml_new(const PdsVector *L)
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_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
int pds_neuronml_iterate(PdsNeuronML *NeuronML, const PdsVector *Input, PdsVector *Output)
Itera la red neuronal multicapa NeuronML. .
float PdsNnReal
Definition: pdsnnglobal.h:52
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_neuronml_init_u_uniform(PdsNeuronML *NeuronML, PdsUniform *RV)
Inicializa todos los umbrales U usando una variable aleatoria uniforme.
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