def verify_grad(fun, pt, n_tests=2, rng=None, eps=None, out_type=None, abs_tol=None, rel_tol=None, mode=None, cast_to_output_type=False, no_debug_ref=True):
 from theano import compile, shared
 import theano.tensor
 from theano.tensor import as_tensor_variable, TensorType
 assert isinstance(pt, (list, tuple))
 pt = [numpy.array(p) for p in pt]
 for (i, p) in enumerate(pt):
  if (p.dtype not in ('float16', 'float32', 'float64')):
   raise TypeError(('verify_grad can work only with floating point inputs, but input %i has dtype "%s".' % (i, p.dtype)))
 _type_tol = dict(float16=0.05, float32=0.01, float64=0.0001)
 if (abs_tol is None):
  abs_tol = builtins.max((_type_tol[str(p.dtype)] for p in pt))
 if (rel_tol is None):
  rel_tol = builtins.max((_type_tol[str(p.dtype)] for p in pt))
 if (rng is None):
  raise TypeError('rng should be a valid instance of numpy.random.RandomState. You may want to use theano.tests.unittest_tools.verify_grad instead of theano.gradient.verify_grad.')
 def function(inputs, output, name, mode=mode):
  f = compile.function(inputs, output, accept_inplace=True, allow_input_downcast=True, mode=mode, on_unused_input='ignore', name=name)
  return f
 tensor_pt = [TensorType(as_tensor_variable(p).dtype, as_tensor_variable(p).broadcastable)(name=('input %i' % i)) for (i, p) in enumerate(pt)]
 o_output = fun(*tensor_pt)
 if isinstance(o_output, list):
  raise NotImplementedError('cant (yet) autotest gradient of fun with multiple outputs')
 o_fn = function(tensor_pt, o_output, name='gradient.py fwd')
 o_fn_out = o_fn(*[p.copy() for p in pt])
 if (isinstance(o_fn_out, tuple) or isinstance(o_fn_out, list)):
  raise TypeError('It seems like you are trying to use verify_grad on an op or a function which outputs a list: there should be a single (array-like) output instead')
 def random_projection():
  plain = (rng.rand(*o_fn_out.shape) + 0.5)
  if (cast_to_output_type and (o_output.dtype == 'float32')):
   return numpy.array(plain, o_output.dtype)
  return plain
 t_r = shared(random_projection())
 t_r.name = 'random_projection'
 cost = theano.tensor.sum((t_r * o_output))
 if no_debug_ref:
  mode_for_cost = mode_not_debug(mode)
 else:
  mode_for_cost = mode
 cost_fn = function(tensor_pt, cost, name='gradient.py cost', mode=mode_for_cost)
 symbolic_grad = grad(cost, tensor_pt, disconnected_inputs='ignore')
 grad_fn = function(tensor_pt, symbolic_grad, name='gradient.py symbolic grad')
 for test_num in xrange(n_tests):
  try:
   num_grad = numeric_grad(cost_fn, [p.copy() for p in pt], eps, out_type)
   analytic_grad = grad_fn(*[p.copy() for p in pt])
   assert isinstance(analytic_grad, list)
   (max_arg, max_err_pos, max_abs_err, max_rel_err) = num_grad.max_err(analytic_grad, abs_tol, rel_tol)
   if ((max_abs_err > abs_tol) and (max_rel_err > rel_tol)):
    raise verify_grad.E_grad(max_arg, max_err_pos, max_abs_err, max_rel_err, abs_tol, rel_tol)
   if (test_num < (n_tests - 1)):
    t_r.set_value(random_projection(), borrow=True)
  except Exception as e:
   e.args += ('\nThe error happened with the following inputs:', pt, '\nThe value of eps is:', eps, '\nThe out_type is:', out_type)
   raise

