Advertisement
Hexadroid

Untitled

Nov 20th, 2020
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <random>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <iostream>
  7. #include <windows.h>
  8.  
  9. const int nr_neurons_A = 56;
  10. const int nr_neurons_B = 64;
  11. const int nr_neurons_C = 64;
  12. const int nr_neurons_D = 64;
  13. const int nr_neurons_E = 64;
  14. const int nr_neurons_F = 64;
  15. const int nr_neurons_G = 56;
  16. int tota = nr_neurons_A + nr_neurons_B + nr_neurons_C + nr_neurons_D + nr_neurons_E + nr_neurons_F + nr_neurons_G;
  17.  
  18. int Z;
  19. int A, B, C, D, E, F, G;
  20. int loops = 266000;
  21. int showtime = 0.50*loops;
  22. int zrows = 15;
  23. int temp, i1, i2, rows, shuffled;
  24. int i, j;
  25. int showoutputs;
  26.  
  27. double xDD, gDD, fDD, eDD, dDD, cDD, bDD, zDD;
  28. double bDD_xupd, bDD_xbupd;
  29. double cDD_wupd, cDD_bupd;
  30. double dDD_wupd, dDD_bupd;
  31. double eDD_wupd, eDD_bupd;
  32. double fDD_wupd, fDD_bupd;
  33. double gDD_wupd, gDD_bupd;
  34. double zDD_wupd, zDD_bupd;
  35.  
  36. double membDD_xupd[nr_neurons_A][nr_neurons_B];
  37. double memBWC_xupd[nr_neurons_B][nr_neurons_C];
  38. double memCWD_xupd[nr_neurons_C][nr_neurons_D];
  39. double memDWD_xupd[nr_neurons_D][nr_neurons_E];
  40. double memEWD_xupd[nr_neurons_E][nr_neurons_F];
  41. double memFWD_xupd[nr_neurons_F][nr_neurons_G];
  42. double memGWD_xupd[nr_neurons_G];
  43.  
  44. double sqcalc, gemiddelde;
  45. double TOTAL, TOTALcCoD, TOTALao, TOTALbo, TOTALco, TOTALdo, TOTALeo, TOTALfo;
  46. double learn = 0.00000000000000000005;
  47. double xerr[nr_neurons_G], xerrtotal;
  48. double sqerrtot = 0;
  49.  
  50. bool training_mode = false;
  51.  
  52. double train_input[100][nr_neurons_A];
  53. double train_output[100][nr_neurons_G];
  54. double requested1[nr_neurons_A];
  55.  
  56. struct MiddleNode {
  57.  
  58.  std::vector<double> w = std::vector<double>(256, 0.00);
  59.  std::vector<double> o = std::vector<double>(256, 0.00);
  60.  std::vector<double> bw = std::vector<double>(256, 0.00);
  61.  std::vector<double> b = std::vector<double>(256, 0.00);
  62.  
  63. };
  64.  
  65. struct InputNode {
  66.  
  67.  std::vector<double> w = std::vector<double>(256, 0.00);
  68.  std::vector<double> o = std::vector<double>(256, 0.00);
  69.  std::vector<double> bw = std::vector<double>(256, 0.00);
  70.  std::vector<double> b = std::vector<double>(256, 0.00);
  71.  
  72. };
  73.  
  74. struct OutputNode {
  75.  
  76.  std::vector<double> w = std::vector<double>(256, 0.00);
  77.  std::vector<double> o = std::vector<double>(256, 0.00);
  78.  std::vector<double> bw = std::vector<double>(256, 0.00);
  79.  std::vector<double> b = std::vector<double>(256, 0.00);
  80.  
  81. };
  82.  
  83. double choose(int x, int aa) {
  84.  
  85.   if (training_mode) return train_input[x][aa];
  86.   else return requested1[aa];
  87.  
  88. }
  89.  
  90.  
  91. double initialize() {
  92.  
  93.   return ((rand()%10000)/10000.00);
  94.  
  95. }
  96.  
  97. double fd(double x) {
  98.  
  99.   return (x >= 0) ? (1) : (0);
  100.  
  101. }
  102.  
  103. double fx(double x) {
  104.  
  105.   return (x >= 0) ? (x) : (0);
  106.  
  107. }
  108.  
  109. int _tmain(int argc, _TCHAR * argv[]) {
  110.  
  111.   srand (time(NULL));
  112.  
  113.   std::random_device rd;
  114.   std::mt19937 gh(rd());
  115.  
  116.   learn = learn / tota;
  117.  
  118.  
  119.   InputNode a[nr_neurons_A];
  120.   MiddleNode b[nr_neurons_B];
  121.   MiddleNode c[nr_neurons_C];
  122.   MiddleNode d[nr_neurons_D];
  123.   MiddleNode e[nr_neurons_E];
  124.   MiddleNode f[nr_neurons_F];
  125.   OutputNode g[nr_neurons_G];
  126.  
  127.   for (i = 0; i < zrows; i++) {
  128.  
  129.     for (j = 0; j < nr_neurons_A; j++) {
  130.  
  131.       train_input[i][j] = (i + 1.00) / (j + 1);
  132.       train_output[i][j] = 2.33 * pow(train_input[i][j],2);
  133.  
  134.     }
  135.  
  136.   }
  137.  
  138.   for (j = 0; j < nr_neurons_A; j++) {
  139.  
  140.     requested1[j] = 1.50 / (j + 1.00);
  141.  
  142.   }
  143.  
  144.   double input = 0;
  145.  
  146.   int i1, i2;
  147.  
  148.   for (i1 = 0; i1 < nr_neurons_A; i1++)
  149.     for (i2 = 0; i2 < nr_neurons_B; i2++) {
  150.       a[i1].w[i2] = initialize();
  151.       a[i1].bw[i2] = initialize();
  152.       a[i1].b[i2] = initialize();
  153.     }
  154.  
  155.   for (i1 = 0; i1 < nr_neurons_B; i1++)
  156.     for (i2 = 0; i2 < nr_neurons_C; i2++) {
  157.       b[i1].w[i2] = initialize();
  158.       b[i1].bw[i2] = initialize();
  159.       b[i1].b[i2] = initialize();
  160.     }
  161.  
  162.   for (i1 = 0; i1 < nr_neurons_C; i1++)
  163.     for (i2 = 0; i2 < nr_neurons_D; i2++) {
  164.       c[i1].w[i2] = initialize();
  165.       c[i1].bw[i2] = initialize();
  166.       c[i1].b[i2] = initialize();
  167.     }
  168.  
  169.   for (i1 = 0; i1 < nr_neurons_D; i1++)
  170.     for (i2 = 0; i2 < nr_neurons_E; i2++) {
  171.       d[i1].w[i2] = initialize();
  172.       d[i1].bw[i2] = initialize();
  173.       d[i1].b[i2] = initialize();
  174.     }
  175.  
  176.   for (i1 = 0; i1 < nr_neurons_E; i1++)
  177.     for (i2 = 0; i2 < nr_neurons_F; i2++) {
  178.       e[i1].w[i2] = initialize();
  179.       e[i1].bw[i2] = initialize();
  180.       e[i1].b[i2] = initialize();
  181.     }
  182.  
  183.   for (i1 = 0; i1 < nr_neurons_F; i1++)
  184.     for (i2 = 0; i2 < nr_neurons_G; i2++) {
  185.       f[i1].w[i2] = initialize();
  186.       f[i1].bw[i2] = initialize();
  187.       f[i1].b[i2] = initialize();
  188.     }
  189.  
  190.   for (i1 = 0; i1 < nr_neurons_G; i1++) {
  191.     g[i1].w[0] = initialize();
  192.     g[i1].bw[0] = initialize();
  193.     g[i1].b[0] = initialize();
  194.   }
  195.  
  196.   sqcalc = 99999999.99;
  197.   for (temp = 0; temp < loops; temp++) {
  198.  
  199.  
  200.  
  201.  
  202.     //for (rows = 0; rows < zrows+1; rows++)
  203.     rows = rand() % (zrows + 1); {
  204.  
  205.       shuffled = rows;
  206.  
  207.       if (shuffled == zrows) training_mode = false;
  208.       else training_mode = true;;
  209.  
  210.  
  211.  
  212.       TOTALbo = 0; TOTALao = 0;
  213.       for (C = 0; C < nr_neurons_C; C++) {
  214.  
  215.         for (B = 0; B < nr_neurons_B; B++) {
  216.           TOTAL = 0;
  217.           for (A = 0; A < nr_neurons_A; A++) {
  218.  
  219.             if (C == 0) {
  220.               TOTALao += (a[A].o[B] = fx(choose(shuffled, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ ));
  221.  
  222.              }
  223.  
  224.             TOTAL += a[A].o[B] * b[B].w[C];
  225.           }
  226.  
  227.           TOTALbo += (b[B].o[C] = fx(TOTAL /*+ b[B].bw[C] * b[B].b[C]*/ ));
  228.  
  229.  
  230.         }
  231.  
  232.       }
  233.  
  234.       TOTALdo = 0;
  235.       TOTALco = 0;
  236.  
  237.       for (E = 0; E < nr_neurons_E; E++) {
  238.         for (D = 0; D < nr_neurons_D; D++) {
  239.           TOTAL = 0;
  240.           for (C = 0; C < nr_neurons_C; C++) {
  241.  
  242.             if (E == 0) {
  243.  
  244.               double TOTALcCoD = 0;
  245.               for (B = 0; B < nr_neurons_B; B++) {
  246.                 TOTALcCoD += b[B].o[C] * c[C].w[D];
  247.               }
  248.  
  249.               TOTALco += (c[C].o[D] = fx(TOTALcCoD /*+ c[C].bw[D] * c[C].b[D]*/ ));
  250.  
  251.  
  252.             }
  253.  
  254.             TOTAL += c[C].o[D] * d[D].w[E];
  255.  
  256.           }
  257.  
  258.           TOTALdo += (d[D].o[E] = fx(TOTAL /*+ d[D].bw[E] * d[D].b[E]*/ ));
  259.  
  260.         }
  261.  
  262.       }
  263.  
  264.       TOTALeo = 0;
  265.       for (F = 0; F < nr_neurons_F; F++) {
  266.         for (E = 0; E < nr_neurons_E; E++) {
  267.           TOTAL = 0;
  268.           for (D = 0; D < nr_neurons_D; D++) {
  269.             TOTAL += d[D].o[E] * e[E].w[F];
  270.           }
  271.           TOTALeo += (e[E].o[F] = fx(TOTAL /*+ e[E].bw[F] * e[E].b[F]*/ ));
  272.  
  273.  
  274.         }
  275.  
  276.       }
  277.  
  278.       TOTALfo = 0;
  279.       for (G = 0; G < nr_neurons_G; G++) {
  280.         for (F = 0; F < nr_neurons_F; F++) {
  281.           TOTAL = 0;
  282.           for (E = 0; E < nr_neurons_E; E++) {
  283.             TOTAL += e[E].o[F] * f[F].w[G];
  284.           }
  285.  
  286.           TOTALfo += (f[F].o[G] = fx(TOTAL /*+ f[F].bw[G] * f[F].b[G]*/ ));
  287.  
  288.  
  289.         }
  290.  
  291.       }
  292.  
  293.       for (G = 0; G < nr_neurons_G; G++) {
  294.         TOTAL = 0;
  295.         for (F = 0; F < nr_neurons_F; F++) {
  296.           TOTAL += f[F].o[G] * g[G].w[0];
  297.         }
  298.  
  299.         g[G].o[0] = fx(TOTAL /*+ g[G].bw[0] * g[G].b[0]*/ );
  300.  
  301.       }
  302.  
  303.       if (temp > showtime) {
  304.  
  305.         if (training_mode) {
  306.           printf("\n-");
  307.           for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  308.  
  309.             printf("\n input: %.2lf The current output_%d for training set[%d] is %.28lf of (%lf)", train_input[shuffled][showoutputs], showoutputs, shuffled, g[showoutputs].o[0], train_output[shuffled][showoutputs]);
  310.  
  311.           }
  312.         } else {
  313.  
  314.           for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  315.  
  316.             printf("\n(loop: % d) output_%d for requested data (%.28lf) is currently : %.8lf  (should be:%lf)", temp, showoutputs, requested1[showoutputs], g[showoutputs].o[0], 2.00 * pow(requested1[showoutputs],2) );
  317.  
  318.           }
  319.  
  320.         }
  321.  
  322.       }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.       xerrtotal = 0;
  330.       if (training_mode) {
  331.         for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  332.           xerr[showoutputs] = 2 * (g[showoutputs].o[0] - train_output[shuffled][showoutputs]);
  333.  
  334.           xerrtotal += (xerr[showoutputs]);
  335.         }
  336.  
  337.         sqerrtot = xerrtotal;
  338.  
  339.  
  340.  
  341.         xDD = learn * xerrtotal;
  342.  
  343.  
  344.         for (G = 0; G < nr_neurons_G; G++) {
  345.  
  346.           zDD_wupd = TOTALfo * learn * xerr[G];
  347.  
  348.           gDD = xDD * g[G].w[0];
  349.           gDD_wupd = TOTALeo * gDD;
  350.           g[G].w[0] -= fd(g[G].o[0]) * zDD_wupd * nr_neurons_G;
  351.  
  352.  
  353.           for (F = 0; F < nr_neurons_F; F++) {
  354.             fDD = f[F].w[G] * gDD;
  355.             fDD_wupd = TOTALdo * fDD;
  356.             f[F].w[G] -= fd(f[F].o[G]) * gDD_wupd * nr_neurons_F / xerrtotal * xerr[G];
  357.  
  358.             }
  359.         }
  360.  
  361.  
  362.  
  363.  
  364.          for (F = 0; F < nr_neurons_F; F++) {
  365.  
  366.  
  367.             for (E = 0; E < nr_neurons_E; E++) {
  368.               eDD = e[E].w[F] * fDD;
  369.               eDD_wupd = TOTALco * eDD;
  370.               e[E].w[F] -= fd(e[E].o[F]) * fDD_wupd * nr_neurons_E;
  371.  
  372.               }
  373.          }
  374.  
  375.  
  376.  
  377.             for (E = 0; E < nr_neurons_E; E++) {
  378.  
  379.  
  380.               for (D = 0; D < nr_neurons_D; D++) {
  381.                 dDD = d[D].w[E] * eDD;
  382.                 dDD_wupd = TOTALbo * dDD;
  383.                 d[D].w[E] -= fd(d[D].o[E]) * eDD_wupd * nr_neurons_D;
  384.  
  385.                 }
  386.             }
  387.  
  388.  
  389.           for (D = 0; D < nr_neurons_D; D++) {
  390.  
  391.  
  392.                 for (C = 0; C < nr_neurons_C; C++) {
  393.                   cDD = c[C].w[D] * dDD;
  394.                   cDD_wupd = cDD * TOTALao;
  395.                   c[C].w[D] -= fd(c[C].o[D]) * dDD_wupd * nr_neurons_C;
  396.  
  397.                   }
  398.           }
  399.  
  400.  
  401.            for (C = 0; C < nr_neurons_C; C++) {
  402.  
  403.  
  404.                   for (B = 0; B < nr_neurons_B; B++) {
  405.                     bDD = b[B].w[C] * cDD;
  406.                     b[B].w[C] -= fd(b[B].o[C]) * cDD_wupd * nr_neurons_B;
  407.  
  408.                     }
  409.            }
  410.  
  411.  
  412.  
  413.  
  414.             for (B = 0; B < nr_neurons_B; B++) {
  415.  
  416.  
  417.                     for (A = 0; A < nr_neurons_A; A++) {
  418.  
  419.                       a[A].w[B] -= train_input[shuffled][A] * fd(a[A].o[B]) * bDD * nr_neurons_A;
  420.  
  421.                       }
  422.             }
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.       }
  432.  
  433.  
  434.     }
  435.  
  436.  
  437.  
  438.     if (!(temp % (showtime/100))) printf("\n %d %%",++Z);
  439.  
  440.     sqcalc += sqerrtot;
  441.  
  442.     if (temp % 1000 == 0) {
  443.       gemiddelde = sqcalc / 1000;
  444.       sqcalc = 0;
  445.     }
  446.  
  447.     sqerrtot = 0;
  448.  
  449.   }
  450.  
  451.   printf("\nEnd.");
  452.   scanf("%d", &Z);
  453.  
  454.   return 0;
  455.  
  456. }
  457.  
  458.  
  459.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement