def with_linker_inplace(self, linker, op, type, rand_val): for shape_info in ("complete", "only_broadcastable", "none"): for xsh, ysh in [ ((5, 5), (5, 5)), ((5, 5), (1, 5)), ((5, 5), (5, 1)), ((1, 1), (1, 1)), ((2, 3, 4, 5), (2, 3, 4, 5)), ((2, 3, 4, 5), (1, 3, 1, 5)), ((2, 3, 4, 5), (1, 1, 1, 1)), ((), ()), ]: if shape_info == "complete": x_type = type(aesara.config.floatX, shape=xsh) y_type = type(aesara.config.floatX, shape=ysh) elif shape_info == "only_broadcastable": # This condition is here for backwards compatibility, when the only # type shape provided by Aesara was broadcastable/non-broadcastable x_type = type( aesara.config.floatX, broadcastable=[(entry == 1) for entry in xsh], ) y_type = type( aesara.config.floatX, broadcastable=[(entry == 1) for entry in ysh], ) else: x_type = type(aesara.config.floatX, shape=[None for _ in xsh]) y_type = type(aesara.config.floatX, shape=[None for _ in ysh]) x = x_type("x") y = y_type("y") e = op(aes.Add(aes.transfer_type(0)), {0: 0})(x, y) f = make_function(copy(linker).accept(FunctionGraph([x, y], [e]))) xv = rand_val(xsh) yv = rand_val(ysh) zv = xv + yv f(xv, yv) assert (xv == zv).all() # test Elemwise.infer_shape # the Shape op don't implement c_code! if isinstance(linker, PerformLinker): x = x_type("x") y = y_type("y") e = op(aes.Add(aes.transfer_type(0)), {0: 0})(x, y) f = make_function( copy(linker).accept(FunctionGraph([x, y], [e.shape])) ) xv = rand_val(xsh) yv = rand_val(ysh) zv = xv + yv assert xv.shape == zv.shape assert tuple(f(xv, yv)) == zv.shape
def with_linker_inplace(self, linker, op, type, rand_val): for xsh, ysh in [ ((5, 5), (5, 5)), ((5, 5), (1, 5)), ((5, 5), (5, 1)), ((1, 1), (1, 1)), ((2, 3, 4, 5), (2, 3, 4, 5)), ((2, 3, 4, 5), (1, 3, 1, 5)), ((2, 3, 4, 5), (1, 1, 1, 1)), ((), ()), ]: x = type(aesara.config.floatX, [(entry == 1) for entry in xsh])("x") y = type(aesara.config.floatX, [(entry == 1) for entry in ysh])("y") e = op(aes.Add(aes.transfer_type(0)), {0: 0})(x, y) f = copy(linker).accept(FunctionGraph([x, y], [e])).make_function() xv = rand_val(xsh) yv = rand_val(ysh) zv = xv + yv f(xv, yv) assert (xv == zv).all() # test Elemwise.infer_shape # the Shape op don't implement c_code! if isinstance(linker, PerformLinker): x = type(aesara.config.floatX, [(entry == 1) for entry in xsh])("x") y = type(aesara.config.floatX, [(entry == 1) for entry in ysh])("y") e = op(aes.Add(aes.transfer_type(0)), {0: 0})(x, y) f = (copy(linker).accept(FunctionGraph( [x, y], [e.shape])).make_function()) xv = rand_val(xsh) yv = rand_val(ysh) zv = xv + yv f(xv, yv) assert xv.shape == zv.shape
def test_fill(self): for linker, op, t, rval in zip( self.linkers, [self.op, self.cop], [self.type, self.ctype], [self.rand_val, self.rand_cval], ): x = t(aesara.config.floatX, (False, False))("x") y = t(aesara.config.floatX, (True, True))("y") e = op(aes.Second(aes.transfer_type(0)), {0: 0})(x, y) f = make_function(linker().accept(FunctionGraph([x, y], [e]))) xv = rval((5, 5)) yv = rval((1, 1)) f(xv, yv) assert (xv == yv).all()
def _scal_inplace(symbol): """Replace a symbol definition with an elementwise version of the corresponding scalar Op""" symbolname = symbol.__name__ inplace = symbolname.endswith("_inplace") if inplace: scalar_op = getattr(scal, symbolname[:-len("_inplace")]) inplace_scalar_op = scalar_op.__class__(scal.transfer_type(0)) rval = elemwise.Elemwise(inplace_scalar_op, {0: 0}, name=symbolname) else: scalar_op = getattr(scal, symbolname) rval = elemwise.Elemwise(scalar_op, name=symbolname) if getattr(symbol, "__doc__", False): rval.__doc__ = symbol.__doc__ + "\n" + rval.__doc__ # for the meaning of this see the ./epydoc script # it makes epydoc display rval as if it were a function, not an object rval.__epydoc_asRoutine = symbol rval.__module__ = "aesara.tensor.inplace" pprint.assign( rval, printing.FunctionPrinter(symbolname.replace("_inplace", "="))) return rval
//%(z)s = 0.5*(ultrafasttanh(0.5*x)+1.); %(z)s = 0.5*(%(z)s+1.); }""" % locals()) @staticmethod def c_code_cache_version(): return (5, ) ultra_fast_scalar_sigmoid = UltraFastScalarSigmoid( aes.upgrade_to_float, name="ultra_fast_scalar_sigmoid") ultra_fast_sigmoid = Elemwise(ultra_fast_scalar_sigmoid, name="ultra_fast_sigmoid") ultra_fast_sigmoid_inplace = Elemwise( UltraFastScalarSigmoid(aes.transfer_type(0)), inplace_pattern={0: 0}, name="ultra_fast_sigmoid_inplace", ) pprint.assign(ultra_fast_sigmoid, printing.FunctionPrinter(["ultra_fast_sigmoid"])) # @opt.register_uncanonicalize @local_optimizer(None) def local_ultra_fast_sigmoid(fgraph, node): """ When enabled, change all sigmoid to ultra_fast_sigmoid. For example do mode.including('local_ultra_fast_sigmoid')
"library", "tensor", "nnet", "sigmoid_prec.png", ) plt.savefig(fname) print("New picture saved at", fname) print(val_ultra.max()) print(val_ultra.min()) scalar_sigmoid = ScalarSigmoid(scalar.upgrade_to_float, name="scalar_sigmoid") sigmoid = elemwise.Elemwise(scalar_sigmoid, name="sigmoid") sigmoid_inplace = elemwise.Elemwise( ScalarSigmoid(scalar.transfer_type(0)), inplace_pattern={0: 0}, name="sigmoid_inplace", ) pprint.assign(sigmoid, printing.FunctionPrinter("sigmoid")) class UltraFastScalarSigmoid(scalar.UnaryScalarOp): """ This is just speed opt. Not for stability. """ nfunc_spec = ("scipy.special.expit", 1, 1)