def verify_grad ( fun , pt , n_tests = 2 , rng = None , eps = None , out_type = None , abs_tol = None , rel_tol = None , mode = None , cast_to_output_type = False , no_debug_ref = True ) : from theano import compile , shared import theano . tensor from theano . tensor import as_tensor_variable , TensorType assert isinstance ( pt , ( list , tuple ) ) pt = [ numpy . array ( p ) for p in pt ] for ( i , p ) in enumerate ( pt ) : if ( p . dtype not in ( 'float16' , 'float32' , 'float64' ) ) : raise TypeError ( ( 'verify_grad can work only with floating point inputs, but input %i has dtype "%s".' % ( i , p . dtype ) ) ) _type_tol = dict ( float16 = 0.05 , float32 = 0.01 , float64 = 0.0001 ) if ( abs_tol is None ) : abs_tol = builtins . max ( ( _type_tol [ str ( p . dtype ) ] for p in pt ) ) if ( rel_tol is None ) : rel_tol = builtins . max ( ( _type_tol [ str ( p . dtype ) ] for p in pt ) ) if ( rng is None ) : raise TypeError ( 'rng should be a valid instance of numpy.random.RandomState. You may want to use theano.tests.unittest_tools.verify_grad instead of theano.gradient.verify_grad.' ) def function ( inputs , output , name , mode = mode ) : f = compile . function ( inputs , output , accept_inplace = True , allow_input_downcast = True , mode = mode , on_unused_input = 'ignore' , name = name ) return f tensor_pt = [ TensorType ( as_tensor_variable ( p ) . dtype , as_tensor_variable ( p ) . broadcastable ) ( name = ( 'input %i' % i ) ) for ( i , p ) in enumerate ( pt ) ] o_output = fun ( * tensor_pt ) if isinstance ( o_output , list ) : raise NotImplementedError ( 'cant (yet) autotest gradient of fun with multiple outputs' ) o_fn = function ( tensor_pt , o_output , name = 'gradient.py fwd' ) o_fn_out = o_fn ( * [ p . copy ( ) for p in pt ] ) if ( isinstance ( o_fn_out , tuple ) or isinstance ( o_fn_out , list ) ) : raise TypeError ( 'It seems like you are trying to use verify_grad on an op or a function which outputs a list: there should be a single (array-like) output instead' ) def random_projection ( ) : plain = ( rng . rand ( * o_fn_out . shape ) + 0.5 ) if ( cast_to_output_type and ( o_output . dtype == 'float32' ) ) : return numpy . array ( plain , o_output . dtype ) return plain t_r = shared ( random_projection ( ) ) t_r . name = 'random_projection' cost = theano . tensor . sum ( ( t_r * o_output ) ) if no_debug_ref : mode_for_cost = mode_not_debug ( mode ) else : mode_for_cost = mode cost_fn = function ( tensor_pt , cost , name = 'gradient.py cost' , mode = mode_for_cost ) symbolic_grad = grad ( cost , tensor_pt , disconnected_inputs = 'ignore' ) grad_fn = function ( tensor_pt , symbolic_grad , name = 'gradient.py symbolic grad' ) for test_num in xrange ( n_tests ) : try : num_grad = numeric_grad ( cost_fn , [ p . copy ( ) for p in pt ] , eps , out_type ) analytic_grad = grad_fn ( * [ p . copy ( ) for p in pt ] ) assert isinstance ( analytic_grad , list ) ( max_arg , max_err_pos , max_abs_err , max_rel_err ) = num_grad . max_err ( analytic_grad , abs_tol , rel_tol ) if ( ( max_abs_err > abs_tol ) and ( max_rel_err > rel_tol ) ) : raise verify_grad . E_grad ( max_arg , max_err_pos , max_abs_err , max_rel_err , abs_tol , rel_tol ) if ( test_num < ( n_tests - 1 ) ) : t_r . set_value ( random_projection ( ) , borrow = True ) except Exception as e : e . args += ( '\nThe error happened with the following inputs:' , pt , '\nThe value of eps is:' , eps , '\nThe out_type is:' , out_type ) raise
wrapper for tensor/basic .