%% MLP with backpropagation learning for XOR
%% TT 13/10/15
 clear; close all; hold on;
 
 % training vectors (XOR)
X=[0 0 1 1; ...
   0 1 0 1; ...
   1 1 1 1];

Y=[0 1 1 0];
 
 % model specifications
 Ni=size(X,1); Nh=3; No=size(Y,1);
 wh=randn(Nh,Ni); wo=randn(No,Nh); dwo=0; dwh=0;

 % Applying and training network with sigmoid activation function
 for trial=1:2000;
 
     h=1./(1+exp(-wh*X));     
     y=1./(1+exp(-wo*h));
     
     % delta term for each layer (objective function error)    
     do=(y.*(1-y)).*(Y-y);
     dh=(h.*(1-h)).*(wo'*do);
     
     %update weights
     dwo=0.9*dwo+h*do';
     dwh=0.9*dwh+X*dh';
     
     wo=wo+0.1*dwo';
     wh=wh+0.1*dwh';
     
     % test error 
     d(trial)=sum((y-Y).^2)/4;
     d2(trial)=sum(abs((y>0.5)-Y))/4;
     
 end
 plot(d); plot(d2,'r'); axis([0,size(d,2),0,1]);
