Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm> // std::copy
- #include <math.h>
- #include <cmath>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- //Control Variables
- bool flag = false;
- float beta = 1.00;
- float c = 1.0;
- float epsilon = 0.000001;
- //Vectors
- float z[4] = {0.0, 1.0, 1.0, 0.0};
- float s[3] = {0.0, 1.0, 2.0};
- float u[4][3] = {};
- float w[2][3] = {};
- float x1[4] = {};
- float x2[4] = {};
- float x3[4] = {1.0, 1.0, 1.0, 1.0};
- float x[3][3] = {};
- float y[4] = {};
- float si[3] = {};
- float s_new[3] = {};
- float wij[2][3] = {};
- float w_new[2][3] = {};
- float F(float u){ //FUNKCJA
- return 1.0 / (1.0 + exp((-1.0) * beta * u));
- }
- float derivativeF(float u){ //POCHODNA FUNKCJI
- return beta * (1 - F(u));
- }
- void calculateX1(float w[][3], float u[][3]){
- for (int p = 0; p < 4; p++)
- {
- x1[p] = F(w[0][0] * u[p][0] + w[0][1] * u[p][1] + w[0][2] * u[p][2]);
- }
- }
- void calculateX2(float w[][3], float u[][3]){
- for (int p = 0; p < 4; p++)
- {
- x2[p] = F(w[1][0] * u[p][0] + w[1][1] * u[p][1] + w[1][2] * u[p][2]);
- }
- }
- void X(float x1[], float x2[], float x3[]){
- x[0][0] = x1[0];
- x[0][1] = x1[1];
- x[0][2] = x1[2];
- x[1][0] = x2[0];
- x[1][1] = x2[1];
- x[1][2] = x2[2];
- x[2][0] = x3[0];
- x[2][1] = x3[1];
- x[2][2] = x3[2];
- }
- void calculateY(float s[], float x[][3]){
- for (int p = 0; p < 4; p++)
- {
- y[p] = F(s[0] * x[0][p] + s[1] * x[1][p] + s[2] * x[2][p]);
- }
- }
- void calculateSi(float y[], float z[], float s[], float x[][3]){
- for (int i = 0; i < 3; i++)
- {
- for (int p = 0; p < 4; p++)
- {
- si[i] += (y[p] - z[p]) * derivativeF(s[0] * x[0][p] + s[1] * x[1][p] + s[2] * x[2][p]) * x[i][p];
- }
- }
- }
- void calculateWij(float y[], float z[], float s[], float w[][3], float u[][3], float x[][3]){
- float u1[4] = {};
- float u2[4] = {};
- for(int i = 0; i < 2; i++){ //zeruj
- for(int j = 0; j < 3; j++){
- wij[i][j] = 0.0;
- }
- }
- for (int p = 0; p < 4; p++)
- {
- u1[p] = s[0] * x[0][p] + s[1] * x[1][p] + s[2] * x[2][p];
- }
- for (int i = 0; i < 2; i++)
- {
- for (int p = 0; p < 4; p++)
- {
- u2[p] += w[i][0] * u[p][0] + w[i][1] * u[p][1] + w[i][2] * u[p][2];
- }
- }
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- for (int p = 0; p < 4; p++)
- {
- wij[i][j] += (y[p] - z[p]) * derivativeF(u1[p]) * s[i] * derivativeF(u2[p]) * u[p][j];
- }
- }
- }
- }
- bool maximum(float w_new[][3], float w[][3], float s_new[], float s[]){
- float max1 = 0.0;
- float max2 = 0.0;
- for (int i = 0; i < 3; i++)
- {
- if (abs(s_new[i] - s[i]) >= max1)
- {
- max1 = abs(s_new[i] - s[i]);
- }
- }
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- if (abs(w_new[i][j] - w[i][j]) >= max2)
- {
- max2 = abs(w_new[i][j] - w[i][j]);
- }
- }
- }
- if ((max1 < epsilon) || (max2 < epsilon))
- return true;
- else
- return false;
- }
- void trainNet(){
- //Metoda gradientu
- int counter = 0;
- while(flag != true){
- calculateX1(w,u);
- calculateX2(w,u);
- X(x1,x2,x3);
- calculateY(s,x);
- calculateSi(y, z, s, x);
- calculateWij(y, z, s, w, u, x);
- s_new[0] = s[0] - c * si[0];
- s_new[1] = s[1] - c * si[1];
- s_new[2] = s[2] - c * si[2];
- w_new[0][0] = w[0][0] - c * wij[0][0];
- w_new[0][1] = w[0][1] - c * wij[0][1];
- w_new[0][2] = w[0][2] - c * wij[0][2];
- w_new[1][0] = w[1][0] - c * wij[1][0];
- w_new[1][1] = w[1][1] - c * wij[1][1];
- w_new[1][2] = w[1][2] - c * wij[1][2];
- flag = maximum(w_new, w, s_new, s); //Did we find it
- for(int i = 0; i < 3; i++){
- s[i] = s_new[i];
- }
- for(int i = 0; i < 2; i++){
- for(int j = 0; j < 3; j++){
- w[i][j] = w_new[i][j];
- }
- }
- counter++;
- } //FINISHED WHILE
- cout << "Zakonczono. Liczba wykonanych iteracji to: " << counter << endl;
- cout << "Wagi:" << endl;
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- cout << "w" << (i + 1) << (j + 1) << " = " << w_new[i][j] << " ";
- }
- cout << endl;
- }
- for (int i = 0; i < 3; i++)
- {
- cout << "s" << (i + 1) << " = " << s_new[i] << " ";
- }
- cout << endl << endl;
- cout << " XOR:" << endl;
- cout << " 0 0 | "; printf("%f\n", y[0]);
- cout << " 1 0 | "; printf("%f\n", y[1]);
- cout << " 0 1 | "; printf("%f\n", y[2]);
- cout << " 1 1 | "; printf("%f\n", y[3]);
- }
- int main()
- {
- //U FILL
- u[0][0] = 0; u[0][1] = 0; u[0][2] = 1;
- u[1][0] = 1; u[1][1] = 0; u[1][2] = 1;
- u[2][0] = 0; u[2][1] = 1; u[2][2] = 1;
- u[3][0] = 1; u[3][1] = 1; u[3][2] = 1;
- //STARTING WEIGHTS
- for (int i = 0; i < 2; i++)
- {
- w[i][0] = 0;
- w[i][1] = 1;
- w[i][2] = 2;
- }
- trainNet();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment