def get_numerical_gradient(f, tensors, delta=1e-05):
 gradients = []
 for i in range(len(tensors)):
  tensors[i] = tensors[i].astype(np.float64)
  gradients.append(np.zeros(tensors[i].shape))
 for (tensor, gradient) in zip(tensors, gradients):
  tensor_flat = tensor.reshape(((-1),))
  gradient_flat = gradient.reshape(((-1),))
  for idx in range(len(tensor_flat)):
   backup = tensor_flat[idx]
   tensor_flat[idx] = (tensor_flat[idx] + delta)
   f_inc = np.sum(f(np, *tensors))
   tensor_flat[idx] = (backup - delta)
   f_dec = np.sum(f(np, *tensors))
   tensor_flat[idx] = backup
   gradient_flat[idx] = ((f_inc - f_dec) / (2.0 * delta))
 return gradients

def get_numerical_gradient ( f , tensors , delta = 1e-05 ) : gradients = [ ] for i in range ( len ( tensors ) ) : tensors [ i ] = tensors [ i ] . astype ( np . float64 ) gradients . append ( np . zeros ( tensors [ i ] . shape ) ) for ( tensor , gradient ) in zip ( tensors , gradients ) : tensor_flat = tensor . reshape ( ( ( - 1 ) , ) ) gradient_flat = gradient . reshape ( ( ( - 1 ) , ) ) for idx in range ( len ( tensor_flat ) ) : backup = tensor_flat [ idx ] tensor_flat [ idx ] = ( tensor_flat [ idx ] + delta ) f_inc = np . sum ( f ( np , * tensors ) ) tensor_flat [ idx ] = ( backup - delta ) f_dec = np . sum ( f ( np , * tensors ) ) tensor_flat [ idx ] = backup gradient_flat [ idx ] = ( ( f_inc - f_dec ) / ( 2.0 * delta ) ) return gradients
sum all of fs elements to make the last layer error as one .