neuron_kd_2cc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "neuron2cc.h"
  8. /* -------------------------------------------------------------------------------
  9. Lecture des differrentes Look Up Tables
  10. ------------------------------------------------------------------------------- */
  11. void neuron_lect_LUTs()
  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<NC1; i++)
  20. fscanf(fic,"%d %d %f",&poub,&poub,&b1[i]);
  21. for(i=0; i<NC2; 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<NE; j++){
  25. for(i=0; i<NC1; i++)
  26. fscanf(fic,"%d %d %f",&poub,&poub,&w1[j][i]);
  27. }
  28. for(j=0; j<NC1; j++){
  29. for(i=0; i<NC2; i++)
  30. fscanf(fic,"%d %d %f",&poub,&poub,&w2[j][i]);
  31. }
  32. for(i=0; i<NC2; 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<NES; i++)
  38. fscanf(fic,"%f",&moy[i]);
  39. fclose(fic);
  40. if( (fic=fopen(LUT_ECART,"r")) == NULL) {perror(LUT_ECART); exit(-1);}
  41. fscanf(fic,"%f",&fpoub);
  42. for(i=0; i<NES; i++)
  43. fscanf(fic,"%f",&ecart[i]);
  44. fclose(fic);
  45. }
  46. /* -------------------------------------------------------------------------------
  47. Calcul du Kd a partir des poids
  48. - Input:
  49. input[NE] = Rrs (412) 443 490 510 555 670 ASOL
  50. ------------------------------------------------------------------------------- */
  51. float neuron_passe_avant(float input[NE+1])
  52. {
  53. float a[NC1], b[NC2], y=0.0, x[NE];
  54. int i,j;
  55. /* Normalisation */
  56. for(i=1; i<=NE; i++){
  57. x[i-1] = ((2./3.)*(input[i]-moy[i-1]))/ecart[i-1];
  58. }
  59. for(i=0;i<NC1;i++){
  60. a[i] = 0.0;
  61. for(j=0;j<NE;j++){
  62. a[i] += (x[j]*w1[j][i]);
  63. }
  64. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + b1[i]));
  65. }
  66. for(i=0;i<NC2;i++){
  67. b[i] = 0.0;
  68. for(j=0;j<NC1;j++){
  69. b[i] += (a[j]*w2[j][i]);
  70. }
  71. b[i] = 1.715905*(float)tanh((2./3.)*(double)(b[i] + b2[i]));
  72. }
  73. for(j=0;j<NC2;j++){
  74. y += (b[j]*w3[j]);
  75. }
  76. /* Denormalisation */
  77. y = 1.5*(y + b3)*ecart[NES-1] + moy[NES-1];
  78. y = (float)pow(10.,y);
  79. return(y);
  80. }
  81. /*
  82. int main (int argc, char *argv[])
  83. {
  84. FILE *fic;
  85. float data_in[NE],result_in,result_out,res_norm, sum_rms=0., sum_rel=0.;
  86. int i,nb,lu;
  87. if( (fic=fopen("LUTS/base_Kd490_IOCCG_NOMAD_all_log_Kd_test.dat","r")) == NULL) {perror("input"); exit(-1);}
  88. neuron_lect_LUTs();
  89. nb = 0;
  90. while((lu=fscanf(fic,"%f",&data_in[0])) == 1){
  91. for(i=1; i<NE; i++)
  92. fscanf(fic,"%f",&data_in[i]);
  93. fscanf(fic,"%f",&result_in);
  94. res_norm = ((2./3.)*(result_in-moy[NES-1]))/ecart[NES-1];
  95. result_out = passe_avant(data_in);
  96. result_in = 1.5*res_norm*ecart[NES-1] + moy[NES-1];
  97. result_out = (float)pow(10.,result_out);
  98. result_in = (float)pow(10.,result_in);
  99. sum_rms += (float)pow((double)(result_in-result_out),2.);
  100. sum_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  101. printf("%f %f\n",result_in,result_out);
  102. nb++;
  103. }
  104. printf("rmse = %f, rel err= %f nb = %d\n",(float)sqrt((double)sum_rms/nb),sum_rel/nb,nb);
  105. fclose(fic);
  106. exit(1);
  107. }*/