Th3NiKo

Cwiczenia 5 - Neuronowe

Apr 14th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>    // std::copy
  3. #include <math.h>
  4. #include <cmath>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. using namespace std;
  8.  
  9. //Control Variables
  10. bool flag = false;
  11. float beta = 1.00;
  12. float c = 1.0;
  13. float epsilon = 0.000001;
  14.  
  15. //Vectors
  16. float z[4] = {0.0, 1.0, 1.0, 0.0};
  17. float s[3] = {0.0, 1.0, 2.0};
  18. float u[4][3] = {};
  19. float w[2][3] = {};
  20.  
  21. float x1[4] = {};
  22. float x2[4] = {};
  23. float x3[4] = {1.0, 1.0, 1.0, 1.0};
  24. float x[3][3] = {};
  25. float y[4] = {};
  26.  
  27. float si[3] = {};
  28. float s_new[3] = {};
  29. float wij[2][3] = {};
  30. float w_new[2][3] = {};
  31.  
  32.  
  33. float F(float u){ //FUNKCJA
  34.     return 1.0 / (1.0 + exp((-1.0) * beta * u));
  35. }
  36.  
  37. float derivativeF(float u){ //POCHODNA FUNKCJI
  38.     return beta * (1 - F(u));
  39. }
  40.  
  41. void calculateX1(float w[][3], float u[][3]){
  42.     for (int p = 0; p < 4; p++)
  43.     {
  44.         x1[p] = F(w[0][0] * u[p][0] + w[0][1] * u[p][1] + w[0][2] * u[p][2]);
  45.     }
  46. }
  47.  
  48. void calculateX2(float w[][3], float u[][3]){
  49.     for (int p = 0; p < 4; p++)
  50.     {
  51.         x2[p] = F(w[1][0] * u[p][0] + w[1][1] * u[p][1] + w[1][2] * u[p][2]);
  52.     }
  53. }
  54.  
  55. void X(float x1[], float x2[], float x3[]){
  56.     x[0][0] = x1[0];
  57.     x[0][1] = x1[1];
  58.     x[0][2] = x1[2];
  59.     x[1][0] = x2[0];
  60.     x[1][1] = x2[1];
  61.     x[1][2] = x2[2];
  62.     x[2][0] = x3[0];
  63.     x[2][1] = x3[1];
  64.     x[2][2] = x3[2];
  65. }
  66.  
  67. void calculateY(float s[], float x[][3]){
  68.     for (int p = 0; p < 4; p++)
  69.     {
  70.         y[p] = F(s[0] * x[0][p] + s[1] * x[1][p] + s[2] * x[2][p]);
  71.     }
  72. }
  73.  
  74. void calculateSi(float y[], float z[], float s[], float x[][3]){
  75.     for (int i = 0; i < 3; i++)
  76.     {
  77.         for (int p = 0; p < 4; p++)
  78.         {
  79.             si[i] += (y[p] - z[p]) * derivativeF(s[0] * x[0][p] + s[1] * x[1][p] + s[2] * x[2][p]) * x[i][p];
  80.         }
  81.     }
  82. }
  83.  
  84. void calculateWij(float y[], float z[], float s[], float w[][3], float u[][3], float x[][3]){
  85.     float u1[4] = {};
  86.     float u2[4] = {};
  87.  
  88.     for(int i = 0; i < 2; i++){ //zeruj
  89.         for(int j = 0; j < 3; j++){
  90.             wij[i][j] = 0.0;
  91.         }
  92.     }
  93.  
  94.     for (int p = 0; p < 4; p++)
  95.     {
  96.         u1[p] = s[0] * x[0][p] + s[1] * x[1][p] + s[2] * x[2][p];
  97.     }
  98.  
  99.     for (int i = 0; i < 2; i++)
  100.     {
  101.         for (int p = 0; p < 4; p++)
  102.         {
  103.             u2[p] += w[i][0] * u[p][0] + w[i][1] * u[p][1] + w[i][2] * u[p][2];
  104.         }
  105.     }
  106.  
  107.     for (int i = 0; i < 2; i++)
  108.     {
  109.         for (int j = 0; j < 3; j++)
  110.         {
  111.             for (int p = 0; p < 4; p++)
  112.             {
  113.                 wij[i][j] += (y[p] - z[p]) * derivativeF(u1[p]) * s[i] * derivativeF(u2[p]) * u[p][j];
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119. bool maximum(float w_new[][3], float w[][3], float s_new[], float s[]){
  120.     float max1 = 0.0;
  121.     float max2 = 0.0;
  122.     for (int i = 0; i < 3; i++)
  123.     {
  124.         if (abs(s_new[i] - s[i]) >= max1)
  125.         {
  126.             max1 = abs(s_new[i] - s[i]);
  127.         }
  128.     }
  129.  
  130.     for (int i = 0; i < 2; i++)
  131.     {
  132.         for (int j = 0; j < 3; j++)
  133.         {
  134.             if (abs(w_new[i][j] - w[i][j]) >= max2)
  135.             {
  136.                 max2 = abs(w_new[i][j] - w[i][j]);
  137.             }
  138.         }
  139.     }
  140.     if ((max1 < epsilon) || (max2 < epsilon))
  141.         return true;
  142.     else
  143.         return false;
  144. }
  145.  
  146. void trainNet(){
  147.  
  148. //Metoda gradientu
  149. int counter = 0;
  150. while(flag != true){
  151.     calculateX1(w,u);
  152.     calculateX2(w,u);
  153.     X(x1,x2,x3);
  154.     calculateY(s,x);
  155.     calculateSi(y, z, s, x);
  156.     calculateWij(y, z, s, w, u, x);
  157.     s_new[0] = s[0] - c * si[0];
  158.     s_new[1] = s[1] - c * si[1];
  159.     s_new[2] = s[2] - c * si[2];
  160.     w_new[0][0] = w[0][0] - c * wij[0][0];
  161.     w_new[0][1] = w[0][1] - c * wij[0][1];
  162.     w_new[0][2] = w[0][2] - c * wij[0][2];
  163.     w_new[1][0] = w[1][0] - c * wij[1][0];
  164.     w_new[1][1] = w[1][1] - c * wij[1][1];
  165.     w_new[1][2] = w[1][2] - c * wij[1][2];
  166.     flag = maximum(w_new, w, s_new, s); //Did we find it
  167.     for(int i = 0; i < 3; i++){
  168.         s[i] = s_new[i];
  169.     }
  170.  
  171.     for(int i = 0; i < 2; i++){
  172.         for(int j = 0; j < 3; j++){
  173.             w[i][j] = w_new[i][j];
  174.         }
  175.     }
  176.     counter++;
  177. } //FINISHED WHILE
  178.     cout << "Zakonczono. Liczba wykonanych iteracji to: " << counter << endl;
  179.     cout << "Wagi:" << endl;
  180.     for (int i = 0; i < 2; i++)
  181.     {
  182.         for (int j = 0; j < 3; j++)
  183.         {
  184.             cout << "w"  << (i + 1) << (j + 1) << " = " << w_new[i][j] << "   ";
  185.         }
  186.         cout << endl;
  187.     }
  188.     for (int i = 0; i < 3; i++)
  189.     {
  190.         cout << "s" << (i + 1) << " = " << s_new[i] << "   ";
  191.     }
  192.  
  193.     cout << endl << endl;
  194.     cout << "  XOR:" << endl;
  195.     cout << "  0 0 | "; printf("%f\n", y[0]);
  196.     cout << "  1 0 | "; printf("%f\n", y[1]);
  197.     cout << "  0 1 | "; printf("%f\n", y[2]);
  198.     cout << "  1 1 | "; printf("%f\n", y[3]);
  199.  
  200. }
  201.  
  202. int main()
  203. {
  204.     //U FILL
  205.     u[0][0] = 0; u[0][1] = 0; u[0][2] = 1;
  206.     u[1][0] = 1; u[1][1] = 0; u[1][2] = 1;
  207.     u[2][0] = 0; u[2][1] = 1; u[2][2] = 1;
  208.     u[3][0] = 1; u[3][1] = 1; u[3][2] = 1;
  209.  
  210.     //STARTING WEIGHTS
  211.     for (int i = 0; i < 2; i++)
  212.     {
  213.         w[i][0] = 0;
  214.         w[i][1] = 1;
  215.         w[i][2] = 2;
  216.     }
  217.  
  218.     trainNet();
  219.  
  220.  
  221.  
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment