function [grad,res]=gradient(M,N,x,t,w,beta,res,k,ioption);
% M  - input- # data examples
% N  - input- # unknowns in layer
% t  - input- # observed data
grad=zeros(N,1);residual=zeros(M,1);
for m=1:M        % Loop over M data examples in training set
  z=w'*x(:,m);
  g=1/(1+exp(-z*beta));
  dg=beta*g*(1-g);
 % residual(m)=z-t(m);                  % residual by Jerry
  residual(m)=g-t(m);                  % residual by Zongcai
  grad0  = dg*x(:,m)*residual(m);      % Stochastic gradient
%  grad1  = -x(:,m)*residual(m)*ioption;% cross-entropy gradient
  grad   = grad+grad0;                 % Batch gradient
end
res(k)=sqrt(residual'*residual)/M;