Coderx7

XOR using backpropagation in matlab (I)

Sep 7th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.16 KB | None | 0 0
  1. % XOR calculator using BackPropagation
  2. clear all;clc;
  3. %specify input samples (creating trainings set)
  4. pattern = [1 1;
  5.            0 0;
  6.            1 0;
  7.            0 1];
  8. [patternRows,patternCols] = size(pattern);
  9. %creating targets or desired answers for each set
  10. target = [0,0,1,1];
  11. %initializing weights with random values
  12. w1 = [-1 1 ;    %neuron 1
  13.       -1 2];    %neuron two
  14. w2 = [-2,0.5];  %outputneuron
  15. %bias
  16. b12 = [1 -1]; % for neuron one and two respectively
  17. b3 = -1.5;    % for neuron three (output neuron)
  18. lr  = 0.01;     %learning rate
  19. a12 = [2 1];    %layer 1 outputs (neuron one and twos output respectively)
  20. a3  = 1;        %layer 2 output  (neuron 3 output)
  21. delta = 0;
  22. i=1;
  23. while (i<1000 || error ~=0)
  24.       for row_number = 1: patternRows
  25.           for neuron = 1:2
  26.               %calculate the output of each neuron
  27.               a12(neuron) = logsig( pattern(row_number,:) * w1(neuron,:)' + b12(neuron) );
  28.           end
  29.           %calculate the output
  30.           a3 = hardlim(w2(:)'*a12(:)+ b3 );
  31.           %calculate the error
  32.           error = target(row_number) - a3;
  33.           %calculate the local gradient (sensivity)
  34.           %which is f'(n) * e(n) for output layer
  35.           SM = a3*error;
  36.           %calculating delta which is learningrate * localgradient*inputs
  37.           delta = lr * SM * a12;
  38.           %updating the weights of last layer
  39.           w2_new = w2 + delta;
  40.           %updating the last layers bias          
  41.           b3_new = b3 + lr * SM;
  42.           %calculating local gradients(sensivities) for hiddent layers
  43.           %which is f(n)*SumOfNextLayers(localgradient*weights)
  44.           Sm = [a12(1)*(1-a12(1)) 0;
  45.                 0  a12(2)*(1-a12(2)) ] * w2' * SM ;
  46.           %calculating delta which is the same as before
  47.           delta = (lr .* Sm)* pattern(row_number,:);
  48.           %updating the weights and biases of hidden layer
  49.           w2 = w2_new;
  50.           b3 = b3_new;
  51.           w1 = w1 + delta;
  52.           b12 = b12 + (lr * Sm)';
  53.            sss = sprintf('%d ) a3= %d # e= %d # w1=%d,%d # w2 =%d,%d \n',i,a3 ,error,w1(1),w1(2),w2(1),w2(2));
  54.            fprintf(' %s ',sss);
  55.       end
  56.      i= i+1;
  57.   end
Advertisement
Add Comment
Please, Sign In to add comment