Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ann.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- # include <cmath>
- #include <math.h>
- #include <vector>
- #include <iostream>
- struct Data {
- std::vector<int> input;
- std::vector<int> output;
- } data1;
- struct Topology
- {
- int L;
- std::vector<int> l;
- } topolygy;
- double f(double x);
- double f_deriv(double x);
- double gL(double a, double z, double t);
- double gl(int layer_id, int w_i, int w_j, double *a_arr, double *z_arr, double *t_arr, double *w_arr, int *s,int *sw, int L, int *l);
- double w_gradient(int layer_id, int w_i, int w_j, double *a_arr, double *z_arr, double *t_arr, double *w_arr,int *s, int *sw, int L, int *l);
- double delta_w(double grad, double dw);
- int randint();
- const double ETA = 0.1;
- const double ALPHA = 0.5;
- class AnnBase {
- public:
- virtual void prepare(Topology top) = 0;
- virtual void init(Topology top, double w_arr_1[]) = 0;
- virtual void train(Data data) = 0;
- virtual void feedForward(Data data, double a, double b) = 0;
- };
- class AnnSerialDBL : public AnnBase {
- public:
- void prepare(Topology top);
- void init(Topology top, double w_arr_1[]);
- void train(Data data);
- void feedForward(Data data,double a, double b);
- public:
- int sum1;
- int L;
- int * l;
- int * s;
- double * a_arr;
- double * z_arr;
- int * W;
- int * sw;
- double * w_arr;
- double * dw_arr;
- double * t_arr;
- };
- using namespace std;
- int main()
- {
- topolygy.L = 4;
- topolygy.l.push_back(2);
- topolygy.l.push_back(10);
- topolygy.l.push_back(5);
- topolygy.l.push_back(2);
- topolygy.l.push_back(2);
- AnnSerialDBL labas;
- labas.prepare(topolygy);
- labas.init(topolygy, NULL);
- for (int i = 0; i < topolygy.L; i++) {
- // cout << ": " << labas.W[i];
- }
- data1.input.push_back(1);
- data1.output.push_back(2);
- data1.input.push_back(1);
- data1.output.push_back(2);
- for (int i = 0; i < 100; i++) {
- int a = randint();
- int b = randint();
- data1.input[0] = a;
- data1.input[1] = b;
- if (a == b) {
- data1.output[0] = 1;
- data1.output[1] = 0;
- }
- else {
- data1.output[0] = 0;
- data1.output[1] = 1;
- }
- labas.train(data1);
- }
- int sum = 0;
- int mult = 0;
- for (int i = 0; i < labas.L; i++) {
- sum += topolygy.l.at(i) + 1;
- }
- for (int i = 0; i < sum; i++) {
- // cout << labas.a_arr[i] << endl;
- }
- for (int i = 0; i < 5;i++){
- data1.input[0] = randint();;
- data1.input[1] = randint();;
- double t=1, y=0;
- //cout << endl;
- labas.feedForward(data1,t,y);
- cout << endl << "1: " << data1.input[0] << " 2: " << data1.input[1] << endl;
- cout << endl << "1: " << data1.output[0] << " 2: " << data1.output[1] << endl<<endl;
- }
- int a;
- cin >> a;
- return 0;
- }
- int randint() {
- double r = ((double)rand() / (RAND_MAX));
- int a = 0;
- if (r > 0.5) {
- a = 1;
- }
- else
- {
- a = 0;
- }
- return a;
- }
- void AnnSerialDBL::prepare(Topology top)
- {
- l = new int[top.L];
- s = new int[top.L];
- int sum = 0;
- int mult = 0;
- for (int i = 0; i < top.L; i++) {
- sum += top.l.at(i) + 1;
- }
- sum1 = sum;
- for (int i = 0; i < top.L - 1; i++) {
- mult += (top.l.at(i) + 1)*top.l.at(i+1);
- }
- a_arr = new double[sum];
- z_arr = new double[sum];
- W = new int[top.L];
- sw = new int[top.L];
- w_arr = new double[mult];
- dw_arr = new double[mult];
- t_arr = new double[top.l.at(top.L - 1)];
- }
- void AnnSerialDBL::init(Topology top, double w_arr_1[] = NULL)
- {
- L = top.L;
- //Neuronu kiekiai sluoksnyje
- for (int i = 0; i < top.L; i++) {
- l[i] = top.l.at(i) + 1;
- }
- //Sluoksniu pradzios indeksai
- for (int i = 0; i < top.L; i++) {
- s[i] = 0;
- for (int j = i; j > 0; j--) {
- s[i] += l[j - 1];
- }
- }
- //Bias neuronai
- for (int i = 0; i < top.L - 1; i++) {
- a_arr[s[i + 1] - 1] = 1;
- }
- //Svoriu kiekiai l-ame sluoksnyje
- for (int i = 0; i < top.L - 1; i++) {
- W[i] = l[i] * (l[i + 1] - 1);
- //cout << "Svoriu sk: " << W[i] << " Pradzios index: ";
- sw[i] = 0;
- if (i != 0) {
- for (int j = 0; j < i; j++) {
- sw[i] += W[j];
- }
- }
- if (w_arr_1 == NULL) {
- for (int j = 0; j < W[i]; j++) {
- w_arr[sw[i] + j] = (double)rand() / double(RAND_MAX);
- //cout << w_arr[sw[i] + j]<< endl;
- dw_arr[sw[i] + j] = 0;
- }
- }
- else {
- w_arr = w_arr_1; //ar reikia pokycius issisaugoti irgi?
- }
- //cout << sw[i] << " " << endl;
- }
- }
- void AnnSerialDBL::train(Data data)
- {
- a_arr[0] = (double)data.input[0];
- a_arr[1] = (double)data.input[1];
- for (int j = 0; j < sum1; j++) {
- z_arr[j] = 0;
- }
- //FFEEED FORWARD
- for (int i = 0; i < L - 1; i++) {//per sluoksnius einu+
- for (int j = 0; j < l[i]; j++) { //kiek neuronu sluoksnyje+
- for (int k = 0; k < l[i + 1] - 1; k++) {//per sekancio sluoksnio z+
- z_arr[s[i + 1] + k] += w_arr[sw[i] + k + j*l[i + 1]] * a_arr[s[i] + j];
- //cout << "w: "<< w_arr[sw[i] + k + j*l[i + 1]] << endl;
- //cout << "a: " << a_arr[s[i] + j] << endl;
- cout << "z reiksmes: " << z_arr[s[i+1] + k] << endl;
- cout << endl;
- }
- }
- for (int k = 0; k < l[i + 1] - 1; k++) {//per sekancio sluoksnio z
- a_arr[s[i + 1] + k] = f(z_arr[s[i + 1] + k]);
- cout << s[i + 1] + k << " a reiksmes: " << a_arr[s[i + 1] + k] << endl;
- }
- }
- t_arr[0] = (double)data.output[0];
- t_arr[1] = (double)data.output[1];
- //back propogation:
- for (int i = 0; i < L - 1; i++) {//per sluoksnius
- for (int j = 0; j < l[i]; j++) {//per neuronus
- for (int k = 0; k < l[i + 1] - 1; k++) {//per kito sluoksnio neuronus
- dw_arr[sw[i] + k] = delta_w(w_gradient(i, j, k, a_arr, z_arr, t_arr, w_arr, s,sw, L, l), dw_arr[sw[i] + k]);
- w_arr[sw[i] + k] += dw_arr[sw[i] + k];
- // cout << w_arr[sw[i] + k] << " " << endl;
- }
- }
- }
- }
- void AnnSerialDBL::feedForward(Data data, double a, double b)
- {
- a_arr[0] = (double)data.input[0];
- a_arr[1] = (double)data.input[1];
- for (int j = 0; j < sum1; j++) {
- z_arr[j] = 0;
- }
- //FFEEED FORWARD
- for (int i = 0; i < L - 1; i++) {//per sluoksnius einu+
- for (int j = 0; j < l[i]; j++) { //kiek neuronu sluoksnyje+
- for (int k = 0; k < l[i + 1] - 1; k++) {//per sekancio sluoksnio z+
- z_arr[s[i + 1] + k] += w_arr[sw[i] + k + j*l[i + 1]] * a_arr[s[i] + j];
- /* cout << "w: "<< w_arr[sw[i] + k + j*l[i + 1]] << endl;
- cout << "a: " << a_arr[s[i] + j] << endl;
- cout << "z reiksmes: " << z_arr[s[i+1] + k] << endl;
- cout << endl;*/
- }
- }
- for (int k = 0; k < l[i + 1] - 1; k++) {//per sekancio sluoksnio z
- a_arr[s[i + 1] + k] = f(z_arr[s[i + 1] + k]);
- // cout << s[i + 1] + k << " a reiksmes: " << a_arr[s[i + 1] + k] << endl;
- }
- }
- //cout << a_arr[s[L - 1]];
- //cout << a_arr[s[L - 1]+1];
- a = a_arr[s[L-1]];
- b = a_arr[s[L-1]+1];
- if (a < b) {
- data.output[0] = 1;
- data.output[0] = 0;
- }
- else {
- data.output[1] = 0;
- data.output[1] = 1;
- }
- //data.output[0] = a_arr[s[L - 1]];
- // data.output[1] = a_arr[s[L - 1] + 1];
- }
- double f(double x) {
- return 1 / (1 + exp(-x));
- }
- double f_deriv(double x) {
- return exp(-x) / pow((1 + exp(-x)), 2);
- }
- double gL(double a, double z, double t) {
- double w = f_deriv(z) * (a - t);
- //cout << "z: " << z << " a: " << a << " t: " << t << endl;
- return w;
- }
- double gl(int layer_id, int w_i, int w_j, double *a_arr, double *z_arr, double *t_arr, double *w_arr, int *s,int *sw, int L, int *l) {
- double w = f_deriv(z_arr[s[layer_id] + w_j]);
- double sum = 0;
- for (int i = 0; i < l[layer_id + 1] - 1; i++) {
- if (layer_id + 2 == L) {
- sum += w_arr[sw[layer_id] + i] * gL(a_arr[s[layer_id + 1] + i], z_arr[s[layer_id + 1]+ i], t_arr[i]);
- }
- else {
- sum += w_arr[sw[layer_id] + w_j] * gl(layer_id + 1, w_i, i, a_arr, z_arr, t_arr,w_arr, s, sw, L, l);
- }
- }
- return w*sum;
- }
- double w_gradient(int layer_id, int w_i, int w_j, double *a_arr, double *z_arr, double *t_arr, double *w_arr,int *s, int *sw, int L, int *l) {
- double w = a_arr[s[layer_id] + w_i];
- if (layer_id + 2 == L) {
- w *= gL(a_arr[s[layer_id + 1] + w_j], z_arr[s[layer_id + 1] + w_j], t_arr[w_j]);
- }
- else {
- w *= gl(layer_id + 1, w_i, w_j, a_arr, z_arr, t_arr, w_arr, s, sw, L, l);
- }
- //cout << L << endl;
- //cout << w << " layer id:"<< layer_id <<endl;
- return w;
- }
- double delta_w(double grad, double dw) {
- return -ETA*grad + ALPHA*dw;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement