def PatternOptimizer(p1, p2, ign=True): return gof.OpKeyOptimizer(gof.PatternSub(p1, p2), ignore_newtrees=ign)
softplus = elemwise.Elemwise(scalar_softplus, name="softplus") pprint.assign(softplus, printing.FunctionPrinter("softplus")) def _skip_mul_1(r): if r.owner and r.owner.op == tensor.mul: not_is_1 = [i for i in r.owner.inputs if not _is_1(i)] if len(not_is_1) == 1: return not_is_1[0] logsigm_to_softplus = gof.PatternSub( (tensor.log, (sigmoid, "x")), (tensor.neg, (softplus, (tensor.neg, "x"))), allow_multiple_clients=True, values_eq_approx=values_eq_approx_remove_inf, skip_identities_fn=_skip_mul_1, ) def _is_1(expr): """ Returns ------- bool True iff expr is a constant close to 1. """ try:
scalar_softplus = ScalarSoftplus(scalar.upgrade_to_float, name='scalar_softplus') softplus = elemwise.Elemwise(scalar_softplus, name='softplus') pprint.assign(softplus, printing.FunctionPrinter('softplus')) def _skip_mul_1(r): if r.owner and r.owner.op == tensor.mul: not_is_1 = [i for i in r.owner.inputs if not _is_1(i)] if len(not_is_1) == 1: return not_is_1[0] logsigm_to_softplus = gof.PatternSub( (tensor.log, (sigmoid, 'x')), (tensor.neg, (softplus, (tensor.neg, 'x'))), allow_multiple_clients=True, skip_identities_fn=_skip_mul_1) def _is_1(expr): """rtype bool. True iff expr is a constant close to 1 """ try: v = opt.get_scalar_constant_value(expr) return numpy.allclose(v, 1) except tensor.NotScalarConstantError: return False log1msigm_to_softplus = gof.PatternSub( (tensor.log,
from six import iteritems from theano.compile.io import In, Out from theano.compile import function from theano.compile import UnusedInputError from theano.gof import MissingInputError from theano.compat import exc_message from theano.tests.unittest_tools import SkipTest from theano import tensor from theano import tensor as T import theano import numpy as N PatternOptimizer = lambda p1, p2, ign=True: gof.OpKeyOptimizer( gof.PatternSub(p1, p2), ignore_newtrees=ign) def checkfor(testcase, fn, E): try: fn() except Exception as e: if isinstance(e, E): # we got the exception we wanted return else: # we did not get the exception we wanted raise # fn worked, but it shouldn't have testcase.fail()
from theano import config, gof from theano.compile.io import In, Out from theano.compile import function from theano.compile import UnusedInputError from theano.gof import MissingInputError from theano.gof.python25 import all, any from theano import tensor from theano import tensor as T import theano import numpy as N from numpy.testing.noseclasses import KnownFailureTest PatternOptimizer = lambda p1, p2, ign=True: gof.OpKeyOptimizer(gof.PatternSub(p1, p2), ignore_newtrees=ign) def checkfor(testcase, fn, E): try: fn() except Exception, e: if isinstance(e, E): # we got the exception we wanted return else: # we did not get the exception we wanted raise # fn worked, but it shouldn't have testcase.fail()