neuron_difKd_2cc_7x5.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <memory.h>
  7. #include "neuron_difKd2cc_7x5.h"
  8. /* -------------------------------------------------------------------------------
  9. Lecture des differrentes Look Up Tables
  10. ------------------------------------------------------------------------------- */
  11. void neuron_lect_LUTs_dif()
  12. {
  13. FILE *fic;
  14. int i,j,poub;
  15. char *ligne=malloc(sizeof(char)*150);
  16. float fpoub;
  17. if( (fic=fopen(LUT_POIDS,"r")) == NULL) {perror(LUT_POIDS); exit(-1);}
  18. fgets(ligne,150,fic);
  19. for(i=0; i<DNC1; i++)
  20. fscanf(fic,"%d %d %f",&poub,&poub,&b1[i]);
  21. for(i=0; i<DNC2; i++)
  22. fscanf(fic,"%d %d %f",&poub,&poub,&b2[i]);
  23. fscanf(fic,"%d %d %f",&poub,&poub,&b3);
  24. for(j=0; j<DNE; j++){
  25. for(i=0; i<DNC1; i++)
  26. fscanf(fic,"%d %d %f",&poub,&poub,&w1[j][i]);
  27. }
  28. for(j=0; j<DNC1; j++){
  29. for(i=0; i<DNC2; i++)
  30. fscanf(fic,"%d %d %f",&poub,&poub,&w2[j][i]);
  31. }
  32. for(i=0; i<DNC2; i++)
  33. fscanf(fic,"%d %d %f",&poub,&poub,&w3[i]);
  34. fclose(fic);
  35. if( (fic=fopen(LUT_MOY,"r")) == NULL) {perror(LUT_MOY); exit(-1);}
  36. fscanf(fic,"%f",&fpoub);
  37. for(i=0; i<DNE; i++)
  38. fscanf(fic,"%f",&moy[i]);
  39. for(i=0; i<3; i++)
  40. fscanf(fic,"%f",&fpoub);
  41. fscanf(fic,"%f",&moy[DNES-1]);
  42. fclose(fic);
  43. if( (fic=fopen(LUT_ECART,"r")) == NULL) {perror(LUT_ECART); exit(-1);}
  44. fscanf(fic,"%f",&fpoub);
  45. for(i=0; i<DNE; i++)
  46. fscanf(fic,"%f",&ecart[i]);
  47. for(i=0; i<3; i++)
  48. fscanf(fic,"%f",&fpoub);
  49. fscanf(fic,"%f",&ecart[DNES-1]);
  50. fclose(fic);
  51. }
  52. /* -------------------------------------------------------------------------------
  53. Calcul du Kd a partir des poids
  54. - Input:
  55. input[DNE] = Rrs (412) 443 490 510 555 670 ASOL
  56. ------------------------------------------------------------------------------- */
  57. float neuron_passe_avant_dif(float input[DNE+1])
  58. {
  59. float a[DNC1], b[DNC2], y=0.0, x[DNE];
  60. int i,j;
  61. /* Normalisation */
  62. for(i=0; i<DNE; i++){
  63. x[i] = ((2./3.)*(input[i]-moy[i]))/ecart[i];
  64. }
  65. for(i=0;i<DNC1;i++){
  66. a[i] = 0.0;
  67. for(j=0;j<DNE;j++){
  68. a[i] += (x[j]*w1[j][i]);
  69. }
  70. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + b1[i]));
  71. }
  72. for(i=0;i<DNC2;i++){
  73. b[i] = 0.0;
  74. for(j=0;j<DNC1;j++){
  75. b[i] += (a[j]*w2[j][i]);
  76. }
  77. b[i] = 1.715905*(float)tanh((2./3.)*(double)(b[i] + b2[i]));
  78. }
  79. for(j=0;j<DNC2;j++){
  80. y += (b[j]*w3[j]);
  81. }
  82. /* Denormalisation */
  83. y = 1.5*(y + b3)*ecart[DNES-1] + moy[DNES-1];
  84. y = (float)pow(10.,y);
  85. return(y);
  86. }
  87. /*
  88. int main (int argc, char *argv[])
  89. {
  90. FILE *fic;
  91. float data_in[DNE],result_in,result_out,res_norm, sum_rms=0., sum_rel=0.;
  92. int i,nb,lu;
  93. if( (fic=fopen("LUTS/base_Kd490_IOCCG_NOMAD_all_log_Kd_test.dat","r")) == NULL) {perror("input"); exit(-1);}
  94. neuron_lect_LUTs();
  95. nb = 0;
  96. while((lu=fscanf(fic,"%f",&data_in[0])) == 1){
  97. for(i=1; i<DNE; i++)
  98. fscanf(fic,"%f",&data_in[i]);
  99. fscanf(fic,"%f",&result_in);
  100. res_norm = ((2./3.)*(result_in-moy[DNES-1]))/ecart[DNES-1];
  101. result_out = passe_avant(data_in);
  102. result_in = 1.5*res_norm*ecart[DNES-1] + moy[DNES-1];
  103. result_out = (float)pow(10.,result_out);
  104. result_in = (float)pow(10.,result_in);
  105. sum_rms += (float)pow((double)(result_in-result_out),2.);
  106. sum_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  107. printf("%f %f\n",result_in,result_out);
  108. nb++;
  109. }
  110. printf("rmse = %f, rel err= %f nb = %d\n",(float)sqrt((double)sum_rms/nb),sum_rel/nb,nb);
  111. fclose(fic);
  112. exit(1);
  113. }*/