                                 theta2Rows * theta2Cols );
 
   tempMatrix = (floatType_t *) malloc( sizeof(floatType_t) *
-                               ( Xexamples * (theta1Rows+1) + 
-                                 Xexamples * (theta1Rows+1) +
-                                 Xexamples * (theta2Rows+1) + 
-                                 theta2Cols + 11) );
+                               ( Xexamples * (theta1Rows+1) + //z2 
+                                 Xexamples * (theta1Rows+1) + //a2
+                                 Xexamples * (theta2Rows+1) + //a3
+                                 Xexamples * (theta1Rows+1) + //delta2
+                                 Xexamples * 11 ) );            //delta3
 
   for( int i = 0; i < Xexamples; i++ ) 
     X[INDX(0,i,Xfeatures)] = (floatType_t) 1.0;

  *cost = jTemp;
#endif
#endif
#if 1
  floatType_t *delta3;
  delta3 = yTemp;



/* offset the pointers in the scratch memory */

//  z2 = tempMatrix;
//  a2 = &z2[INDX(Xexamples,theta1Rows,Xexamples)];
//  a3 = &a2[INDX(Xexamples,theta1Rows+1,Xexamples)];
//  yTemp = &a3[INDX(Xexamples,theta2Rows+1,Xexamples)];
//  delta2 = &yTemp[11];
#if 0
  z2 = tempMatrix;
  a2 = &z2[INDX(Xexamples,theta1Rows+1,Xexamples)];
  a3 = &a2[INDX(Xexamples,theta1Rows+1,Xexamples)];
  delta2 = &a3[INDX(Xexamples,theta2Rows+1,Xexamples)];
  yTemp = &delta2[INDX(Xexamples,theta1Rows+1,Xexamples)];
#endif
  z2     = tempMatrix;
  a2     = &z2[Xexamples*(theta1Rows+1)];
  a3     = &a2[Xexamples*(theta1Rows+1)];
  delta2 = &a3[Xexamples*(theta2Rows+1)];
  yTemp  = &delta2[Xexamples*(theta1Rows+1)];

  floatType_t *d_tempMatrix;
  CUDA_CALL( cudaMalloc( &d_tempMatrix, sizeof(floatType_t) *
                               ( Xexamples * (theta1Rows+1) + //z2 
                                 Xexamples * (theta1Rows+1) + //a2
                                 Xexamples * (theta2Rows+1) + //a3
                                 Xexamples * (theta1Rows+1) + //delta2
                                 Xexamples * 11 ) ) );            //delta3

  floatType_t *d_z2, *d_a2, *d_a3, *d_yTemp, *d_delta2;

  d_z2     = d_tempMatrix;
  d_a2     = &d_z2[Xexamples*(theta1Rows+1)];
  d_a3     = &d_a2[Xexamples*(theta1Rows+1)];
  d_delta2 = &d_a3[Xexamples*(theta2Rows+1)];
  d_yTemp  = &d_delta2[Xexamples*(theta1Rows+1)];

#if 0
  d_z2 = d_tempMatrix;
  d_a2 = &d_z2[INDX(Xexamples,theta1Rows,Xexamples)];
  d_a3 = &d_a2[INDX(Xexamples,theta1Rows+1,Xexamples)];
  d_delta2 = &d_a3[INDX(Xexamples,theta2Rows+1,Xexamples)];
  d_yTemp = &d_delta2[INDX(Xexamples,theta1Rows+1,Xexamples)];
#endif

  checkCUDNN( cudnnSetTensor4dDescriptor(srcTensorDesc,
                                         CUDNN_TENSOR_NCHW,
