Skip to content

Instantly share code, notes, and snippets.

@darthsuogles
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save darthsuogles/e30b88098857e093c98c to your computer and use it in GitHub Desktop.

Select an option

Save darthsuogles/e30b88098857e093c98c to your computer and use it in GitHub Desktop.
Checking numerical value of a Theano expression
import inspect # for line number
import re # for extracting debug information
_check_expr_regex = re.compile('THEANO_CHECK_EXPR\(.+\)')
def THEANO_CHECK_EXPR( expr, verbose = False ):
"""
Check the expression
Limited resolution at the line level
"""
caller = inspect.currentframe().f_back
try:
caller_expr = inspect.stack()[1][4][0]
caller_expr = ', '.join(_check_expr_regex.findall( caller_expr ))
except:
caller_expr = "NA"; pass
caller_lineno = caller.f_back.f_lineno
def_lineno = caller.f_lineno
expr_str = theano.printing.debugprint( expr, depth = 1, print_type = False, ids = "", file = 'str' )
print_prefix = 'DEBUG: [CALLER Ln %d, DEF Ln %d], { %s }' % ( caller_lineno, def_lineno, caller_expr.strip() )
if verbose:
print_prefix += ', ' + expr_str.strip()
return theano.printing.Print( print_prefix + ":" )( expr )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment