Perceptron Neural Network using Matlab

P

Perceptron is one of the simplest forms of a neural network model. The following code snippet is a simple version of such a neural network using Matlab. Before using it please read some background information at wikipedia : https://en.wikipedia.org/wiki/Perceptron

clc; 
clear all; 
close all;

% Data
inputs=[1 0 0; 1 0 1;1 1 0;1 1 1];    
                      
% Desired Output
desiredOutput=[1 1 1 0];

% Learning Rate                                   
learningRate=0.1;   

% HardLimit threshold                                       
    threshold=0.5;   

% Initial Weights                           
weights=[0 0 0];
                                            
for j=1:200
   disp(sprintf('----- Round %d -----',j));
   for i=1:size(inputs,1)                                  
      c1=weights(1)*inputs(i,1);                          
      c2=weights(2)*inputs(i,2);                          
      c3=weights(3)*inputs(i,3);                    
      csum=c1+c2+c3;                             
      if(csum>threshold)
         output(i)=1;                                  
      else
         output(i)=0;                                   
      end
      error=desiredOutput(i)-output(i);                  
      correction= learningRate*error;                     
      weights(1)=weights(1)+inputs(i,1)*correction;     
      weights(2)=weights(2)+inputs(i,2)*correction;   
      weights(3)=weights(3)+inputs(i,3)*correction;   
      disp(sprintf('Input [ %d %d %d ]\t\t
                    DOut|COut [ %d ]|[ %d ]\t 
                    New Weights [ %1.1f %1.1f %1.1f ]'
                    ,inputs(i,1),inputs(i,2),inputs(i,3)
                    ,desiredOutput(i),output(i)
                    ,weights(1),weights(2),weights(3)));
   end
   if isequal(output,desiredOutput)break;end               
end

 

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags