Advertisement
Hexadroid

mach

Nov 16th, 2020
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.54 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. //256 inputs, 256 outputs, faster: 5 blocks of 64 neurons
  10. const int nr_neurons_A = 256;
  11. const int nr_neurons_B = 64;
  12. const int nr_neurons_C = 64;
  13. const int nr_neurons_D = 64;
  14. const int nr_neurons_E = 64;
  15. const int nr_neurons_F = 64;
  16. const int nr_neurons_G = 256;
  17.  
  18. int Z;
  19. int A, B, C, D, E, F, G;
  20. int loops = 26600;
  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.000000000000000000001;        // this many neurons one needs to make a very tiny learning rate, or the results will be 0 or inf
  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. //remove cursor blinking
  117. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  118. CONSOLE_CURSOR_INFO CursoInfo;
  119. CursoInfo.dwSize = 1;
  120. CursoInfo.bVisible = false;
  121. SetConsoleCursorInfo(hConsole, &CursoInfo);
  122.  
  123.  
  124.  
  125.   InputNode a[nr_neurons_A];
  126.   MiddleNode b[nr_neurons_B];
  127.   MiddleNode c[nr_neurons_C];
  128.   MiddleNode d[nr_neurons_D];
  129.   MiddleNode e[nr_neurons_E];
  130.   MiddleNode f[nr_neurons_F];
  131.   OutputNode g[nr_neurons_G];
  132.  
  133.   for (i = 0; i < zrows; i++) {
  134.  
  135.     for (j = 0; j < nr_neurons_A; j++) {
  136.  
  137.       train_input[i][j] = (i + 1.00) / (j + 1);
  138.       train_output[i][j] = 2.00 * train_input[i][j];
  139.  
  140.     }
  141.  
  142.   }
  143.  
  144.   for (j = 0; j < nr_neurons_A; j++) {
  145.  
  146.     requested1[j] = 1.50 / (j + 1.00);
  147.  
  148.   }
  149.  
  150.   double input = 0;
  151.  
  152.   int i1, i2;
  153.  
  154.   for (i1 = 0; i1 < nr_neurons_A; i1++)
  155.     for (i2 = 0; i2 < nr_neurons_B; i2++) {
  156.       a[i1].w[i2] = initialize();
  157.       a[i1].bw[i2] = initialize();
  158.       a[i1].b[i2] = initialize();
  159.     }
  160.  
  161.   for (i1 = 0; i1 < nr_neurons_B; i1++)
  162.     for (i2 = 0; i2 < nr_neurons_C; i2++) {
  163.       b[i1].w[i2] = initialize();
  164.       b[i1].bw[i2] = initialize();
  165.       b[i1].b[i2] = initialize();
  166.     }
  167.  
  168.   for (i1 = 0; i1 < nr_neurons_C; i1++)
  169.     for (i2 = 0; i2 < nr_neurons_D; i2++) {
  170.       c[i1].w[i2] = initialize();
  171.       c[i1].bw[i2] = initialize();
  172.       c[i1].b[i2] = initialize();
  173.     }
  174.  
  175.   for (i1 = 0; i1 < nr_neurons_D; i1++)
  176.     for (i2 = 0; i2 < nr_neurons_E; i2++) {
  177.       d[i1].w[i2] = initialize();
  178.       d[i1].bw[i2] = initialize();
  179.       d[i1].b[i2] = initialize();
  180.     }
  181.  
  182.   for (i1 = 0; i1 < nr_neurons_E; i1++)
  183.     for (i2 = 0; i2 < nr_neurons_F; i2++) {
  184.       e[i1].w[i2] = initialize();
  185.       e[i1].bw[i2] = initialize();
  186.       e[i1].b[i2] = initialize();
  187.     }
  188.  
  189.   for (i1 = 0; i1 < nr_neurons_F; i1++)
  190.     for (i2 = 0; i2 < nr_neurons_G; i2++) {
  191.       f[i1].w[i2] = initialize();
  192.       f[i1].bw[i2] = initialize();
  193.       f[i1].b[i2] = initialize();
  194.     }
  195.  
  196.   for (i1 = 0; i1 < nr_neurons_G; i1++) {
  197.     g[i1].w[0] = initialize();
  198.     g[i1].bw[0] = initialize();
  199.     g[i1].b[0] = initialize();
  200.   }
  201.  
  202.   sqcalc = 99999999.99;
  203.   for (temp = 0; temp < loops; temp++) {
  204.    // printf("\nn %d",temp);
  205.  
  206.     //std::shuffle(v.begin(), v.end(), gh);
  207.  
  208.     //for (rows = 0; rows < zrows+1; rows++)
  209.     rows = rand() % (zrows + 1); {
  210.  
  211.       shuffled = rows; // =v[rows];
  212.  
  213.       if (shuffled == zrows) training_mode = false;
  214.       else training_mode = true;;
  215.  
  216.       // if (training_mode) input = train_input[shuffled]; else input = requested0;
  217.  
  218.       TOTALbo = 0; TOTALao = 0;
  219.       for (C = 0; C < nr_neurons_C; C++) {
  220.  
  221.         for (B = 0; B < nr_neurons_B; B++) {
  222.           TOTAL = 0;
  223.           for (A = 0; A < nr_neurons_A; A++) {
  224.  
  225.             if (C == 0) {                                                           //bias handling
  226.               TOTALao += (a[A].o[B] = fx(choose(shuffled, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ ));
  227.              membDD_xupd[A][B] = train_input[shuffled][A] * fd(a[A].o[B]);
  228.              }
  229.  
  230.             TOTAL += a[A].o[B] * b[B].w[C];
  231.           }
  232.  
  233.           TOTALbo += (b[B].o[C] = fx(TOTAL /*+ b[B].bw[C] * b[B].b[C]*/ ));
  234.           memBWC_xupd[B][C] = fd(b[B].o[C]);
  235.  
  236.         }
  237.  
  238.       }
  239.  
  240.       TOTALdo = 0;
  241.       TOTALco = 0;
  242.  
  243.       for (E = 0; E < nr_neurons_E; E++) {
  244.         for (D = 0; D < nr_neurons_D; D++) {
  245.           TOTAL = 0;
  246.           for (C = 0; C < nr_neurons_C; C++) {
  247.  
  248.             if (E == 0) {
  249.  
  250.               double TOTALcCoD = 0;
  251.               for (B = 0; B < nr_neurons_B; B++) {
  252.                 TOTALcCoD += b[B].o[C] * c[C].w[D];
  253.               }
  254.  
  255.               TOTALco += (c[C].o[D] = fx(TOTALcCoD /*+ c[C].bw[D] * c[C].b[D]*/ ));
  256.               memCWD_xupd[C][D] = fd(c[C].o[D]);
  257.  
  258.             }
  259.  
  260.             TOTAL += c[C].o[D] * d[D].w[E];
  261.             memDWD_xupd[D][E] = fd(d[D].o[E]);
  262.           }
  263.  
  264.           TOTALdo += (d[D].o[E] = fx(TOTAL /*+ d[D].bw[E] * d[D].b[E]*/ ));
  265.  
  266.         }
  267.  
  268.       }
  269.  
  270.       TOTALeo = 0;
  271.       for (F = 0; F < nr_neurons_F; F++) {
  272.         for (E = 0; E < nr_neurons_E; E++) {
  273.           TOTAL = 0;
  274.           for (D = 0; D < nr_neurons_D; D++) {
  275.             TOTAL += d[D].o[E] * e[E].w[F];
  276.           }
  277.           TOTALeo += (e[E].o[F] = fx(TOTAL /*+ e[E].bw[F] * e[E].b[F]*/ ));
  278.           memEWD_xupd[E][F] = fd(e[E].o[F]);
  279.  
  280.         }
  281.  
  282.       }
  283.  
  284.       TOTALfo = 0;
  285.       for (G = 0; G < nr_neurons_G; G++) {
  286.         for (F = 0; F < nr_neurons_F; F++) {
  287.           TOTAL = 0;
  288.           for (E = 0; E < nr_neurons_E; E++) {
  289.             TOTAL += e[E].o[F] * f[F].w[G];
  290.           }
  291.  
  292.           TOTALfo += (f[F].o[G] = fx(TOTAL /*+ f[F].bw[G] * f[F].b[G]*/ ));
  293.           memFWD_xupd[F][G] = fd(f[F].o[G]);
  294.  
  295.         }
  296.  
  297.       }
  298.  
  299.       for (G = 0; G < nr_neurons_G; G++) {
  300.         TOTAL = 0;
  301.         for (F = 0; F < nr_neurons_F; F++) {
  302.           TOTAL += f[F].o[G] * g[G].w[0];
  303.         }
  304.  
  305.         g[G].o[0] = fx(TOTAL /*+ g[G].bw[0] * g[G].b[0]*/ );
  306.         memGWD_xupd[G] = fd(g[G].o[0]);
  307.       }
  308.  
  309.       if (temp > showtime) {
  310.  
  311.         if (training_mode) {
  312.           printf("\n-");
  313.           for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  314.  
  315.             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]);
  316.  
  317.           }
  318.         } else {
  319.  
  320.           for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  321.  
  322.             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 * requested1[showoutputs]);
  323.  
  324.           }
  325.  
  326.         }
  327.  
  328.       }
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.       xerrtotal = 0;
  336.       if (training_mode) {
  337.         for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  338.           xerr[showoutputs] = 2 * (g[showoutputs].o[0] - train_output[shuffled][showoutputs]);
  339.  
  340.           xerrtotal += (xerr[showoutputs]);
  341.         }
  342.  
  343.         sqerrtot = xerrtotal;
  344.  
  345.  
  346.  
  347.         xDD = learn * xerrtotal;
  348.  
  349.         //removed : loop -in loop -in loop etc:   v*w*x*y*z  vs  v*w + w*x + x*y + y*z
  350.         for (G = 0; G < nr_neurons_G; G++) {
  351.  
  352.           zDD_wupd = TOTALfo * learn * xerr[G];
  353.           //zDD_bupd = gamma * learn * xerr[G];
  354.  
  355.           gDD = xDD * g[G].w[0];
  356.           gDD_wupd = TOTALeo * gDD;
  357.           g[G].w[0] -= memGWD_xupd[G] * zDD_wupd;
  358.           //gDD_bupd = gamma * gDD;
  359.           //g[G].bw[0] -= fd(g[G].o[0]) * zDD_bupd;   //code for bias processing, however, this part is not perfect as it mucks up the results.
  360.  
  361.           for (F = 0; F < nr_neurons_F; F++) {
  362.             fDD = f[F].w[G] * gDD;
  363.             fDD_wupd = TOTALdo * fDD;
  364.             f[F].w[G] -= memFWD_xupd[F][G] * gDD_wupd;
  365.             //fDD_bupd = gamma * fDD;
  366.             //f[F].bw[G] -= fd(f[F].o[G]) * gDD_bupd;
  367.             }
  368.         }
  369.  
  370.  
  371.  
  372.  
  373.          for (F = 0; F < nr_neurons_F; F++) {
  374.  
  375.  
  376.             for (E = 0; E < nr_neurons_E; E++) {
  377.               eDD = e[E].w[F] * fDD;
  378.               eDD_wupd = TOTALco * eDD;
  379.               e[E].w[F] -= memEWD_xupd[E][F] * fDD_wupd;
  380.               //eDD_bupd = gamma * eDD;
  381.               //e[E].bw[F] -= fd(e[E].o[F]) * fDD_bupd ;
  382.               }
  383.          }
  384.  
  385.  
  386.  
  387.             for (E = 0; E < nr_neurons_E; E++) {
  388.  
  389.  
  390.               for (D = 0; D < nr_neurons_D; D++) {
  391.                 dDD = d[D].w[E] * eDD;
  392.                 dDD_wupd = TOTALbo * dDD;
  393.                 d[D].w[E] -= memDWD_xupd[D][E] * eDD_wupd;
  394.                 //dDD_bupd = gamma * dDD;
  395.                 //d[D].bw[E] -= fd(d[D].o[E]) * eDD_bupd ;
  396.                 }
  397.             }
  398.  
  399.  
  400.           for (D = 0; D < nr_neurons_D; D++) {
  401.  
  402.  
  403.                 for (C = 0; C < nr_neurons_C; C++) {
  404.                   cDD = c[C].w[D] * dDD;
  405.                   cDD_wupd = cDD * TOTALao;
  406.                   c[C].w[D] -= memCWD_xupd[C][D] * dDD_wupd;
  407.                   //cDD_bupd = cDD * gamma;
  408.                   //c[C].bw[D] -= fd(c[C].o[D]) * dDD_bupd ;
  409.                   }
  410.           }
  411.  
  412.  
  413.            for (C = 0; C < nr_neurons_C; C++) {
  414.  
  415.  
  416.                   for (B = 0; B < nr_neurons_B; B++) {
  417.                     bDD = b[B].w[C] * cDD;
  418.                     b[B].w[C] -= memBWC_xupd[B][C] * cDD_wupd;
  419.                     //bDD_wupd : can't use totals like TOTALbo for A, using xupd
  420.                     //b[B].bw[C] -= fd(b[B].o[C]) * cDD_bupd ;
  421.                     }
  422.            }
  423.  
  424.  
  425.  
  426.  
  427.             for (B = 0; B < nr_neurons_B; B++) {
  428.  
  429.  
  430.                     for (A = 0; A < nr_neurons_A; A++) {
  431.  
  432.                       //bDD_xupd = membDD_xupd[A][B] * bDD;
  433.                       a[A].w[B] -= membDD_xupd[A][B] * bDD;
  434.                       //a[A].bw[B] -= bDD_xupd * gamma;
  435.                       }
  436.             }
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.       }
  446.  
  447.  
  448.     }
  449.  
  450.  
  451.  
  452.     if (!(temp % (showtime/100))) printf("\n %d %%",++Z);
  453.  
  454.     sqcalc += sqerrtot;
  455.  
  456.     if (temp % 1000 == 0) {
  457.       gemiddelde = sqcalc / 1000;
  458.       sqcalc = 0;
  459.     }
  460.  
  461.     sqerrtot = 0;
  462.  
  463.   }
  464.  
  465.   printf("\nEnd.");
  466.   scanf("%d", &Z);
  467.  
  468.   return 0;
  469.  
  470. }
  471.  
  472.  
  473. //machine learning, a.i. tryout
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement