%% MLP with backpropagation learning on XOR problem
 clear; clf;
 N_i=2; N_h=2; N_o=1;
 w_h=rand(N_h,N_i)-0.5; w_o=rand(N_o,N_h)-0.5;

 % training vectors (XOR)
 r_i=[0 1 0 1 ; 0 0 1 1];
 r_d=[0 1 1 0];

 % Updating and training network with sigmoid activation function
 for sweep=1:10000;
   % training randomly on one pattern
     i=ceil(4*rand);
     r_h=1./(1+exp(-w_h*r_i(:,i)));
     r_o=1./(1+exp(-w_o*r_h));
     d_o=(r_o.*(1-r_o)).*(r_d(:,i)-r_o);
     d_h=(r_h.*(1-r_h)).*(w_o'*d_o);
     w_o=w_o+0.7*(r_h*d_o')';
     w_h=w_h+0.7*(r_i(:,i)*d_h')';
   % test all pattern
     r_o_test=1./(1+exp(-w_o*(1./(1+exp(-w_h*r_i)))));
     d(sweep)=0.5*sum((r_o_test-r_d).^2);
 end
 plot(d)
