public double learn(int p,int step)
  {
    double err=0;
    for (int runs=0;runs<step;runs++)
    {
    forward();
    for (int k=0;k<Zdim;k++) 
       { double ee=(outpat[p][k]-Z[k]);
         eZ[k]=ee*(1-Z[k])*Z[k];
                 // this part equals G'(Znet) for b=0.5
         err+=ee*ee;
        }
     
    for (int i=0;i<Ydim+1;i++) 
     {
       eY[i]=0;
       for (int k=0;k<Zdim;k++) eY[i]+=W[k][i]*eZ[k]; //each Y[i]'s error share
       eY[i]=eY[i]*(1-Y[i])*Y[i];
                   // this part equals G'(Ynet) for b=0.5
     }
       
    for (int i=0;i<Ydim+1;i++)
    for (int j=0;j<Xdim+1;j++) 
      {                        // momentum term 
       int M=Ydim/Zdim;
       int N=30;// Xdim/Zdim;
       int R=N/M;

        dw[i][j]=eta*eY[i]*X[j] + beta*_dw[i][j];
        _dw[i][j]=dw[i][j];

      }

    for (int k=0;k<Zdim  ;k++)
    for (int i=0;i<Ydim+1;i++) 
     {
      dW[k][i]=eta*eZ[k]*Y[i] + beta*_dW[k][i];
      _dW[k][i]=dW[k][i];
     }

   addto(W,Zdim,Ydim+1,dW);
   addto(w,Ydim+1,Xdim+1,dw);
   } 
   return err/step;
 }