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++ - Problema compilazione
Forum - C/C++ - Problema compilazione

Avatar
web_pirate (Normal User)
Rookie


Messaggi: 51
Iscritto: 27/12/2011

Segnala al moderatore
Postato alle 16:23
Lunedì, 02/01/2012
Sto cercando di compilare questo sorgente:
Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define file_name "training_set.txt"
  6. #define LEARNING_RATE 0.5
  7.  
  8. float sig(float x){
  9.         return 1/(1+pow(M_E, -x));
  10. }
  11.  
  12. float *rete_neurale(float *input, float **w_x_h, float *bias_h, float *neur_h, float **w_h_y, float *bias_y, int n_output){
  13. float *output=malloc(n_output*sizeof(float));
  14. float somma=0;
  15. int i, j;
  16. int n_in=sizeof(input)/sizeof(input[0]);
  17. int n_h=sizeof(neur_h)/sizeof(neur_h[0]);
  18.  
  19.         for(i=0;n_h;i++){
  20.                 neur_h[i]=0;
  21.         }
  22.  
  23.         for(i=0;i<n_in;i++){
  24.                 for(j=0;j<n_h;j++){
  25.                         if(i==n_in-1){
  26.                                 neur_h[j]+=input[i]*w_x_h[i][j]+bias_h[j];
  27.                                 neur_h[j]=sig(neur_h[j]);
  28.                         }else{
  29.                                 neur_h[j]+=input[i]*w_x_h[i][j];
  30.                         }
  31.                 }
  32.         }
  33.  
  34.         for(i=0;i<n_h;i++){
  35.                 for(j=0;j<n_output;j++){
  36.                         if(i==n_h-1){
  37.                                 output[j]+=neur_h[i]*w_h_y[i][j]+bias_y[j];
  38.                                 output[j]=sig(output[j]);
  39.                         }else{
  40.                                 output[j]+=neur_h[i]*w_h_y[i][j];
  41.                         }
  42.                 }
  43.         }
  44.  
  45. return output;
  46. }
  47.  
  48. float aggiusta_peso(float peso, float delta, float learning_rate, float in){
  49.         return peso+(delta*learning_rate*in);
  50. }
  51.  
  52. float aggiusta_bias(float bias, float delta, float err){
  53.         return bias+delta*err;
  54. }
  55.  
  56. float max(float *arr){
  57. int i;
  58. float max1=1;
  59.  
  60.         for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){
  61.                 if(arr[i]>max1){
  62.                         max1=arr[i];
  63.                 }
  64.         }
  65.  
  66. return max1;
  67. }
  68.  
  69. int main(){
  70. int i, j, e, y;
  71. int n_in=2, n_h=2, n_out=2, n_es=2, epoch;
  72. float input[n_in];
  73. float w_x_h[n_in][n_h];
  74. float delta_h[n_h];
  75. float bias_h[n_h];
  76. float neur_h[n_h];
  77. float w_h_y[n_h][n_out];
  78. float delta_y[n_out];
  79. float bias_y[n_out];
  80. float *output;
  81. float err_out[n_out], err_h[n_h], global_err;
  82. float es_in[n_es][n_in], es_out[n_es][n_out];
  83. float max;
  84. FILE *fp;
  85.  
  86.         printf("\nInput: ");
  87.         scanf("%d", &n_in);
  88.  
  89.         printf("\nNeuroni strato nascosto: ");
  90.         scanf("%d", &n_h);
  91.  
  92.         printf("\nOutput: ");
  93.         scanf("%d", &n_out);
  94.  
  95.         printf("\nNumero elementi training set: ");
  96.         scanf("%d", &n_es);
  97.  
  98.         printf("\nEpoche di apprendimento: ");
  99.         scanf("%d", &epoch);
  100.  
  101.         for(i=0;i<n_in;i++){
  102.                 for(j=0;j<n_h;j++){
  103.                         w_x_h[i][j]=0.5;
  104.                 }
  105.         }
  106.  
  107.         for(i=0;i<n_h;i++){
  108.                 for(j=0;j<n_out;j++){
  109.                         w_h_y[i][j]=0.5;
  110.                 }
  111.         }
  112.  
  113.         for(i=0;i<n_h;i++){
  114.                 bias_h[i]=0.5;
  115.         }
  116.  
  117.         for(i=0;i<n_out;i++){
  118.                 bias_y[i]=0.5;
  119.         }
  120.  
  121.         fp=fopen(file_name, "r");
  122.        
  123.         if(!fp){
  124.                 printf("\nImpossibile aprire il training set.\n");
  125.                 return 0;
  126.         }else{
  127.                 printf("\nAccesso al training set.");
  128.         }
  129.  
  130.         fseek(fp, 0, SEEK_SET);
  131.  
  132.         for(e=0;e<n_es;e++){
  133.                 for(i=0;i<n_in;i++){
  134.                         fscanf(fp, "%f", &es_in[e][i]);
  135.                 }
  136.  
  137.                 for(i=0;i<n_out;i++){
  138.                         fscanf(fp, "%f", &es_out[e][i]);
  139.                 }
  140.         }
  141.  
  142.         for(e=0;e<epoch;e++){
  143.                 printf("\n");
  144.                 global_err=0;
  145.                 for(i=0;i<n_es;i++){
  146.                         for(j=0;j<n_in;j++){
  147.                                 input[j]=es_in[i][j];
  148.                         }
  149.  
  150.                         output=rete_neurale(input, w_x_h, bias_h, neur_h, w_h_y, bias_y, n_out);
  151.                        
  152.                         for(j=0;j<n_out;j++){
  153.                                 err_out[j]=es_out[i][j]-output[j];
  154.                         }
  155.  
  156.                         for(j=0;j<n_out;j++){
  157.                                 delta_y[j]=err_out[j]*(output[j]*(1-output[j]));
  158.                         }
  159.  
  160.                         for(j=0;j<n_h;j++){
  161.                                 err_h[j]=0;
  162.                         }
  163.  
  164.                         for(j=0;j<n_h;j++){
  165.                                 for(y=0;y<n_out;y++){
  166.                                         err_h[j]=err_h[j]+(delta_y[y]*w_h_y[j][y]);
  167.                                 }
  168.                         }
  169.  
  170.                         for(j=0;j<n_h;j++){
  171.                                 delta_h[j]=err_h[j]*(neur_h[j]*(1-neur_h[j]));
  172.                         }
  173.  
  174.                         for(j=0;j<n_h;j++){
  175.                                 for(y=0;y<n_out;y++){
  176.                                         w_h_y[j][y]=aggiusta_peso(w_h_y[j][y], delta_y[y], LEARNING_RATE, neur_h[j]);
  177.                                 }
  178.                         }
  179.  
  180.                         for(j=0;j<n_out;j++){
  181.                                 bias_y[j]=aggiusta_bias(bias_y[j], delta_y[j], err_out[j]);
  182.                         }
  183.  
  184.                         for(j=0;j<n_in;j++){
  185.                                 for(y=0;y<n_h;y++){
  186.                                         w_x_h[j][y]=aggiusta_peso(w_x_h[j][y], delta_h[y], LEARNING_RATE, input[j]);
  187.                                 }
  188.                         }
  189.  
  190.                         for(j=0;j<n_h;j++){
  191.                                 bias_h[j]=aggiusta_bias(bias_h[j], delta_h[j], err_h[j]);
  192.                         }
  193.  
  194.                 printf("\n");
  195.                 for(j=0;j<n_in;j++){
  196.                         printf("%f ", es_in[i][j]);
  197.                 }
  198.                 printf("=");
  199.                 for(j=0;j<n_out;j++){
  200.                         printf("%f (%f)", output[j], es_out[i][j]);
  201.                 }
  202.                 }
  203.         }
  204.                                        
  205. return 0;
  206. }


