Advertisement
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 = 56;
- const int nr_neurons_B = 64;
- const int nr_neurons_C = 64;
- const int nr_neurons_D = 64;
- const int nr_neurons_E = 64;
- const int nr_neurons_F = 64;
- const int nr_neurons_G = 56;
- int tota = 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 = 266000;
- int showtime = 0.50*loops;
- int zrows = 15;
- 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.00000000000000000005;
- double xerr[nr_neurons_G], xerrtotal;
- double sqerrtot = 0;
- bool training_mode = false;
- double train_input[100][nr_neurons_A];
- double train_output[100][nr_neurons_G];
- double requested1[nr_neurons_A];
- 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 ((rand()%10000)/10000.00);
- }
- 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());
- learn = learn / tota;
- 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); {
- shuffled = rows;
- if (shuffled == zrows) training_mode = false;
- else training_mode = true;;
- TOTALbo = 0; TOTALao = 0;
- for (C = 0; C < nr_neurons_C; C++) {
- for (B = 0; B < nr_neurons_B; B++) {
- TOTAL = 0;
- for (A = 0; A < nr_neurons_A; A++) {
- if (C == 0) {
- TOTALao += (a[A].o[B] = fx(choose(shuffled, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ ));
- }
- TOTAL += a[A].o[B] * b[B].w[C];
- }
- TOTALbo += (b[B].o[C] = fx(TOTAL /*+ b[B].bw[C] * b[B].b[C]*/ ));
- }
- }
- TOTALdo = 0;
- TOTALco = 0;
- for (E = 0; E < nr_neurons_E; E++) {
- for (D = 0; D < nr_neurons_D; D++) {
- TOTAL = 0;
- for (C = 0; C < nr_neurons_C; C++) {
- if (E == 0) {
- double TOTALcCoD = 0;
- for (B = 0; B < nr_neurons_B; B++) {
- TOTALcCoD += b[B].o[C] * c[C].w[D];
- }
- TOTALco += (c[C].o[D] = fx(TOTALcCoD /*+ c[C].bw[D] * c[C].b[D]*/ ));
- }
- TOTAL += c[C].o[D] * d[D].w[E];
- }
- TOTALdo += (d[D].o[E] = fx(TOTAL /*+ d[D].bw[E] * d[D].b[E]*/ ));
- }
- }
- TOTALeo = 0;
- for (F = 0; F < nr_neurons_F; F++) {
- for (E = 0; E < nr_neurons_E; E++) {
- TOTAL = 0;
- for (D = 0; D < nr_neurons_D; D++) {
- TOTAL += d[D].o[E] * e[E].w[F];
- }
- TOTALeo += (e[E].o[F] = fx(TOTAL /*+ e[E].bw[F] * e[E].b[F]*/ ));
- }
- }
- TOTALfo = 0;
- for (G = 0; G < nr_neurons_G; G++) {
- for (F = 0; F < nr_neurons_F; F++) {
- TOTAL = 0;
- for (E = 0; E < nr_neurons_E; E++) {
- TOTAL += e[E].o[F] * f[F].w[G];
- }
- TOTALfo += (f[F].o[G] = fx(TOTAL /*+ f[F].bw[G] * f[F].b[G]*/ ));
- }
- }
- for (G = 0; G < nr_neurons_G; G++) {
- TOTAL = 0;
- for (F = 0; F < nr_neurons_F; F++) {
- TOTAL += f[F].o[G] * g[G].w[0];
- }
- g[G].o[0] = fx(TOTAL /*+ g[G].bw[0] * g[G].b[0]*/ );
- }
- if (temp > showtime) {
- if (training_mode) {
- printf("\n-");
- for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
- 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]);
- }
- } 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;
- for (G = 0; G < nr_neurons_G; G++) {
- zDD_wupd = TOTALfo * learn * xerr[G];
- gDD = xDD * g[G].w[0];
- gDD_wupd = TOTALeo * gDD;
- g[G].w[0] -= fd(g[G].o[0]) * zDD_wupd * nr_neurons_G;
- for (F = 0; F < nr_neurons_F; F++) {
- fDD = f[F].w[G] * gDD;
- fDD_wupd = TOTALdo * fDD;
- f[F].w[G] -= fd(f[F].o[G]) * gDD_wupd * nr_neurons_F / xerrtotal * xerr[G];
- }
- }
- for (F = 0; F < nr_neurons_F; F++) {
- for (E = 0; E < nr_neurons_E; E++) {
- eDD = e[E].w[F] * fDD;
- eDD_wupd = TOTALco * eDD;
- e[E].w[F] -= fd(e[E].o[F]) * fDD_wupd * nr_neurons_E;
- }
- }
- for (E = 0; E < nr_neurons_E; E++) {
- for (D = 0; D < nr_neurons_D; D++) {
- dDD = d[D].w[E] * eDD;
- dDD_wupd = TOTALbo * dDD;
- d[D].w[E] -= fd(d[D].o[E]) * eDD_wupd * nr_neurons_D;
- }
- }
- for (D = 0; D < nr_neurons_D; D++) {
- for (C = 0; C < nr_neurons_C; C++) {
- cDD = c[C].w[D] * dDD;
- cDD_wupd = cDD * TOTALao;
- c[C].w[D] -= fd(c[C].o[D]) * dDD_wupd * nr_neurons_C;
- }
- }
- for (C = 0; C < nr_neurons_C; C++) {
- for (B = 0; B < nr_neurons_B; B++) {
- bDD = b[B].w[C] * cDD;
- b[B].w[C] -= fd(b[B].o[C]) * cDD_wupd * nr_neurons_B;
- }
- }
- for (B = 0; B < nr_neurons_B; B++) {
- for (A = 0; A < nr_neurons_A; A++) {
- a[A].w[B] -= train_input[shuffled][A] * fd(a[A].o[B]) * bDD * nr_neurons_A;
- }
- }
- }
- }
- 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
Advertisement