Advertisement
ayurchyk1998

NN_report2

Jun 25th, 2021
1,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.60 KB | None | 0 0
  1. clc;
  2. clear;
  3. xdel(winsid());
  4.  
  5. m=100;
  6. n=20;
  7. p=3;
  8. L=m-p;
  9.  
  10. xfst = 0;
  11.  
  12. for i=1:m+n
  13.     x(i)=xfst + 0.1 * i;
  14.     y(i)=0.8*sin(4*x(i))+0.7;
  15.     printf('%f \n', y(i));
  16. end;
  17. //рандомная подборка веса для входящих значений
  18. for i=1:p
  19.     for j=1:p
  20.         w(i, j)=0.2*rand()-0.1;
  21.         printf('w(%d%d) = %f \n', i, j, w(i, j));
  22.     end;
  23.     v(i)=0.2*rand()-0.1;
  24.     printf('v(%d) = %f \n', i, v(i));
  25. end;
  26. //пороги входящих значений
  27. for i=1:p
  28.     T(i) = 0;
  29.     printf('T(%d) = %f \n', i, T(i));
  30. end
  31.  
  32. T_2=0;
  33. epochs = 1;
  34. Em = 1e-4;
  35. done = %F;
  36. alpha = 0.11;
  37.  
  38. //тренировка, пока не закончим перебирать все точки либо среднеквадр ошибка будет меньше Em (нашей выбранной)
  39. while done==%F do
  40.     E = 0;
  41.     Es(epochs) = 0;
  42.     for i=1:L
  43.         for j=1:p
  44.             S(j)=0;
  45.             for k=1:p
  46.                 S(j)=S(j) + y(i+k-1) * w(k, j)
  47.             end;
  48.             S(j)=S(j)-T(j);
  49.             temp_y(j)=1/(1+%e^(-S(j)));
  50.         end;
  51.         output=0;
  52.         for j=1:p
  53.             output=output+v(j)*temp_y(j);
  54.         end;
  55.         output=output-T_2;
  56.         for j=1:p
  57.             v(j)=v(j) - alpha*(output-y(i+p))*temp_y(j);
  58.         end;
  59.         T_2=T_2+alpha*(output-y(i+p));
  60.         for j=1:p
  61.             for k=1:p
  62.                 w(j,k)=w(j,k)-alpha*(output-y(i+p))*v(k)*temp_y(k)*(1-temp_y(k))* y(j+i-1);
  63.             end;
  64.             T(j)=T(j)+alpha*(output-y(i+p))*v(j)*temp_y(j)*(1-temp_y(j));
  65.         end;
  66.         E = E + (output - y(i+p))^2;
  67.     end;
  68.     Es(epochs) = E / 2;
  69.     printf('Es = %f\n', Es(epochs));
  70.     if Es(epochs)<=Em then
  71.         done = %T;
  72.     end;
  73.     if epochs > 1000 //для граф ошибок
  74.         done = %T;
  75.     end;
  76.     epochs = epochs+1;
  77. end;
  78.  
  79. //обучение нейронной сети
  80. for i=L:L+n
  81.    for j=1:p
  82.        S(j)=0;
  83.        for k=1:p
  84.            S(j)=S(j) + y(i+k-1) * w(k, j)
  85.        end;
  86.        S(j)=S(j)-T(j);
  87.        temp_y(j)=1/(1+%e^(-S(j)));
  88.     end;
  89.     output(i+p)=0;
  90.     for j=1:p
  91.         output(i+p)=output(i+p)+v(j)*temp_y(j);
  92.     end;
  93.     output(i+p)=output(i+p)-T_2;
  94.  
  95.     printf('%f\n', y(i+p)); //theoretical values
  96.     printf('%f\n', output(i+p)); //testing values
  97. end;
  98.  
  99. //график ошибки, тренировки и обучения сети
  100. plot(1:length(Es), Es, 'g*-');
  101. xlabel('number of epoch', 'fontsize', 3);
  102. ylabel('Es', 'fontsize', 3);
  103. //halt;
  104. figure();
  105. plot(x(L:L+n), y(L+p:L+p+n), 'b*-');
  106. plot(x(L:L+n), output(L+p:L+p+n), 'r-+');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement