Home | Develop | Download | Contact
example_neuron.c
1 /*
2  * example_neuron.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 
41 #include <stdlib.h>
42 #include <pds/pdssn.h>
43 
44 PdsSnReal load_input_and_iterate_neuron(PdsNIVector *Input,const PdsSnReal *X,PdsSnNatural N,PdsNeuron *Neuron)
45 {
46  PdsSnReal m;
47  // Load data in the input vector
49 
50  // Iterate
51  pds_neuron_iterate(Neuron);
52 
53  // Get the output value
54  pds_neuron_get_output(Neuron,&m);
55  return m;
56 }
57 
58 void evaluate_error_and_update_weight(PdsNeuron *Neuron,PdsSnReal Y,PdsSnReal Alpha)
59 {
61  pds_neuron_update_u_and_weight(Neuron,Alpha);
62 }
63 
64 int main(int argc, char** argv)
65 {
66  PdsNeuron *Neuron=NULL;
67  PdsSnReal Out0,Out1;
68  int i=0;
69  PdsSnReal Alpha=0.1;
70 
71  PdsNIVector *Input=NULL;
72  PdsSnReal X0[4]={1.0, 0.5, -0.3, -1.7};
73  PdsSnReal X1[4]={1.0,-1.5, +0.3, -0.7};
74 
75  Input=pds_nivector_new(4);
76  Neuron=pds_neuron_new(4);
77 
78  if( (Input==NULL)||(Neuron==NULL) ) return EXIT_SUCCESS;
79 
80  // Connect the input vector with the input of neuron
82 
83  // Initializing weights
84  pds_neuron_init_u_and_weight_rand(Neuron,-1.0,+1.0);
86 
87  Out1=load_input_and_iterate_neuron(Input,X1,4,Neuron);
88  Out0=load_input_and_iterate_neuron(Input,X0,4,Neuron);
89  printf("Iterando %d:\tY0=%7.3f\tY1=%7.3f\n",i,Out0,Out1);
90 
91  // Updating weights
92  for(i=0;i<200;i++)
93  {
94  load_input_and_iterate_neuron(Input,X0,4,Neuron);
95  evaluate_error_and_update_weight(Neuron,-1.0,Alpha);
96 
97  load_input_and_iterate_neuron(Input,X1,4,Neuron);
98  evaluate_error_and_update_weight(Neuron,+1.0,Alpha);
99  }
100 
101  Out1=load_input_and_iterate_neuron(Input,X1,4,Neuron);
102  Out0=load_input_and_iterate_neuron(Input,X0,4,Neuron);
103  printf("Iterando %d:\tY0=%7.3f\tY1=%7.3f\n",i,Out0,Out1);
104 
106 
107  // Release the memory
108  pds_nivector_destroy(&Input);
109  pds_neuron_destroy(&Neuron);
110 
111  return EXIT_SUCCESS;
112 }
int pds_neuron_connect_input_with_nivector(PdsNeuron *Neuron, const PdsNIVector *NIVector)
Conecta el valor de salida Y de las neuronas NIVector con la entrada X[id] de la neurona Neuron...
int pds_nivector_set_input_from_array(PdsNIVector *NIVector, const PdsSnReal *X, int Nel)
Escribe el valor de la entrada de las Neuronas.
int pds_neuron_update_u_and_weight(PdsNeuron *Neuron, PdsSnReal Alpha)
Actualiza los pesos W[i] de la neurona (using the Delta rule). "e" es el error de la salida de la ...
int pds_neuron_printf_u_and_weight(const PdsNeuron *Neuron)
Imprime los valores de U y los pesos en la Neurona.
int pds_neuron_init_u_and_weight_rand(PdsNeuron *Neuron, PdsSnReal min, PdsSnReal max)
Inicia el vector de pesos y U de la Neurona con valores aleatorios uniformemente distribuido entre mi...
int pds_neuron_get_output(const PdsNeuron *Neuron, PdsSnReal *m)
Devuelve el valor de la salida Y de la Neurona.
void pds_neuron_destroy(PdsNeuron **Neuron)
Libera una neurona de tipo puntero PdsNeuron, y limpia el puntero con NULL.
int pds_neuron_iterate(PdsNeuron *Neuron)
Itera la Neurona Neuron. Adicionalmente (Neuron->Y[1]=0) .
void pds_nivector_destroy(PdsNIVector **NIVector)
Libera un vector de neuronas de tipo puntero PdsNIVector, y limpia el puntero con NULL...
int pds_neuron_evaluate_diff_error(PdsNeuron *Neuron, PdsSnReal y)
Compara el valor de salida de la neurona, con "y", luego lo carga en la variable Y[1] de la neurona ...
La estructura tipo PdsNIVector . Esta estructura genera un arreglo de neuronas. Para usar incluir pds...
Definition: pdsnivector.h:80
La estructura tipo PdsNeuron . Esta estructura genera una neurona de Nel entradas.
Definition: pdsneuron.h:77
float PdsSnReal
Definition: pdssnglobal.h:50
PdsNIVector * pds_nivector_new(PdsSnNatural Nel)
Crea un vector de neuronas de entrada de tipo PdsNIVector e inicia con cero todos los valores...
unsigned int PdsSnNatural
Definition: pdssnglobal.h:60
PdsNeuron * pds_neuron_new(PdsSnNatural Nel)
Crea una neurona de tipo PdsNeuron de Nel entradas.

Enlaces de interés

HomePage Bazaar Download Bug report Ayuda Developer Feed