Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Rete Neurale
Forum - C/C++ - Rete Neurale

Avatar
web_pirate (Normal User)
Rookie


Messaggi: 51
Iscritto: 27/12/2011

Segnala al moderatore
Postato alle 1:35
Venerdì, 30/12/2011
Sinceramente non sapevo se aprire una discussione o una domanda. :asd: Se ho sbagliato mi scuso in anticipo. Salve a tutti, sono nuovo nel forum. Da un mesetto sto cercando di scrivere un programma che sfrutti una rete neurale e che riesca ad addestrarsi con un set di esempi. Così, per prova, ho cercato di scriverne una che impara a fare le somme. Il programma sembra funzionare, ma quando passo i diversi esempi alcuni la rete li svolge e il risultato e minimo, mentre altri hanno un errore che supera addirittura le 10 unità. Quale potrebbe essere il problema?
Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define n_es 10
  5. #define n_in 2
  6. #define n_h 3
  7. #define LEARN_RATE 0.5
  8.  
  9. float input[n_in];
  10. float w_x_h[n_in][n_h];
  11. float bias_x[n_h];
  12. float d_x_h[n_h];
  13. float neur_h[n_h];
  14. float w_h_y[n_h];
  15. float bias_y=0.1;
  16. float d_h_y;
  17. float err_out, err_h[n_h], global_err;
  18. float es_in[n_es][n_in], es_out[n_es];
  19. float max_n=1;
  20.  
  21. float sig(float x){
  22.         return 1/(1+pow(M_E, -x));
  23. }
  24.  
  25. float return_rete(float *input){
  26. int i, j;
  27. float somma=0;
  28. float output;
  29.  
  30.         for(i=0;i<n_h;i++){
  31.                 neur_h[i]=0;
  32.         }
  33.  
  34.         for(i=0;i<n_in;i++){
  35.                 for(j=0;j<n_h;j++){
  36.                         neur_h[j]+=input[i]*w_x_h[i][j];
  37.                 }
  38.         }
  39.  
  40.         for(i=0;i<n_h;i++){
  41.                 neur_h[i]+=bias_x[i];
  42.                 neur_h[i]=sig(neur_h[i]);
  43.         }
  44.  
  45.         output=0;
  46.  
  47.         for(i=0;i<n_h;i++){
  48.                 output+=neur_h[i]*w_h_y[i];
  49.         }
  50.  
  51.         output+=bias_y;
  52.         output=sig(output);
  53.  
  54. return output;
  55. }
  56.  
  57. void set_w(){
  58. int i, j;
  59.        
  60.         for(i=0;i<n_in;i++){
  61.                 for(j=0;j<n_h;j++){
  62.                         w_x_h[i][j]=0.2;
  63.                         w_h_y[j]=0.2;
  64.                 }
  65.         }
  66. }
  67.  
  68. void set_d(){
  69. int i, j;
  70.  
  71.         for(j=0;j<n_h;j++){
  72.                 d_x_h[j]=0;
  73.         }
  74. }
  75.  
  76. float aggiusta_peso(float peso, float delta, float learning_rate, float in){
  77.         return peso+(delta*learning_rate*in);
  78. }
  79.  
  80. float max(float *arr){
  81. int i;
  82. float max1=1;
  83.        
  84. bias_x[0]=0.5;
  85. bias_x[1]=0.5;
  86. bias_x[2]=0.5;
  87.  
  88.         for(i=0;i<n_es;i++){
  89.                 if(arr[i]>max1){
  90.                         max1=arr[i];
  91.                 }
  92.         }
  93.  
  94. return max1;
  95. }
  96.  
  97. int main(){
  98. int i, j, y, e;
  99. float output;
  100. float prova[2];
  101.        
  102.         for(i=0;i<n_es;i++){
  103.                 es_in[i][0]=rand() % 100;
  104.                 es_in[i][1]=rand() % 100;
  105.                 es_out[i]=es_in[i][0]+es_in[i][1];
  106.         }
  107.  
  108.         max_n=max(es_out);
  109.  
  110.         for(i=0;i<n_es;i++){
  111.                 es_in[i][0]=es_in[i][0]/max_n;
  112.                 es_in[i][1]=es_in[i][1]/max_n;
  113.                 es_out[i]=es_out[i]/max_n;
  114.         }
  115.  
  116.         for(e=0;e<3000;e++){
  117.                 printf("\n");
  118.                 global_err=0;
  119.                 for(y=0;y<n_es;y++){
  120.                         for(i=0;i<n_in;i++){
  121.                                 input[i]=es_in[y][i];
  122.                         }
  123.  
  124.                         output=return_rete(input);
  125.  
  126.                         err_out=es_out[y]-output;
  127.  
  128.                         d_h_y=err_out*(output*(1-output));
  129.  
  130.                         for(i=0;i<n_h;i++){
  131.                                 err_h[i]=d_h_y*w_h_y[i];
  132.                         }
  133.  
  134.                         set_d();
  135.        
  136.                         for(j=0;j<n_h;j++){
  137.                                 d_x_h[j]+=err_h[j]*(neur_h[j]*(1-neur_h[j]));
  138.                         }
  139.  
  140.                         for(i=0;i<n_h;i++){
  141.                                 w_h_y[i]=aggiusta_peso(w_h_y[i], d_h_y, LEARN_RATE, neur_h[i]);
  142.                         }
  143.                
  144.                         bias_y+=LEARN_RATE*d_h_y;
  145.                
  146.                         for(i=0;i<n_in;i++){
  147.                                 for(j=0;j<n_h;j++){
  148.                                         w_x_h[i][j]=aggiusta_peso(w_x_h[i][j], d_x_h[j], LEARN_RATE, input[i]);
  149.                                 }
  150.                         }
  151.  
  152.                         for(i=0;i<n_h;i++){
  153.                                 bias_x[i]+=LEARN_RATE*d_x_h[i];
  154.                         }
  155.        
  156.                 printf("\n%f + %f = %f (%f)", es_in[y][0]*max_n, es_in[y][1]*max_n, return_rete(es_in[y])*max_n, es_out[y]*max_n);
  157.                
  158.                 }
  159.         }
  160.  
  161. prova[0]=55/max_n;
  162. prova[1]=55/max_n;
  163. printf("\n");
  164. printf("55 + 55 = %f\n", return_rete(prova)*max_n);
  165.  
  166. return 0;
  167. }


PM
Avatar
tasx (Dev Team)
Expert


Messaggi: 439
Iscritto: 15/12/2008

Up
0
Down
V
Segnala al moderatore
Postato alle 11:59
Venerdì, 30/12/2011
Ciao!

Se vuoi eiste una libreria comodissima e facilissima da usare: http://leenissen.dk/fann/wp/


ciaociao!!

Ciao. Guarda la libreria la conosco, ma preferisco scrivere il codice da me. Usando una libreria non capirei cosa sto facendo, mentre il mio obiettivo è proprio imparare. Grazie lo stesso! :) - web_pirate - 30/12/11 13:34
capisco, allora buona fortuna!! ;) - tasx - 30/12/11 14:20
PM