%% MLP with backpropagation learning for XOR
%% TT 13/10/15
 clear; close all; hold on;
 
 % training vectors (XOR)
 x=[0 1 0 1; 0 0 1 1; 1 1 1 1];
 z=[0 1 1 0];
 
 % model specifications
 Ni=size(x,1); Nh=3; No=size(z,1);
 wh=randn(Nh,Ni); wo=randn(No,Nh); dwo=0; dwh=0;

 % Applying and training network with sigmoid activation function
 for trial=1:10000;
 
     h=1./(1+exp(-wh*x));     
     y=1./(1+exp(-wo*h));
     
     % delta term for each layer (objective function error)    
     do=(y.*(1-y)).*(z-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-z).^2)/4;
     d2(trial)=sum(abs((y>0.5)-z))/4;
     
 end
 plot(d); plot(d2,'r'); axis([0,size(d,2),0,1]);
