#NEWLINE# def __init__( self , nan_is_error = None , inf_is_error = None , big_is_error = None , optimizer = 'default' , linker = None ) : #NEWLINE# #INDENT# self . provided_optimizer = optimizer #NEWLINE# if ( nan_is_error is None ) : #NEWLINE# #INDENT# nan_is_error = config . NanGuardMode . nan_is_error #NEWLINE# #UNINDENT# if ( inf_is_error is None ) : #NEWLINE# #INDENT# inf_is_error = config . NanGuardMode . inf_is_error #NEWLINE# #UNINDENT# if ( big_is_error is None ) : #NEWLINE# #INDENT# big_is_error = config . NanGuardMode . big_is_error #NEWLINE# #UNINDENT# assert ( nan_is_error or inf_is_error or big_is_error ) #NEWLINE# compile_gpu_func ( nan_is_error , inf_is_error , big_is_error ) #NEWLINE# def do_check_on( var , nd , f , is_input ) : #NEWLINE# #INDENT# '\n            Checks `var` for NaNs / Infs. If detected, raises an exception\n            and / or prints information about `nd`, `f`, and `is_input` to\n            help the user determine the cause of the invalid values.\n\n            Parameters\n            ----------\n            var : numpy.ndarray\n                The value to be checked.\n            nd : theano.gof.Apply\n                The Apply node being executed.\n            f : callable\n                The thunk for the apply node.\n            is_input : bool\n                If True, `var` is an input to `nd`.\n                If False, it is an output.\n\n            ' #NEWLINE# error = False #NEWLINE# sio = StringIO ( ) #NEWLINE# if nan_is_error : #NEWLINE# #INDENT# if contains_nan ( var , nd ) : #NEWLINE# #INDENT# print ( 'NaN detected' , file = sio ) #NEWLINE# error = True #NEWLINE# #UNINDENT# #UNINDENT# if inf_is_error : #NEWLINE# #INDENT# if contains_inf ( var , nd ) : #NEWLINE# #INDENT# print ( 'Inf detected' , file = sio ) #NEWLINE# error = True #NEWLINE# #UNINDENT# #UNINDENT# if big_is_error : #NEWLINE# #INDENT# err = False #NEWLINE# if isinstance ( var , theano . gof . type . CDataType . _cdata_type ) : #NEWLINE# #INDENT# err = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( var , np . random . mtrand . RandomState ) : #NEWLINE# #INDENT# err = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( var . size == 0 ) : #NEWLINE# #INDENT# err = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( cuda . cuda_available and isinstance ( var , cuda . CudaNdarray ) ) : #NEWLINE# #INDENT# err = ( f_gpuabsmax ( var . reshape ( var . size ) ) > 10000000000.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# err = ( np . abs ( var ) . max ( ) > 10000000000.0 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if err : #NEWLINE# #INDENT# print ( 'Big value detected' , file = sio ) #NEWLINE# error = True #NEWLINE# #UNINDENT# #UNINDENT# if error : #NEWLINE# #INDENT# if ( not is_input ) : #NEWLINE# #INDENT# print ( 'NanGuardMode found an error in the output of a node in this variable:' , file = sio ) #NEWLINE# print ( theano . printing . debugprint ( nd , file = 'str' ) , file = sio ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'NanGuardMode found an error in an input of this node.' , file = sio ) #NEWLINE# print ( 'Node:' , file = sio ) #NEWLINE# print ( nd , file = sio ) #NEWLINE# print ( 'The input variable that cause problem:' , file = sio ) #NEWLINE# print ( theano . printing . debugprint ( nd , file = 'str' ) , file = sio ) #NEWLINE# #UNINDENT# msg = sio . getvalue ( ) #NEWLINE# if ( config . NanGuardMode . action == 'raise' ) : #NEWLINE# #INDENT# raise AssertionError ( msg ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( config . NanGuardMode . action == 'pdb' ) : #NEWLINE# #INDENT# print ( msg ) #NEWLINE# import pdb #NEWLINE# pdb . set_trace ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( config . NanGuardMode . action == 'warn' ) : #NEWLINE# #INDENT# logger . error ( msg ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# def nan_check( i , node , fn ) : #NEWLINE# #INDENT# '\n            Runs `fn` while checking its inputs and outputs for NaNs / Infs.\n\n            Parameters\n            ----------\n            i :\n                Currently ignored.\n                TODO: determine why it is here or remove).\n            node : theano.gof.Apply\n                The Apply node currently being executed.\n            fn : callable\n                The thunk to execute for this Apply node.\n\n            ' #NEWLINE# inputs = fn . inputs #NEWLINE# for ( x , var ) in zip ( inputs , node . inputs ) : #NEWLINE# #INDENT# if ( ( var . owner is None ) and getattr ( var . tag , 'nan_guard_mode_check' , True ) ) : #NEWLINE# #INDENT# do_check_on ( x [ 0 ] , node , fn , True ) #NEWLINE# #UNINDENT# #UNINDENT# fn ( ) #NEWLINE# outputs = fn . outputs #NEWLINE# for ( x , var ) in zip ( outputs , node . outputs ) : #NEWLINE# #INDENT# if getattr ( var . tag , 'nan_guard_mode_check' , True ) : #NEWLINE# #INDENT# do_check_on ( x [ 0 ] , node , fn , False ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# wrap_linker = theano . gof . WrapLinker ( [ theano . gof . OpWiseCLinker ( ) ] , nan_check ) #NEWLINE# super ( NanGuardMode , self ) . __init__ ( wrap_linker , optimizer = self . provided_optimizer )