ma gcc mi da come messaggio:
Codice sorgente - presumibilmente Delphi

  1. neural.c: In function ‘main’:
  2. neural.c:150:4: warning: passing argument 2 of ‘rete_neurale’ from incompatible pointer type [enabled by default]
  3. neural.c:12:8: note: expected ‘float **’ but argument is of type ‘float (*)[(long unsigned int)(n_h)]’
  4. neural.c:150:4: warning: passing argument 5 of ‘rete_neurale’ from incompatible pointer type [enabled by default]
  5. neural.c:12:8: note: expected ‘float **’ but argument is of type ‘float (*)[(long unsigned int)(n_out)]


Qual'è il problema? O.o

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 17:47
Lunedì, 02/01/2012
Se leggi il messaggio d'errore c'è scritto che

nella riga 150, quindi in

output=rete_neurale(input, w_x_h, bias_h, neur_h, w_h_y, bias_y, n_out);

il secondo parametro, quindi

w_x_h

è di tipo incompatibile con quanto si aspetta la funzione.

Ed in effetti il compilatore ha ragione.


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Avatar
web_pirate (Normal User)
Rookie


Messaggi: 51
Iscritto: 27/12/2011

Segnala al moderatore
Postato alle 19:47
Lunedì, 02/01/2012
Il warning non lo da più se metto un (float**) prima di w_x_h. E' la soluzione giusta? Ho sempre avuto dei dubbi su questo "argomento", dove posso approfondire? Comunque ora, scomparsi i warning, quando eseguo il programma, arriva alla funzione rete_neurale(...) e mi fa un errore di segmentazione. Dite che ho sbagliato ad allocare la memoria per l'array di ritorno "output" della suddetta funzione?

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 20:25
Lunedì, 02/01/2012
Testo quotato

Postato originariamente da web_pirate:

Il warning non lo da più se metto un (float**) prima di w_x_h. E' la soluzione giusta?



No

Testo quotato

Ho sempre avuto dei dubbi su questo "argomento", dove posso approfondire?



Tutti i libri di C (spero tu ne abbia studiato almeno uno) parlando di array/matrici/puntatori.

Testo quotato

Comunque ora, scomparsi i warning, quando eseguo il programma, arriva alla funzione rete_neurale(...) e mi fa un errore di segmentazione.



Ovvio, non hai affatto risolto ...

Testo quotato

Dite che ho sbagliato ad allocare la memoria per l'array di ritorno "output" della suddetta funzione?



No ... il problema è che se il parametro è un doppio puntatore, tu devi passare un doppio puntatore. Altrimenti devi cambiare il tipo di parametro.


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote