Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc;
- clear;
- xdel(winsid());
- //data
- e = [0 1 1 0];
- x = [4 0.5 0 0.5; 4 0 0 0];
- // weights and threshold random
- w = [ rand() rand(); rand() rand() ];
- T = [ 0 0 ];
- v = [ rand() rand() ];
- T_2 = 0;
- alpha = 0.7; // learning step
- Em = 0; // mean square error
- h = 0.1; // steps (x)
- //training
- done = %F; // flag
- loops = 0;
- while done == %F do
- loops = loops + 1;
- Es(loops) = 0;
- for n = 1:4
- for i = 1:2
- Sum(i) = 0;
- for j = 1:2
- Sum(i) = Sum(i) + x(j, n)*w(j, i);
- end;
- Sum(i) = Sum(i) - T(i);
- y(i) = 1/(1+%e^(-Sum(i)));
- end;
- Sum2 = 0;
- for i = 1:2
- Sum2 = Sum2 + v(i) * y(i);
- end;
- Sum2 = Sum2 - T_2;
- z(n) = 1/(1+%e^(-Sum2)); //outputs
- for i = 1:2
- calc_v(loops, i) = v(i);
- v(i) = v(i) - alpha * (z(n) - e(n) * z(n) * (1 - z(n)) * y(i));
- for j = 1:2
- calc_w(loops, j, i) = w(j, i);
- w(j, i) = w(j, i) - alpha * (z(n) - e(n)) * z(n) * (1 - z(n)) * v(i) * y(i) * (1 - y(i)) * x(j, n);
- end;
- calc_T(loops, i) = T(i);
- T(i) = T(i) + alpha * (z(n) - e(n)) * z(n) * (1 - z(n)) * v(i) * y(i) * (1 - y(i));
- end;
- calc_T_2(loops) = T_2;
- T_2 = T_2 + alpha * (z(n) - e(n)) * z(n) * (1 - z(n));
- Es(loops) = Es(loops) + (z(n) - e(n))^2;
- printf('E = %f\n', Es(loops));
- end;
- Es(loops) = Es(loops)/2;
- if Es(loops) <= Em || loops > 1000
- done = %T;
- end;
- end;
- //learning
- index = find(Es==min(Es));
- printf('min(Es) = %f\n\n', Es(index));
- for i = 1:2
- for j = 1:2
- printf('w(%d, %d) = %f\n', j, i, calc_w(index, j, i));
- end;
- printf('\n');
- printf('T(%d) = %f\n', i, calc_T(index, i));
- printf('v(%d) = %f\n', i, calc_v(index, i));
- end;
- printf('\nT = %f\n\n', calc_T_2(index));
- //check your results
- l=0;
- while l==0 do
- printf('\Enter the coordinates of the point you want to check\n');
- pcheck = input("x = ");
- for i = 1:2
- Sum(i) = 0;
- for j = 1:2
- Sum(i) = Sum(i) + calc_w(index, j, i) * pcheck(j);
- end;
- Sum(i) = Sum(i) - calc_T(index, i);
- y(i) = 1/(1+%e^(-Sum(i)));
- end;
- Sum2 = 0;
- for i = 1:2
- Sum2 = Sum2 + calc_v(index, i) * y(i);
- end;
- Sum2= Sum2 - calc_T_2(index);
- result_0 = 1/(1+%e^(-Sum2));
- if result_0 > 0.5
- result = 1;
- else
- result = 0;
- end;
- printf('z = %f\n', result);
- printf('result = %f', result_0);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement