Advertisement
Guest User

Sztuczna-sieci2

a guest
May 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #jednowarstwowa
  2. clear off
  3. nntwarn off
  4. format compact
  5.  
  6. P=-1:.1:-.5;
  7. T=[-.9602, -.5770, -.0729, .3771, .6405, .6600];
  8. [R,Q]=size(P);
  9. [S1,Q]=size(T);
  10. [W1,B1]=nwtan(S1,R);
  11. disp_freq=10;
  12. max_epoch=20000;
  13. err_goal=1e-30;
  14. lr=0.1;
  15. error=[];
  16. A1=tansig(W1*P,B1);
  17. E=T-A1;
  18. SSE=sumsqr(E);
  19.  
  20. for epoch=1:max_epoch
  21. if SSE<err_goal
  22. epoch=epoch-1;
  23. break
  24. end
  25. D1=deltatan(A1,E);
  26. [dW1,dB1]=learnbp(P,D1,lr);
  27. W1=W1+dW1; B1=B1+dB1;
  28.  
  29. A1=tansig(W1*P,B1);
  30. E=T-A1;
  31. SSE=sumsqr(E);
  32. error=[error SSE];
  33.  
  34.  
  35. if(rem(epoch,disp_freq)==0)
  36. epoch
  37. SSE
  38. plot(P,T,'r',P,A1,'g')
  39. pause(1e-50)
  40. end
  41. end
  42.  
  43. #dwuwarstwowa
  44. clear off
  45. nntwarn off
  46. format compact
  47.  
  48. P=-1:.1:-.5;
  49. T=[-.9602, -.5770, -.0729, .3771, .6405, .6600];
  50. [R,Q]=size(P);
  51. S1=3;
  52. [S2,Q]=size(T);
  53. [W1,B1]=nwtan(S1,R);
  54. [W2,B2]=rands(S2,S1);
  55. disp_freq=10;
  56. max_epoch=20000;
  57. err_goal=1e-30;
  58. lr=0.1;
  59. error=[];
  60. A1=tansig(W1*P,B1);
  61. A2=purelin(W2*A1,B2);
  62. E=T-A2;
  63. SSE=sumsqr(E);
  64.  
  65. for epoch=1:max_epoch
  66. if SSE<err_goal
  67. epoch=epoch-1;
  68. break
  69. end
  70. D2=deltalin(A2,E);
  71. D1=deltatan(A1,D2,W2);
  72.  
  73. [dW1,dB1]=learnbp(P,D1,lr);
  74. [dW2,dB2]=learnbp(A1,D2,lr);
  75. W1=W1+dW1; B1=B1+dB1;
  76. W2=W2+dW2; B2=B2+dB2;
  77.  
  78. A1=tansig(W1*P,B1);
  79. A2=purelin(W2*A1,B2);
  80. E=T-A2;
  81. SSE=sumsqr(E);
  82. error=[error SSE];
  83.  
  84.  
  85. if(rem(epoch,disp_freq)==0)
  86. epoch
  87. SSE
  88. plot(P,T,'r',P,A2,'g')
  89. pause(1e-50)
  90. end
  91. end
  92.  
  93. #trojwarstwowa
  94. clear off
  95. nntwarn off
  96. format compact
  97.  
  98. P=-1:.1:-.5;
  99. T=[-.9602, -.5770, -.0729, .3771, .6405, .6600];
  100. [R,Q]=size(P);
  101. S1=3;
  102. S2=2;
  103. [S2,Q]=size(T);
  104. [S3,Q]=size(T);
  105. [W1,B1]=nwtan(S1,R);
  106. [W2,B2]=nwtan(S2,S1);
  107. [W3,B3]=rands(S3,S2);
  108. disp_freq=10;
  109. max_epoch=20000;
  110. err_goal=1e-30;
  111. lr=0.1;
  112. error=[];
  113. A1=tansig(W1*P,B1);
  114. A2=tansig(W2*A1,B2);
  115. A3=purelin(W3*A2,B3);
  116. E=T-A3;
  117. SSE=sumsqr(E);
  118.  
  119. for epoch=1:max_epoch
  120. if SSE<err_goal
  121. epoch=epoch-1;
  122. break
  123. end
  124. D3=deltalin(A2,E);
  125. D2=deltatan(A2,D3,W3);
  126. D1=deltatan(A1,D2,W2);
  127.  
  128. [dW1,dB1]=learnbp(P,D1,lr);
  129. [dW2,dB2]=learnbp(A1,D2,lr);
  130. [dW3,dB3]=learnbp(A2,D3,lr);
  131. W1=W1+dW1; B1=B1+dB1;
  132. W2=W2+dW2; B2=B2+dB2;
  133. W3=W3+dW3; B3=B3+dB3;
  134.  
  135. A1=tansig(W1*P,B1);
  136. A2=tansig(W2*A1,B2);
  137. A3=purelin(W3*A2,B3);
  138. E=T-A3;
  139. SSE=sumsqr(E);
  140. error=[error SSE];
  141.  
  142.  
  143. if(rem(epoch,disp_freq)==0)
  144. epoch
  145. SSE
  146. plot(P,T,'r',P,A3,'g')
  147. pause(1e-50)
  148. end
  149. end
  150.  
  151. #porownanie
  152. s_jednowarstwowa
  153. save wynik_1w
  154. s_dwuwarstwowa
  155. save wynik_2w
  156. s_trojwarstwowa
  157. save wynik_3w
  158. plot([1:length(error)],error,'g')
  159. hold on
  160. load wynik_2w
  161. plot([1:length(error)],error,'b')
  162. load wynik_1w
  163. plot([1:length(error)],error,'r')
  164. hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement