Home | Develop | Download | Contact
testprog2.c
1 /*
2  * testprog2.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  PdsNeuronML *MLayerTest=NULL;
46  PdsVector *L=NULL;
47  PdsVector *X=NULL;
48  PdsVector *Y=NULL;
49  PdsUniform *RV=NULL;
50 
51  PdsNnNatural i,Ntests,Ntraining,count=0;
52  PdsNnNatural MaxIter,IterN,IterP;
53  PdsNnReal Error;
54  PdsNnReal Alpha;
55 
56  FILE *fd=NULL;
57 
58  // Creo una Random Variable para
59  RV=pds_uniform_new(-1,1);
60 
61  // Creo el vector que indicará la cantidad de electrones por capa.
62  // 5 Capas.
63  L=pds_vector_new(6);
64  L->V[0]=32; L->V[1]=64; L->V[2]=42; L->V[3]=28; L->V[4]=14; L->V[5]=7;
65 
66  // Creo El vector de entrada X.
67  X=pds_vector_new(L->V[0]);
68  // Creo El vector de entrada Y.
69  Y=pds_vector_new(L->V[5]);
70 
72 
73  Ntests=9;
74  Ntraining=200;
75 
76  MLayer=pds_neuronml_new(L);
78  pds_neuronml_init_u_uniform(MLayer,RV);
79  Alpha=0.6; pds_neuronml_set_alpha(MLayer,Alpha);
80  Error=0.2; pds_neuronml_set_max_error(MLayer,Error);
81  MaxIter=8192; pds_neuronml_set_max_iterations(MLayer,MaxIter);
83 
84  // Entrenando
85  for(i=0;count<Ntraining;i++)
86  {
87  printf("\nEntrenamiento %4d\n",i);
88 
89  // Entrenando de forma negativa.
90  function_data_work_init_data0(X,Y);
91  pds_neuronml_training(MLayer,X,Y);
92  IterN=pds_neuronml_training_results_printf(MLayer,"-1");
93 
94 
95  // Entrenando de forma positiva.
96  function_data_work_init_data1(X,Y);
97  pds_neuronml_training(MLayer,X,Y);
98  IterP=pds_neuronml_training_results_printf(MLayer,"+1");
99 
100  if((IterN==0)&&(IterP==0)) count++;
101  else count=0;
102 
103  }
104 
105  fd=fopen("data.body.txt","w");
106  pds_neuronml_body_fprintf(MLayer,fd);
107  fclose(fd);
108 
109  MLayerTest=pds_neuronml_new(L);
110 
111  fd=fopen("data.body.txt","r");
112  pds_neuronml_body_fscanf(MLayerTest,fd);
113  fclose(fd);
114 
115 
116 
117  printf("\n\n");
118  // Iterando
119  for(i=0;i<Ntests;i++)
120  {
121  function_data_work_init_data1(X,Y);
122  pds_neuronml_iterate(MLayerTest,X,Y);
123  pds_neuronml_iterate_results_printf(MLayerTest,"+1");
124  function_octplot_vector_in_png(X,"n","X[n]","octfilegood.m","xgood.png");
125  }
126 
127 
128  printf("\n");
129  for(i=0;i<Ntests;i++)
130  {
131  function_data_work_init_data0(X,Y);
132  pds_neuronml_iterate(MLayerTest,X,Y);
133  pds_neuronml_iterate_results_printf(MLayerTest,"-1");
134  function_octplot_vector_in_png(X,"n","X[n]","octfilebad.m","xbad.png");
135  }
136 
137 
138 
139  pds_neuronml_free(MLayer);
140  pds_neuronml_free(MLayerTest);
141 
142  printf("\n");
143 
144  pds_vector_free(X);
145  pds_vector_free(Y);
146  pds_vector_free(L);
147  pds_uniform_free(RV);
148 
149  return EXIT_SUCCESS;
150 }
151 
int pds_neuronml_enable_printf(PdsNeuronML *NeuronML)
Habilita la muestra en pantalla del avance del aprendizaje.
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_body_fscanf(PdsNeuronML *NeuronML, FILE *fd)
Lee de un archivo de texto los pesos W[i], el valor de U. Ocupando una linea cada uno...
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_body_fprintf(const PdsNeuronML *NeuronML, FILE *fd)
Guarda en un archivo de texto los pesos W[i], el valor de U. Ocupando una linea cada uno...
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