Advertisement
Coderx7

XOR using backpropagation in matlab (II)

Sep 7th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.08 KB | None | 0 0
  1. %XOR using backpropagation (no bias version)
  2. % training set
  3. ts=[0 0;
  4.     1 1;
  5.     1 0;
  6.     0 1];
  7. d=[0 0 1 1];
  8. % weights for hidden layers (each row represents wiegths for the corrosponding neuron
  9. wh=[rand() rand();
  10.     rand() rand()];
  11. % wiegths from hidden layer to output layer
  12. wo=[rand() rand()];
  13. % output for neuron 1 and 2 and bias for neuron3
  14. a=[1 1];
  15. % output for neuron 3
  16. a3=0;  
  17. % learning rate
  18. n=0.1;  
  19. iteration=10;
  20. i=0;
  21. while (i<iteration || e~=0)
  22.     for tindex=1:4
  23.           for neuron=1:2
  24.               a(neuron)= logsig(wh(neuron,:)*ts(neuron,:)');
  25.           end
  26.           a3 = hardlim(wo(1)*a(1)+wo(2)*a(2))
  27.           e = d(tindex) - a3
  28.           grad_out = a3*e;
  29.           deltaW = n*grad_out*a;
  30.           wo_new = wo+deltaW;
  31.           grad_h = a(1)*(1-a(1)) * grad_out*wo(1)'
  32.           deltaW = n*grad_h*wh(1,:);
  33.           wh(1,:)= wh(1,:) +deltaW
  34.           grad_h = a(2)*(1-a(2)) * grad_out*wo(2)'
  35.          
  36.           wo = wo_new;
  37.           deltaW = n*grad_h*wh(2,:)
  38.           wh(2,:) = wh(2,:) +deltaW
  39.            
  40.       end
  41.       i=i+1;
  42.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement