Skip to content

Instantly share code, notes, and snippets.

@benanne
Created December 17, 2011 23:11
Show Gist options
  • Select an option

  • Save benanne/1491740 to your computer and use it in GitHub Desktop.

Select an option

Save benanne/1491740 to your computer and use it in GitHub Desktop.
mean of truncated exponential in theano
import numpy as np
import theano
import theano.tensor as T
# mode = theano.compile.DebugMode(check_py_code=False, require_matching_strides=False)
mode = None
# theano.config.floatX = 'float64'
l = T.vector('l')
# mean of a truncated exponential
texp_mean = 1/l - 1/(T.exp(l) - 1)
fn = theano.function([l], texp_mean, mode=mode)
# numpy equivalent
def fn_numpy(l):
return 1/l - 1/(np.exp(l) - 1)
a = np.array([-0.01, -0.1, -1.0, -10.0, -100.0, 0.01, 0.1, 1.0, 10.0, 100.0], dtype=theano.config.floatX)
print "Input: %s" % "\t".join(["%.2f" % i for i in a])
print "Theano: %s" % "\t".join(["%.2f" % i for i in fn(a)])
print "Numpy: %s" % "\t".join(["%.2f" % i for i in fn_numpy(a)])
@benanne

benanne commented Dec 17, 2011

Copy link
Copy Markdown
Author

output:

[sander@arch morb]$ ipython tmp_texp_mean_test.py
Input:      -0.01       -0.10       -1.00       -10.00      -100.00     0.01        0.10        1.00        10.00       100.00
Theano:     -100.50     -10.52      -1.73       -1.10       -1.01       99.50       9.52        0.73        0.10        0.01
Numpy:      0.50        0.51        0.58        0.90        0.99        0.50        0.49        0.42        0.10        0.01

@benanne

benanne commented Dec 17, 2011

Copy link
Copy Markdown
Author

debug mode output:

---------------------------------------------------------------------------
InvalidValueError                         Traceback (most recent call last)
/home/sander/git/morb/tmp_texp_mean_test.py in ()
     23 
     24 print "Input:  %s" % "\t".join(["%.2f" % i for i in a])
---> 25 print "Theano: %s" % "\t".join(["%.2f" % i for i in fn(a)])
     26 print "Numpy:  %s" % "\t".join(["%.2f" % i for i in fn_numpy(a)])
     27 

/usr/lib/python2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    608         t0_fn = time.time()
    609         try:
--> 610             self.fn()
    611         except Exception:
    612             if hasattr(self.fn, 'position_of_error'):

/usr/lib/python2.7/site-packages/theano/compile/debugmode.pyc in deco()
   1592                 TensorType.filter_checks_isfinite = self.maker.mode.check_isfinite
   1593                 try:
-> 1594                     return f()
   1595                 finally:
   1596                     # put back the filter_checks_isfinite


/usr/lib/python2.7/site-packages/theano/compile/debugmode.pyc in f()
   1453                             # check output values for type-correctness

   1454                             if not r.type.is_valid_value(storage_map[r][0]):
-> 1455                                 raise InvalidValueError(r, storage_map[r][0], hint='c output')
   1456 
   1457                             if thunk_py:

InvalidValueError: InvalidValueError
        type(variable) = TensorType(float32, vector)
        variable       = Elemwise{exp,no_inplace}.0
        type(value)    = 
        dtype(value)   = float32
        shape(value)   = (10,)
        value          = [  9.90049839e-01   9.04837430e-01   3.67879450e-01   4.53999310e-05
   3.78350585e-44   1.01005018e
        min(value)     = 3.78351e-44
        max(value)     = inf
        isfinite       = False
        client_node    = None
        hint           = c output
        specific_hint  = none
        context        = ...
  Elemwise{exp,no_inplace} [@159933772] ''   
   |l [@159530796]

@benanne

benanne commented Dec 17, 2011

Copy link
Copy Markdown
Author

debug mode output with only negative values:

---------------------------------------------------------------------------
BadOptimization                           Traceback (most recent call last)
/home/sander/git/morb/tmp_texp_mean_test.py in ()
     24 
     25 print "Input:  %s" % "\t".join(["%.2f" % i for i in a])
---> 26 print "Theano: %s" % "\t".join(["%.2f" % i for i in fn(a)])
     27 print "Numpy:  %s" % "\t".join(["%.2f" % i for i in fn_numpy(a)])
     28 

/usr/lib/python2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    608         t0_fn = time.time()
    609         try:
--> 610             self.fn()
    611         except Exception:
    612             if hasattr(self.fn, 'position_of_error'):

/usr/lib/python2.7/site-packages/theano/compile/debugmode.pyc in deco()
   1592                 TensorType.filter_checks_isfinite = self.maker.mode.check_isfinite
   1593                 try:
-> 1594                     return f()
   1595                 finally:
   1596                     # put back the filter_checks_isfinite


/usr/lib/python2.7/site-packages/theano/compile/debugmode.pyc in f()
   1521                     gc.collect()
   1522 
-> 1523                 _find_bad_optimizations(order, env.equivalence_tracker.reasons, r_vals)
   1524 
   1525                 #####


/usr/lib/python2.7/site-packages/theano/compile/debugmode.pyc in _find_bad_optimizations0(order, reasons, r_vals)
    758                             reason=reason,
    759                             old_graph=old_graph_str,
--> 760                             new_graph=new_graph_str)
    761 
    762 def _find_bad_optimizations1(order, reasons, r_vals):

BadOptimization: BadOptimization Error 
  Variable: id 159732940 Elemwise{mul,no_inplace}.0
  Op Elemwise{mul,no_inplace}(sigmoid.0, TensorConstant{(1,) of 1.0})
  Value Type: 
  Old Value shape, dtype, strides: (5,) float32 (4,)

  Old Value:  [-100.50088501  -10.50833321   -1.58197677   -1.00004542   -1.        ]
  New Value shape, dtype, strides: (5,) float32 (4,)

  New Value:  [ 0.5025      0.52497917  0.7310586   0.99995458  1.        ]
  Max Abs Diff:  101.003
  Mean Abs Diff:  23.6699478149
  Median Abs Diff:  2.31303548813
  Std Abs Diff:  38.8212223492
  Max Rel Diff:  1.0
  Mean Rel Diff:  1.0
  Median Rel Diff:  1.0
  Std Rel Diff:  0.0

  Reason:  local_exp_over_1_plus_exp
  Old Graph:
  Elemwise{true_div,no_inplace} [@159653356] ''   
   |TensorConstant{(1,) of 1.0} [@159734220]
   |Elemwise{add,no_inplace} [@159711276] ''   
   | |TensorConstant{(1,) of -1.0} [@159699660]
   | |Elemwise{exp,no_inplace} [@159418700] ''   
   | | |l [@159045260]

  New Graph:
  Elemwise{mul,no_inplace} [@159732940] ''   
   |sigmoid [@159766572] ''   
   | |Elemwise{neg,no_inplace} [@159765772] ''   
   | | |l [@159045260]
   |TensorConstant{(1,) of 1.0} [@159734220]


Hint: relax the tolerance by setting tensor.cmp_sloppy=1
  or even tensor.cmp_sloppy=2 for less-strict comparison

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment