<s> import numpy as np <EOL> from random import randrange <EOL> def eval_numerical_gradient ( f , x , verbose = True , h = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> grad = np . zeros_like ( x ) <EOL> it = np . nditer ( x , flags = [ '<STR_LIT>' ] , op_flags = [ '<STR_LIT>' ] ) <EOL> while not it . finished : <EOL> ix = it . multi_index <EOL> oldval = x [ ix ] <EOL> x [ ix ] = oldval + h <EOL> fxph = f ( x ) <EOL> x [ ix ] = oldval - h <EOL> fxmh = f ( x ) <EOL> x [ ix ] = oldval <EOL> grad [ ix ] = ( fxph - fxmh ) / ( <NUM_LIT:2> * h ) <EOL> if verbose : <EOL> print ix , grad [ ix ] <EOL> it . iternext ( ) <EOL> return grad <EOL> def eval_numerical_gradient_array ( f , x , df , h = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> grad = np . zeros_like ( x ) <EOL> it = np . nditer ( x , flags = [ '<STR_LIT>' ] , op_flags = [ '<STR_LIT>' ] ) <EOL> while not it . finished : <EOL> ix = it . multi_index <EOL> oldval = x [ ix ] <EOL> x [ ix ] = oldval + h <EOL> pos = f ( x ) . copy ( ) <EOL> x [ ix ] = oldval - h <EOL> neg = f ( x ) . copy ( ) <EOL> x [ ix ] = oldval <EOL> grad [ ix ] = np . sum ( ( pos - neg ) * df ) / ( <NUM_LIT:2> * h ) <EOL> it . iternext ( ) <EOL> return grad <EOL> def eval_numerical_gradient_blobs ( f , inputs , output , h = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> numeric_diffs = [ ] <EOL> for input_blob in inputs : <EOL> diff = np . zeros_like ( input_blob . diffs ) <EOL> it = np . nditer ( input_blob . vals , flags = [ '<STR_LIT>' ] , <EOL> op_flags = [ '<STR_LIT>' ] ) <EOL> while not it . finished : <EOL> idx = it . multi_index <EOL> orig = input_blob . vals [ idx ] <EOL> input_blob . vals [ idx ] = orig + h <EOL> f ( * ( inputs + ( output , ) ) ) <EOL> pos = np . copy ( output . vals ) <EOL> input_blob . vals [ idx ] = orig - h <EOL> f ( * ( inputs + ( output , ) ) ) <EOL> neg = np . copy ( output . vals ) <EOL> input_blob . vals [ idx ] = orig <EOL> diff [ idx ] = np . sum ( ( pos - neg ) * output . diffs ) / ( <NUM_LIT> * h ) <EOL> it . iternext ( ) <EOL> numeric_diffs . append ( diff ) <EOL> return numeric_diffs <EOL> def eval_numerical_gradient_net ( net , inputs , output , h = <NUM_LIT> ) : <EOL> return eval_numerical_gradient_blobs ( lambda * args : net . forward ( ) , <EOL> inputs , output , h = h ) <EOL> def grad_check_sparse ( f , x , analytic_grad , num_checks ) : <EOL> '''<STR_LIT>''' <EOL> h = <NUM_LIT> <EOL> x . shape <EOL> for i in xrange ( num_checks ) : <EOL> ix = tuple ( [ randrange ( m ) for m in x . shape ] ) <EOL> oldval = x [ ix ] <EOL> x [ ix ] = oldval + h <EOL> fxph = f ( x ) <EOL> x [ ix ] = oldval - h <EOL> fxmh = f ( x ) <EOL> x [ ix ] = oldval <EOL> grad_numerical = ( fxph - fxmh ) / ( <NUM_LIT:2> * h ) <EOL> grad_analytic = analytic_grad [ ix ] <EOL> rel_error = abs ( grad_numerical - grad_analytic ) / ( abs ( grad_numerical ) + abs ( grad_analytic ) ) <EOL> print '<STR_LIT>' % ( grad_numerical , grad_analytic , rel_error ) </s>
