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>
- //256 inputs, 256 outputs, faster: 5 blocks of 64 neurons
- const int nr_neurons_A = 256;
- 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 = 256;
- int Z;
- int A, B, C, D, E, F, G;
- int loops = 26600;
- 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.000000000000000000001; // this many neurons one needs to make a very tiny learning rate, or the results will be 0 or inf
- 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());
- //remove cursor blinking
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO CursoInfo;
- CursoInfo.dwSize = 1;
- CursoInfo.bVisible = false;
- SetConsoleCursorInfo(hConsole, &CursoInfo);
- 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.00 * train_input[i][j];
- }
- }
- 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++) {
- // printf("\nn %d",temp);
- //std::shuffle(v.begin(), v.end(), gh);
- //for (rows = 0; rows < zrows+1; rows++)
- rows = rand() % (zrows + 1); {
- shuffled = rows; // =v[rows];
- if (shuffled == zrows) training_mode = false;
- else training_mode = true;;
- // if (training_mode) input = train_input[shuffled]; else input = requested0;
- 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) { //bias handling
- TOTALao += (a[A].o[B] = fx(choose(shuffled, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ ));
- membDD_xupd[A][B] = train_input[shuffled][A] * fd(a[A].o[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]*/ ));
- memBWC_xupd[B][C] = fd(b[B].o[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]*/ ));
- memCWD_xupd[C][D] = fd(c[C].o[D]);
- }
- TOTAL += c[C].o[D] * d[D].w[E];
- memDWD_xupd[D][E] = fd(d[D].o[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]*/ ));
- memEWD_xupd[E][F] = fd(e[E].o[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]*/ ));
- memFWD_xupd[F][G] = fd(f[F].o[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]*/ );
- memGWD_xupd[G] = fd(g[G].o[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 * requested1[showoutputs]);
- }
- }
- }
- 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;
- //removed : loop -in loop -in loop etc: v*w*x*y*z vs v*w + w*x + x*y + y*z
- for (G = 0; G < nr_neurons_G; G++) {
- zDD_wupd = TOTALfo * learn * xerr[G];
- //zDD_bupd = gamma * learn * xerr[G];
- gDD = xDD * g[G].w[0];
- gDD_wupd = TOTALeo * gDD;
- g[G].w[0] -= memGWD_xupd[G] * zDD_wupd;
- //gDD_bupd = gamma * gDD;
- //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.
- for (F = 0; F < nr_neurons_F; F++) {
- fDD = f[F].w[G] * gDD;
- fDD_wupd = TOTALdo * fDD;
- f[F].w[G] -= memFWD_xupd[F][G] * gDD_wupd;
- //fDD_bupd = gamma * fDD;
- //f[F].bw[G] -= fd(f[F].o[G]) * gDD_bupd;
- }
- }
- 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] -= memEWD_xupd[E][F] * fDD_wupd;
- //eDD_bupd = gamma * eDD;
- //e[E].bw[F] -= fd(e[E].o[F]) * fDD_bupd ;
- }
- }
- 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] -= memDWD_xupd[D][E] * eDD_wupd;
- //dDD_bupd = gamma * dDD;
- //d[D].bw[E] -= fd(d[D].o[E]) * eDD_bupd ;
- }
- }
- 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] -= memCWD_xupd[C][D] * dDD_wupd;
- //cDD_bupd = cDD * gamma;
- //c[C].bw[D] -= fd(c[C].o[D]) * dDD_bupd ;
- }
- }
- 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] -= memBWC_xupd[B][C] * cDD_wupd;
- //bDD_wupd : can't use totals like TOTALbo for A, using xupd
- //b[B].bw[C] -= fd(b[B].o[C]) * cDD_bupd ;
- }
- }
- for (B = 0; B < nr_neurons_B; B++) {
- for (A = 0; A < nr_neurons_A; A++) {
- //bDD_xupd = membDD_xupd[A][B] * bDD;
- a[A].w[B] -= membDD_xupd[A][B] * bDD;
- //a[A].bw[B] -= bDD_xupd * gamma;
- }
- }
- }
- }
- 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;
- }
- //machine learning, a.i. tryout
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement