Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. function [Wout Bout] = lRule(D, P)
  2. % LRULE - Perceptron Learning Rule.
  3. % [Wout Bout] = lRule(D, P) if the algorithm converges, it should
  4. % return the Wout - weight matrix and Bout - bias values that enable the
  5. % perceptron to classify the pattern P correctly. D is a desired
  6. % classification.
  7. %
  8. % Inputs:
  9. % D - List of desired classifications. Ith entry is either -1 or 1.
  10. % P - Patterns we want to classify, in our example these are all pats.
  11. %
  12. % Outputs:
  13. % Wout - The corrected weight.
  14. % Bout - The corrected bias.
  15.  
  16. error(nargchk(2,2,nargin));
  17.  
  18. patnum = size(P, 2);
  19. inputs = size(P, 1);
  20.  
  21. % Initialize Classification vector, weight and bias.
  22. C = NaN(1, patnum);
  23. Wout = zeros(1, inputs);
  24. Bout = 0;
  25.  
  26. disp('Our desired classification:')
  27. disp(D)
  28.  
  29. k = 0;
  30. while(~isequal(C, D))
  31. r = randi(patnum, 1);
  32. pat = P(:, r); % Randomly select a pattern.
  33.  
  34. C(r) = sign(Wout * pat - Bout); % Classify it.
  35.  
  36. if (C(r) == 0) % Avoiding zeros.
  37. C(r) = 1;
  38. end
  39.  
  40. if (C(r) ~= D(r)) % If not correctly classified:
  41. Wout = Wout + D(r) * pat'; % Modify the weight.
  42. Bout = Bout - D(r); % Modify the bias.
  43. end
  44. k = k + 1;
  45. end
  46. disp(C)
  47. fprintf('Converged in %i iterations\n', k);
  48.  
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement