Advertisement
ayurchyk1998

Untitled

Jun 8th, 2021
2,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.25 KB | None | 0 0
  1. clc;
  2. clear;
  3. xdel(winsid());
  4.  
  5. //data
  6. e = [0 1 1 0];
  7. x = [4 0.5 0 0.5; 4 0 0 0];
  8. // weights and threshold random
  9. w = [ rand() rand(); rand() rand() ];
  10. T = [ 0 0 ];
  11. v = [ rand() rand() ];
  12. T_2 = 0;
  13.  
  14. alpha = 0.7; // learning step
  15. Em = 0; // mean square error
  16. h = 0.1;    // steps (x)
  17.  
  18. //training
  19. done = %F;  // flag
  20. loops = 0;
  21. while done == %F do
  22.     loops = loops + 1;
  23.     Es(loops) = 0;
  24.     for n = 1:4
  25.         for i = 1:2
  26.             Sum(i) = 0;
  27.             for j = 1:2
  28.                 Sum(i) = Sum(i) + x(j, n)*w(j, i);
  29.             end;
  30.             Sum(i) = Sum(i) - T(i);
  31.             y(i) = 1/(1+%e^(-Sum(i)));
  32.         end;
  33.         Sum2 = 0;
  34.         for i = 1:2
  35.             Sum2 = Sum2 + v(i) * y(i);
  36.         end;
  37.         Sum2 = Sum2 - T_2;
  38.         z(n) = 1/(1+%e^(-Sum2)); //outputs
  39.         for i = 1:2
  40.             calc_v(loops, i) = v(i);
  41.             v(i) = v(i) - alpha * (z(n) - e(n) * z(n) * (1 - z(n)) * y(i));
  42.  
  43.             for j = 1:2
  44.                 calc_w(loops, j, i) = w(j, i);
  45.                 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);
  46.             end;
  47.             calc_T(loops, i) = T(i);
  48.             T(i) = T(i) + alpha * (z(n) - e(n)) * z(n) * (1 - z(n)) * v(i) * y(i) * (1 - y(i));
  49.         end;
  50.         calc_T_2(loops) = T_2;
  51.         T_2 = T_2 + alpha * (z(n) - e(n)) * z(n) * (1 - z(n));
  52.         Es(loops) = Es(loops) + (z(n) - e(n))^2;
  53.         printf('E = %f\n', Es(loops));
  54.     end;
  55.     Es(loops) = Es(loops)/2;
  56.  
  57.     if Es(loops) <= Em || loops > 1000
  58.         done = %T;
  59.     end;
  60. end;
  61. //learning
  62. index = find(Es==min(Es));
  63. printf('min(Es) = %f\n\n', Es(index));
  64. for i = 1:2
  65.     for j = 1:2
  66.         printf('w(%d, %d) = %f\n', j, i, calc_w(index, j, i));
  67.     end;
  68.     printf('\n');
  69.     printf('T(%d) = %f\n', i, calc_T(index, i));
  70.     printf('v(%d) = %f\n', i, calc_v(index, i));
  71. end;
  72. printf('\nT = %f\n\n', calc_T_2(index));
  73.  
  74. //check your results
  75. l=0;
  76. while l==0 do
  77.     printf('\Enter the coordinates of the point you want to check\n');
  78.     pcheck = input("x = ");
  79.     for i = 1:2
  80.         Sum(i) = 0;
  81.         for j = 1:2
  82.             Sum(i) = Sum(i) + calc_w(index, j, i) * pcheck(j);
  83.         end;
  84.         Sum(i) = Sum(i) - calc_T(index, i);
  85.         y(i) = 1/(1+%e^(-Sum(i)));
  86.     end;
  87.     Sum2 = 0;
  88.     for i = 1:2
  89.         Sum2 = Sum2 + calc_v(index, i) * y(i);
  90.     end;
  91.     Sum2= Sum2 - calc_T_2(index);
  92.     result_0 = 1/(1+%e^(-Sum2));
  93.     if result_0 > 0.5
  94.         result = 1;
  95.     else
  96.         result = 0;
  97.     end;
  98.     printf('z = %f\n', result);
  99.     printf('result = %f', result_0);
  100. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement