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>
- static unsigned long x=123456789, y=362436069, z=521288629;
- const double tempo = 1.00 / exp(1.00);
- const int nr_neurons_A = 64;
- const int nr_neurons_B = 2;
- int tota = nr_neurons_A + nr_neurons_B;
- int Z;
- int A, B, C, D, E, F, G;
- int loops = 240000;
- double learn = 0.1;
- int showtime = 0.97*loops;
- int zrows = 4;
- int temp, i1, i2, rows, shuffled;
- int i, j;
- int showoutputs;
- //determine either cross or a circle, kruisje rondje
- double train_input[4][64] = { { 1,0,0,0,0,0,0,1,
- 0,1,0,0,0,0,1,0,
- 0,0,1,0,0,1,0,0,
- 0,0,0,1,1,0,0,0,
- 0,0,0,1,1,0,0,0,
- 0,0,1,0,0,1,0,0,
- 0,1,0,0,0,0,1,0,
- 1,0,0,0,0,0,0,1},
- { 1,0,0,0,0,0,0,1,
- 0,1,0,0,0,0,1,0,
- 0,0,1,0,0,1,0,0,
- 0,0,0,1,0,0,0,0,
- 0,0,0,0,1,0,0,0,
- 0,0,1,0,0,1,0,0,
- 0,1,0,0,0,0,1,0,
- 1,0,0,0,0,0,0,1},
- { 0,0,1,1,1,1,0,0,
- 0,1,0,0,0,0,1,0,
- 1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,1,
- 0,1,0,0,0,0,1,0,
- 0,0,1,1,1,1,0,0},
- { 0,0,1,1,1,1,0,0,
- 0,1,0,0,0,0,1,0,
- 0,1,0,0,0,0,1,0,
- 0,1,0,0,0,0,1,0,
- 0,1,0,0,0,0,1,0,
- 0,1,0,0,0,0,1,0,
- 0,1,0,0,0,0,1,0,
- 0,0,1,1,1,1,0,0},
- };
- double train_output[4][2] = { {1,0}, //cross
- {1,0}, //cross
- {0,1}, //circle
- {0,1} //circle
- };
- double requested[64] = { 1,0,0,0,0,0,0,1,
- 0,1,0,0,0,0,1,0,
- 0,0,1,1,0,1,0,0,
- 0,0,0,1,0,0,0,0,
- 0,0,0,0,1,0,0,0,
- 0,0,1,0,0,1,0,0,
- 0,1,0,0,0,0,1,0,
- 1,0,0,0,0,0,0,1
- };
- //incase we are testing the "a.i." functionality
- double output_for_requested_should_be[1][2] = {
- {1,0}, //cross
- };
- 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 sqcalc, gemiddelde;
- double TOTAL, TOTALcCoD, TOTALao, TOTALbo, TOTALco, TOTALdo, TOTALeo, TOTALfo;
- double xerr[nr_neurons_B], xerrtotal;
- double sqerrtot = 0;
- double ato[5];
- double bto[5];
- double atoTOTAL[65];
- double btoTOTAL[3];
- bool training_mode = false;
- double gDD_times, fDD_times, eDD_times, dDD_times, cDD_times, bDD_times;
- double avgtot, aw, bw, cw, dw, ew, fw, gw;
- double tussen, row_0, row_1, row_2, row_3;
- 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 requested[aa];
- }
- unsigned long xorshf96(void) { //semi random
- unsigned long t;
- x ^= x << 16;
- x ^= x >> 5;
- x ^= x << 1;
- t = x;
- x = y;
- y = z;
- z = t ^ x ^ y;
- return z;
- }
- double initialize() {
- return ((xorshf96()%10000)/10000.00);
- }
- double fd(double x) {
- return (x >= 0) ? (1) : (tempo);
- }
- double fx(double x) {
- return (x >= 0) ? (x) : (x*tempo);
- }
- 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];
- 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++) {
- b[i1].w[0] = initialize();
- b[i1].bw[0] = initialize();
- b[i1].b[0] = initialize();
- }
- sqcalc = 99999999.99;
- for (temp = 0; temp < loops; temp++) {
- //for (rows = 0; rows < zrows+1; rows++)
- rows = xorshf96() % (zrows + 1);
- if (rows == zrows) { training_mode = false; }
- else { training_mode = true; }
- TOTALbo = 0; TOTALao = 0;
- // std::fill(atoTOTAL, atoTOTAL+65, 0);
- std::fill(btoTOTAL, btoTOTAL+3, 0);
- std::fill(ato, ato+65, 0);
- // std::fill(bto, bto+3, 0);
- for (B = 0; B < nr_neurons_B; B++) {
- TOTAL = 0;
- for (A = 0; A < nr_neurons_A; A++) {
- a[A].o[B] = fx(choose(rows, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ );
- ato[B] += a[A].o[B];
- // atoTOTAL[B] += a[A].o[B] ;
- btoTOTAL[0] += a[A].o[B] * b[B].w[0];
- }
- b[B].o[0] = fx(btoTOTAL[0] /*+ b[B].bw[C] * b[B].b[C]*/ );
- }
- xerrtotal = 0;
- if (training_mode==true) {
- for (showoutputs = 0; showoutputs < nr_neurons_B; showoutputs++) {
- xerr[showoutputs] = 2 * (b[showoutputs].o[0] - train_output[rows][showoutputs]);
- xerrtotal += (xerr[showoutputs]);
- }
- sqerrtot = xerrtotal;
- avgtot = 0;
- int tellertje = 0;
- xDD = learn * xerrtotal;
- gDD_times = 0;
- aw = 0; bw = 0; cw = 0; dw = 0; ew = 0; fw = 0; gw = 0;
- bDD_times = 0; cDD_times = 0;dDD_times = 0;eDD_times = 0;fDD_times = 0;gDD_times = 0;
- int Gi,Ga,Fi,Fa,Ea,Da;
- avgtot = 0; aw = 0; bw = 0; cw = 0; dw = 0; ew = 0; fw = 0; gw = 0;
- for (B = 0; B < nr_neurons_B; B++) {
- cDD_wupd = ato[B] * learn * xerr[B];
- tussen = fd(b[B].o[0]) * cDD_wupd;
- b[B].w[0] -= tussen;
- // bw += tussen;
- // avgtot += tussen; tellertje++;
- // if (B==0) row_0 += tussen;
- // if (B==1) row_1 += tussen;
- // if (B==2) row_2 += tussen;
- // if (B==3) row_3 += tussen;
- }
- for (B = 0; B < nr_neurons_B; B++) {bDD_times += b[B].w[0]* xerr[B];
- }
- bDD_times *= learn;
- tellertje = 0;
- for (B = 0; B < nr_neurons_B; B++) {
- for (A = 0; A < nr_neurons_A; A++) {
- tussen = train_input[rows][A] * fd(a[A].o[B]) * bDD_times;
- a[A].w[B] -= tussen;
- // aw += tussen;
- // avgtot += tussen; tellertje++;
- // if (A==0) row_0 += tussen;
- // if (A==1) row_1 += tussen;
- // if (A==2) row_2 += tussen;
- //if (A==3) row_3 += tussen;
- }
- }
- }
- if (temp > showtime) {
- if (training_mode) {
- printf("\n-");
- for (showoutputs = 0; showoutputs < nr_neurons_B; showoutputs++) {
- printf("\n input: %.2lf The current output_%d for training set[%d] is %.28lf of (%lf)", train_input[rows][showoutputs], showoutputs, rows, b[showoutputs].o[0], train_output[rows][showoutputs]);
- //if (rows==4) printf("\n(loop: % d) output_%d for requested data (%.28lf) is currently : %.8lf (should be:%lf)", temp, showoutputs, requested1[showoutputs], b[showoutputs].o[0], output_for_requested_should_be[0][showoutputs] );
- }
- } else {
- for (showoutputs = 0; showoutputs < nr_neurons_B; showoutputs++) {
- printf("\n output for requested data : %lf (should be:%lf)", b[showoutputs].o[0], output_for_requested_should_be[0][showoutputs] );
- }
- }
- }
- 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