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