Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <random>
- #include <algorithm>
- #include <iterator>
- #include <iostream>
- #include <windows.h>
- const int nr_neurons_A = 4;
- const int nr_neurons_B = 4;
- const int nr_neurons_C = 4;
- const int nr_neurons_D = 4;
- const int nr_neurons_E = 4;
- const int nr_neurons_F = 4;
- const int nr_neurons_G = 4;
- int tota = 4;//nr_neurons_A + nr_neurons_B + nr_neurons_C + nr_neurons_D + nr_neurons_E + nr_neurons_F + nr_neurons_G;
- int Z;
- int A, B, C, D, E, F, G;
- int loops = 5900000;
- int showtime = 0.98*loops;
- int zrows = 99;
- int temp, i1, i2, rows, shuffled;
- int i, j;
- int showoutputs;
- double xDD, gDD, fDD, eDD, dDD, cDD, bDD, zDD;
- double bDD_xupd, bDD_xbupd;
- double cDD_wupd, cDD_bupd;
- double dDD_wupd, dDD_bupd;
- double eDD_wupd, eDD_bupd;
- double fDD_wupd, fDD_bupd;
- double gDD_wupd, gDD_bupd;
- double zDD_wupd, zDD_bupd;
- double membDD_xupd[nr_neurons_A][nr_neurons_B];
- double memBWC_xupd[nr_neurons_B][nr_neurons_C];
- double memCWD_xupd[nr_neurons_C][nr_neurons_D];
- double memDWD_xupd[nr_neurons_D][nr_neurons_E];
- double memEWD_xupd[nr_neurons_E][nr_neurons_F];
- double memFWD_xupd[nr_neurons_F][nr_neurons_G];
- double memGWD_xupd[nr_neurons_G];
- double sqcalc, gemiddelde;
- double TOTAL, TOTALcCoD, TOTALao, TOTALbo, TOTALco, TOTALdo, TOTALeo, TOTALfo;
- double learn = 0.00000001;
- double xerr[nr_neurons_G], xerrtotal;
- double sqerrtot = 0;
- double au_input[5];
- double bu_input[5];
- double cu_input[5];
- double du_input[5];
- double eu_input[5];
- double fu_input[5];
- int tijdelijk = 0;
- bool training_mode = false;
- double train_input[100][nr_neurons_A];
- double train_output[100][nr_neurons_G];
- double requested1[nr_neurons_A];
- double gDD_times, fDD_times, eDD_times, dDD_times, cDD_times, bDD_times;
- double avgtot, aw, bw, cw, dw, ew, fw, gw;
- double tussen, divx;
- double rown[4];
- struct MiddleNode {
- std::vector<double> w = std::vector<double>(256, 0.00);
- std::vector<double> o = std::vector<double>(256, 0.00);
- std::vector<double> bw = std::vector<double>(256, 0.00);
- std::vector<double> b = std::vector<double>(256, 0.00);
- };
- struct InputNode {
- std::vector<double> w = std::vector<double>(256, 0.00);
- std::vector<double> o = std::vector<double>(256, 0.00);
- std::vector<double> bw = std::vector<double>(256, 0.00);
- std::vector<double> b = std::vector<double>(256, 0.00);
- };
- struct OutputNode {
- std::vector<double> w = std::vector<double>(256, 0.00);
- std::vector<double> o = std::vector<double>(256, 0.00);
- std::vector<double> bw = std::vector<double>(256, 0.00);
- std::vector<double> b = std::vector<double>(256, 0.00);
- };
- double choose(int x, int aa) {
- if (training_mode) return train_input[x][aa];
- else return requested1[aa];
- }
- double initialize() {
- return 0.54321123456789 ;
- //return ((rand()%10000)/10000.00) + 0.001;
- }
- double fd(double x) {
- return (x >= 0) ? (1) : (0);
- }
- double fx(double x) {
- return (x >= 0) ? (x) : (0);
- }
- int _tmain(int argc, _TCHAR * argv[]) {
- srand (time(NULL));
- std::random_device rd;
- std::mt19937 gh(rd());
- divx = 0;
- InputNode a[nr_neurons_A];
- MiddleNode b[nr_neurons_B];
- MiddleNode c[nr_neurons_C];
- MiddleNode d[nr_neurons_D];
- MiddleNode e[nr_neurons_E];
- MiddleNode f[nr_neurons_F];
- OutputNode g[nr_neurons_G];
- for (i = 0; i < zrows; i++) {
- for (j = 0; j < nr_neurons_A; j++) {
- train_input[i][j] = (i + 1.00) / (j + 1);
- train_output[i][j] = 2.33 * pow(train_input[i][j],2);
- }
- }
- for (j = 0; j < nr_neurons_A; j++) {
- requested1[j] = 1.50 / (j + 1.00);
- }
- double input = 0;
- int i1, i2;
- for (i1 = 0; i1 < nr_neurons_A; i1++)
- for (i2 = 0; i2 < nr_neurons_B; i2++) {
- a[i1].w[i2] = initialize();
- a[i1].bw[i2] = initialize();
- a[i1].b[i2] = initialize();
- }
- for (i1 = 0; i1 < nr_neurons_B; i1++)
- for (i2 = 0; i2 < nr_neurons_C; i2++) {
- b[i1].w[i2] = initialize();
- b[i1].bw[i2] = initialize();
- b[i1].b[i2] = initialize();
- }
- for (i1 = 0; i1 < nr_neurons_C; i1++)
- for (i2 = 0; i2 < nr_neurons_D; i2++) {
- c[i1].w[i2] = initialize();
- c[i1].bw[i2] = initialize();
- c[i1].b[i2] = initialize();
- }
- for (i1 = 0; i1 < nr_neurons_D; i1++)
- for (i2 = 0; i2 < nr_neurons_E; i2++) {
- d[i1].w[i2] = initialize();
- d[i1].bw[i2] = initialize();
- d[i1].b[i2] = initialize();
- }
- for (i1 = 0; i1 < nr_neurons_E; i1++)
- for (i2 = 0; i2 < nr_neurons_F; i2++) {
- e[i1].w[i2] = initialize();
- e[i1].bw[i2] = initialize();
- e[i1].b[i2] = initialize();
- }
- for (i1 = 0; i1 < nr_neurons_F; i1++)
- for (i2 = 0; i2 < nr_neurons_G; i2++) {
- f[i1].w[i2] = initialize();
- f[i1].bw[i2] = initialize();
- f[i1].b[i2] = initialize();
- }
- for (i1 = 0; i1 < nr_neurons_G; i1++) {
- g[i1].w[0] = initialize();
- g[i1].bw[0] = initialize();
- g[i1].b[0] = initialize();
- }
- sqcalc = 99999999.99;
- for (temp = 0; temp < loops; temp++) {
- //for (rows = 0; rows < zrows+1; rows++) {
- // rows = rand() % (zrows + 1); {
- rows = tijdelijk; {
- tijdelijk ++ ;
- if (tijdelijk>zrows) { tijdelijk=0; rows=0;}
- shuffled = rows;
- if (shuffled == zrows) training_mode = false;
- else training_mode = true;
- std::fill(au_input, au_input+5, 0);
- std::fill(bu_input, bu_input+5, 0);
- std::fill(cu_input, cu_input+5, 0);
- std::fill(du_input, du_input+5, 0);
- std::fill(eu_input, eu_input+5, 0);
- std::fill(fu_input, fu_input+5, 0);
- for (B = 0; B < nr_neurons_B; B++)
- {
- for (A = 0; A < nr_neurons_A; A++)
- {
- a[A].o[B] = fx(choose(shuffled, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ );
- au_input[B] += a[A].o[B]; //the input for B
- }
- }
- for (C = 0; C < nr_neurons_C; C++)
- {
- for (B = 0; B < nr_neurons_B; B++)
- {
- b[B].o[C] = fx( au_input[B] * b[B].w[C]);
- bu_input[C] += b[B].o[C];
- }
- }
- for (D = 0; D < nr_neurons_D; D++)
- {
- for (C = 0; C < nr_neurons_C; C++)
- {
- c[C].o[D] = fx( bu_input[C] * c[C].w[D]);
- cu_input[D] += c[C].o[D];
- }
- }
- for (E = 0; E < nr_neurons_E; E++)
- {
- for (D = 0; D < nr_neurons_D; D++)
- {
- d[D].o[E] = fx( cu_input[D] * d[D].w[E]);
- du_input[E] += d[D].o[E];
- }
- }
- for (F = 0; F < nr_neurons_F; F++)
- {
- for (E = 0; E < nr_neurons_E; E++)
- {
- e[E].o[F] = fx( du_input[E] * e[E].w[F]);
- eu_input[F] += e[E].o[F];
- }
- }
- for (G = 0; G < nr_neurons_G; G++)
- {
- for (F = 0; F < nr_neurons_F; F++)
- {
- f[F].o[G] = fx( eu_input[F] * f[F].w[G]);
- fu_input[G] += f[F].o[G];
- }
- }
- for (G = 0; G < nr_neurons_G; G++)
- {
- g[G].o[0] = fx( fu_input[G] * g[G].w[0]);
- //gu_input[H] ..... N/A
- }
- if (temp > showtime) {
- if (training_mode) {
- printf("\n-");
- for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
- divx = divx + abs(g[showoutputs].o[0] - train_output[shuffled][showoutputs]);
- printf("\n %lf input: %.2lf The current output_%d for training set[%d] is %.28lf of (%lf)",divx, train_input[shuffled][showoutputs], showoutputs, shuffled, g[showoutputs].o[0], train_output[shuffled][showoutputs]);
- }
- } else {
- for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
- 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) );
- }
- }
- }
- xerrtotal = 0;
- if (training_mode) {
- for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
- xerr[showoutputs] = 2 * (g[showoutputs].o[0] - train_output[shuffled][showoutputs]);
- xerrtotal += (xerr[showoutputs]);
- }
- sqerrtot = xerrtotal;
- xDD = learn * xerrtotal;
- gDD_times = xDD;
- avgtot = 0; aw = 0; bw = 0; cw = 0; dw = 0; ew = 0; fw = 0; gw = 0;
- for (G = 0; G < nr_neurons_G; G++) gDD_times *= g[G].w[0];
- fDD_times = gDD_times;
- for (G = 0; G < nr_neurons_G; G++) { for (F = 0; F < nr_neurons_F; F++) fDD_times *= f[F].w[G]; }
- eDD_times = fDD_times;
- for (F = 0; F < nr_neurons_F; F++) { for (E = 0; E < nr_neurons_E; E++) eDD_times *= e[E].w[F]; }
- dDD_times = eDD_times;
- for (E = 0; E < nr_neurons_E; E++) { for (D = 0; D < nr_neurons_D; D++) dDD_times *= d[D].w[E]; }
- cDD_times = dDD_times;
- for (D = 0; D < nr_neurons_D; D++) { for (C = 0; C < nr_neurons_C; C++) cDD_times *= c[C].w[D]; }
- bDD_times = cDD_times;
- for (C = 0; C < nr_neurons_C; C++) { for (B = 0; B < nr_neurons_B; B++) bDD_times *= b[B].w[C]; }
- rown[0] = 0.00; rown[1] = 0.00; rown[2] = 0.00; rown[3] = 0.00;// tijdelijk = 0;
- double row_0, row_1, row_2, row_3;
- for (G = 0; G < nr_neurons_G; G++) {
- zDD_wupd = fu_input[G] * learn * xerr[G];
- gDD = xDD * g[G].w[0];
- tussen = fd(g[G].o[0]) * zDD_wupd;
- g[G].w[0] -= tussen;
- gw += tussen;
- avgtot += tussen;
- rown[G] += tussen;
- for (F = 0; F < nr_neurons_F; F++) {
- gDD_wupd = eu_input[F] * gDD /xerrtotal * xerr[G];
- tussen = fd(f[F].o[G]) * gDD_wupd/4;
- f[F].w[G] -= tussen;
- //f[F].w[G] -= new_avgtot;
- //f[F].w[G] -= new_fw;
- fw += tussen;
- avgtot += tussen;
- rown[F] += tussen;
- }
- }
- for (F = 0; F < nr_neurons_F; F++) {
- for (E = 0; E < nr_neurons_E; E++) {
- fDD_wupd = du_input[E] * fDD_times;
- tussen = fd(e[E].o[F]) * fDD_wupd/4;
- e[E].w[F] -= tussen;
- //e[E].w[F] -= new_avgtot;
- //e[E].w[F] -= new_ew;
- ew += tussen;
- avgtot += tussen;
- rown[E] += tussen;
- }
- }
- for (E = 0; E < nr_neurons_E; E++) {
- for (D = 0; D < nr_neurons_D; D++) {
- eDD_wupd = cu_input[D] * eDD_times;
- tussen = fd(d[D].o[E]) * eDD_wupd/4;
- d[D].w[E] -= tussen;
- //d[D].w[E] -= new_avgtot;
- //d[D].w[E] -= new_dw;
- dw += tussen;
- avgtot += tussen;
- rown[D] += tussen;
- }
- }
- for (D = 0; D < nr_neurons_D; D++) {
- for (C = 0; C < nr_neurons_C; C++) {
- dDD_wupd = bu_input[C] * dDD_times;
- tussen = fd(c[C].o[D]) * dDD_wupd/4;
- c[C].w[D] -= tussen;
- //c[C].w[D] -= new_avgtot;
- //c[C].w[D] -= new_cw;
- cw += tussen;
- avgtot += tussen;
- rown[C] += tussen;
- }
- }
- for (C = 0; C < nr_neurons_C; C++) {
- for (B = 0; B < nr_neurons_B; B++) {
- cDD_wupd = au_input[B] * cDD_times;
- tussen = fd(b[B].o[C]) * cDD_wupd/4;
- b[B].w[C] -= tussen;
- //b[B].w[C] -= new_avgtot;
- //b[B].w[C] -= new_bw;
- bw += tussen;
- avgtot += tussen;
- rown[B] += tussen;
- }
- }
- for (B = 0; B < nr_neurons_B; B++) {
- for (A = 0; A < nr_neurons_A; A++) {
- tussen = train_input[shuffled][A] * fd(a[A].o[B]) * bDD_times/4;
- a[A].w[B] -= tussen;
- //a[A].w[B] -= new_avgtot;
- //a[A].w[B] -= new_aw;
- aw += tussen;
- avgtot += tussen;
- rown[A] += tussen;
- }
- }
- //printf("\ntijdelijk %d",tijdelijk);
- avgtot = avgtot / 96;
- aw = aw / (nr_neurons_A*4);
- bw = bw / (nr_neurons_B*4);
- cw = cw / (nr_neurons_C*4);
- dw = dw / (nr_neurons_D*4);
- ew = ew / (nr_neurons_E*4);
- fw = fw / (nr_neurons_F*4);
- gw = gw / (nr_neurons_G);
- rown[0] = rown[0] / 24;
- rown[1] = rown[1] / 24;
- rown[2] = rown[2] / 24;
- rown[3] = rown[3] / 24;
- }
- }
- if (!(temp % (showtime/100))) printf("\n %d %%",++Z);
- sqcalc += sqerrtot;
- if (temp % 1000 == 0) {
- gemiddelde = sqcalc / 1000;
- sqcalc = 0;
- }
- sqerrtot = 0;
- }
- printf("\nEnd.");
- scanf("%d", &Z);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment