Created
December 17, 2011 23:11
-
-
Save benanne/1491740 to your computer and use it in GitHub Desktop.
mean of truncated exponential in theano
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)]) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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