Th3NiKo

Sieci neuronowe - ćwiczenia 6

Apr 24th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <conio.h>
  6. using namespace std;
  7.  
  8. float randomFloat(float minimum, float maximum){
  9.     return minimum + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(maximum-minimum)));
  10. }
  11.  
  12. float rounded(float number){
  13.     return floor((number * 100) + .5) / 100;
  14. }
  15.  
  16.  
  17. //Stale
  18. float c = 0.8f;
  19. float epsilon = 0.00001f;
  20. float beta = 1.0f;
  21.  
  22. //Zmienne
  23. float x[3][25] = {};
  24. float xp[3][25] = {};
  25. float xpp[3][25] = {};
  26.  
  27. float y[3][16] = {};
  28.  
  29. float w[16][25] = {};
  30. float wp[25][16] = {};
  31.  
  32. float b[16] = {};
  33. float bp[25] = {};
  34.  
  35. float DE_wp[25][16] = {};
  36. float DE_bp[25] = {};
  37. float DE_w[16][25] = {};
  38. float DE_b[16] = {};
  39.  
  40. bool train = true;
  41.  
  42.  
  43.  
  44. //Funkcje
  45. float f(float u){
  46.     return 1.0f / (1.0f + exp((-1.0f) * beta * u));
  47. }
  48.  
  49. float Df(float u){
  50.     return beta * f(u) * (1.0f - f(u));
  51. }
  52.  
  53. float f1(float u){
  54.     if(u >= 0) return 1.0f;
  55.     else       return 0.0f;
  56. }
  57.  
  58. void randomizeValues(){
  59.     for(int i = 0; i < 25; i++){
  60.         bp[i] = randomFloat(-0.5f, 0.5f);
  61.         for(int j =0; j < 16; j++){
  62.             wp[i][j] = randomFloat(-1.0f, 1.0f);
  63.         }
  64.     }
  65.     for(int i = 0; i < 16; i++){
  66.         b[i] = randomFloat(-0.5f, 0.5f);
  67.         for(int j =0; j < 25; j++){
  68.             w[i][j] = randomFloat(-1.0f, 1.0f);
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74.  
  75. void calculateY(){
  76.     float suma = 0;
  77.     for(int a = 0; a < 3; a++){
  78.         for(int i = 0; i < 16; i++){
  79.             suma = 0;
  80.             for(int j = 0; j < 25; j++){
  81.                 suma += w[i][j] * x[a][j] + b[i];
  82.             }
  83.             y[a][i] = f(suma);
  84.         }
  85.     }
  86. }
  87.  
  88. void calculateXp(){
  89.     float suma = 0;
  90.     for(int a = 0; a < 3; a++){
  91.         for(int k = 0; k < 25; k++){
  92.             suma = 0;
  93.             for(int i = 0;i < 16; i++){
  94.                 suma += wp[k][i] * y[a][i] + bp[k];
  95.             }
  96.             xp[a][k] = f(suma);
  97.         }
  98.     }
  99. }
  100.  
  101. void calculateXpp(){
  102.     float suma = 0;
  103.     for(int a = 0; a < 3; a++){
  104.         for(int k = 0; k < 25; k++){
  105.             suma = 0;
  106.             for(int i = 0; i < 16; i++){
  107.                 suma += wp[k][i] * y[a][i] + bp[k];
  108.             }
  109.             xpp[a][k] = f1(suma);
  110.         }
  111.  
  112.     }
  113. }
  114.  
  115. void calculateA(){ //?
  116.     for(int p = 0; p < 25; p++){
  117.         float sumaB = 0;
  118.         for(int q = 0; q < 16; q++){
  119.             float sumaGlowna = 0;
  120.             for(int a = 0; a < 3; a++){
  121.                 float sumaPrim = 0;
  122.                 for(int i = 0; i < 16; i++){
  123.                     sumaPrim += wp[p][i] * y[a][i] + bp[p];
  124.                 }
  125.                 sumaGlowna += (xp[a][p] - x[a][p]) * Df(sumaPrim) * y[a][q];
  126.                 if(q == 0)
  127.                 sumaB += (xp[a][p] - x[a][p]) * Df(sumaPrim);
  128.             }
  129.             DE_wp[p][q] = sumaGlowna;
  130.         }
  131.         DE_bp[p] = sumaB;
  132.     }
  133. }
  134.  
  135. void calculateB(){
  136.     for(int p = 0; p < 16; p++){
  137.         float sumaB = 0;
  138.         for(int q = 0; q < 25; q++){
  139.             float sumaGlowna = 0;
  140.             for(int a = 0; a < 3; a++){
  141.                 float sumaSrodek = 0;
  142.                 for(int k = 0; k < 25; k++){
  143.                     float suma1 = 0;
  144.                     for(int i = 0; i < 16; i++){
  145.                         suma1 += wp[k][i] * y[a][i] + bp[k];
  146.                     }
  147.                     float suma2 = 0;
  148.                     for(int j = 0; j < 25; j++){
  149.                         suma2 += w[p][j] * x[a][j] + b[p];
  150.                     }
  151.                     sumaSrodek += (xp[a][k] - x[a][k]) * Df(suma1) * wp[k][p] * Df(suma2) * x[a][q];
  152.                     if(q == 0)
  153.                     sumaB += (xp[a][k] - x[a][k]) * Df(suma1) * wp[k][p] * Df(suma2);
  154.                 }
  155.                 sumaGlowna += sumaSrodek;
  156.             }
  157.         DE_w[p][q] = sumaGlowna;
  158.         }
  159.         DE_b[p] = sumaB;
  160.     }
  161. }
  162.  
  163. int main()
  164. {
  165.     srand (static_cast <unsigned> (time(0)));
  166.     randomizeValues();
  167.     //Obrazki
  168.     x[0][6] = 1; x[0][7] = 1; x[0][12] = 1; x[0][17] = 1; x[0][22] = 1;
  169.     x[1][2] = 1; x[1][3] = 1; x[1][8] = 1; x[1][13] = 1;
  170.     x[2][5] = 1; x[2][6] = 1; x[2][11] = 1; x[2][16] = 1; x[2][21] = 1;
  171.  
  172.     int counter = 0;
  173.     while(train){
  174.  
  175.         calculateY();
  176.         calculateXp();
  177.         calculateA();
  178.         calculateB();
  179.  
  180.         float _bp[25] = {};
  181.         float _b[16]= {};
  182.         float _w[16][25]= {};
  183.         float _wp[25][16]= {};
  184.  
  185.         //Do we have maksimum?
  186.         float max1 = 0.0f;
  187.         float max2 = 0.0f;
  188.  
  189.         //New weights
  190.         for(int i = 0; i < 25; i++){
  191.             _bp[i] = bp[i] - c * DE_bp[i];
  192.             if(abs(_bp[i] - bp[i]) >= max1){
  193.                 max2 = _bp[i] - bp[i];
  194.             }
  195.             bp[i] = _bp[i];
  196.             for(int j = 0; j < 16; j++){
  197.                 _wp[i][j] = wp[i][j] - c * DE_wp[i][j];
  198.                 if(abs(_wp[i][j] - wp[i][j]) >= max1){
  199.                     max1 = abs(_wp[i][j] - wp[i][j]);
  200.                 }
  201.                 wp[i][j] = _wp[i][j];
  202.             }
  203.         }
  204.  
  205.         for(int i = 0; i < 16; i++){
  206.  
  207.             _b[i] = b[i] - c * DE_b[i];
  208.             if(abs(_b[i] - b[i]) >= max2){
  209.                 max2 = _b[i] - b[i];
  210.             }
  211.             b[i] = _b[i];
  212.             for(int j = 0; j < 25; j++){
  213.                 _w[i][j] = w[i][j] - c * DE_w[i][j];
  214.                 if(abs(_w[i][j] - w[i][j]) >= max2){
  215.                     max2 = abs(_w[i][j] - w[i][j]);
  216.                 }
  217.                 w[i][j] = _w[i][j];
  218.             }
  219.         }
  220.  
  221.         if(counter % 500 == 0)
  222.         cout << max1 << " " << max2 << endl;
  223.         if(max1 <= epsilon || max2 <= epsilon){
  224.             train = false;
  225.         }
  226.         counter++;
  227.     }
  228.     cout << "Liczba iteracji: " << counter << endl << endl;
  229.     calculateXpp();
  230.     for(int a = 0; a < 3; a++){
  231.         for(int i = 0; i < 25; i++){
  232.  
  233.             if(i % 5 == 0 && i != 0){
  234.                 cout << endl;
  235.             }
  236.             if(xpp[a][i] >= 0.5f){
  237.                 cout << "*";
  238.             } else {
  239.                 cout << "#";
  240.             }
  241.         }
  242.         cout << endl << endl;
  243.     }
  244.  
  245.  
  246.     getch();
  247.     return 0;
  248. }
Add Comment
Please, Sign In to add comment