function [grad11,grad22,res]=gradient2(M,x,t,w1,w2,k,res,ioption);
% M   - input- # data examples
%(x,t)- input- Training data pairs
% w1  - input- filters for layer 1 
% w2  - input- filters for layer 2 
% t   - input- # observed data
% de/w2 = (a[2]-t)*dg(z[2])*a[1]
% dg(z[2])= g(z[2])(1-g(z[2])) 
% de/w1 = (a[2]-t)*dg(z[2])*w2*dg(z[1])*a[0]
% dg(z[1])= g(z[1])(1-g(z[1])) 
[N,N1]=size(w1);grad1=zeros(N,N1);grad11=grad1;
N1=length(w2);grad2=zeros(N1,1);grad22=grad2;
residual=zeros(M,1);
for m=1:M        % Loop over M data examples in training set 
      a0=x(:,m);
      z1=w1'*a0;
      g1=1./(1+exp(-z1));
      a1=g1;
      dg1=g1.*(1-g1);
      z2=w2'*a1;
      g2=1/(1+exp(-z2));
      a2=g2;
      dg2=g2*(1-g2);
       
      %size(w2),size(dg1),size(a0)
      grad1=a0*((a2-t(m))*dg2*w2.*dg1)'; % Stochastic gradient 1st layer
      grad2=(a2-t(m))*dg2*a1;        % Stochastic gradient 2nd layer
      grad11=grad11+grad1;    % Batch gradient layer 1
      grad22=grad22+grad2;    % Batch gradient layer 2
      
      residual(m)=a2-t(m);       % Residual
end
res(k)=sqrt(residual'*residual)/M;  % Batch Residual