 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Back-propagation network: Letter Recognition
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 clear; clf;

 N_i=156; N_h=50; N_o=26;

 w_h=1*(rand(N_h,N_i)-0.5); w_o=1*(rand(N_o,N_h)-0.5);

 % training vectors (Letters)
 load pattern1;
 r_i=reshape(pattern1',156,26);
 r_d=diag(ones(1,26));

 %Updating and training network with sigmoid activation function
 for sweep=1:100;
     i=ceil(26*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)=sum(sum((r_o_test-r_d).^2));

       %disp([sweep,d(sweep)])
 end

 plot(d)
 