Machine Learning Meetup Notes Perceptron Matlab

From Noisebridge
Revision as of 22:35, 12 March 2009 by Jbm (talk | contribs) (New page: <pre> % Perceptron Implementation (by Jean) % indata = [1,0,0;1,0,1;1,1,0;1,1,1]; expected = [1,1,1,0]; % Learning Rate LR = 0.1; % Initial set of weights. weights = [0,0,0]; threshold = ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
% Perceptron Implementation (by Jean)
%
indata = [1,0,0;1,0,1;1,1,0;1,1,1];
expected = [1,1,1,0];
% Learning Rate
LR = 0.1;

% Initial set of weights.
weights = [0,0,0];
threshold = 0.5;

delta = 1;
error = 1;

% Outside loop to iterate weights. 
while (error ~= 0)
    error = 0;
    for i = 1:size(indata,1)
    	% bias term is the threshold. 
     	if (dot(weights,indata(i,:)) > threshold)
               % dot(weights,indata(i,:))
	     out = 1;  
     	else
	      out = 0;
     	end
           % direction of learning
     	direction =  expected(i) - out;
      	delta = direction * LR * indata(i,:);
      	weights = weights + delta;

            if (norm(delta) ~= 0)
                error = 1;
	end
     end	
end

% Display the final output weights.
weights