Machine Learning Meetup Notes Perceptron Matlab

From Noisebridge
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
% 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