def construct(symbol): symbolname = symbol.__name__ inplace = symbolname.endswith('_inplace') if inplace: msg = "inplace" else: msg = "no_inplace" n = "Elemwise{%s,%s}" % (symbolname, msg) 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=n, nfunc_spec=(nfunc and (nfunc, nin, nout))) else: scalar_op = getattr(scal, symbolname) rval = elemwise.Elemwise(scalar_op, name=n, nfunc_spec=(nfunc and (nfunc, nin, nout))) 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__ = 'tensor' pprint.assign(rval, printing.FunctionPrinter(symbolname)) return rval
def construct_elemwise(name, doc_str, scalar_op): n = "Elemwise{%s, no_inplace}" % (name, ) rval = elemwise.Elemwise(scalar_op, name=n, nfunc_spec=(None and (None, None, None))) rval.__doc__ = doc_str + '\n' + rval.__doc__ rval.__module__ = 'tensor' return rval
os.path.dirname(theano.__file__), "..", "doc", "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. """
def grad(self, (x, ), (gz, )): if x.type in complex_types: raise NotImplementedError() elif x.type in float_types: return gz * 2. / numpy.sqrt(numpy.pi) * exp(-x * x), else: return None, def c_code(self, node, name, (x, ), (z, ), sub): if node.inputs[0].type in complex_types: raise NotImplementedError('type not supported', type) return "%(z)s = erf(%(x)s);" % locals() erf = elemwise.Elemwise(Erf(upgrade_to_float, name='Terf')) class Erfc(UnaryScalarOp): def impl(self, x): return scipy.special.erfc(x) def grad(self, (x, ), (gz, )): if x.type in complex_types: raise NotImplementedError() elif x.type in float_types: return -gz * 2. / numpy.sqrt(numpy.pi) * exp(-x * x), else: return None, def c_code(self, node, name, (x, ), (z, ), sub):
elif node.inputs[0].type == scalar.float64: return """%(z)s = %(x)s < -709.0 ? 0.0 : %(x)s > 19.0 ? 1.0 : 1.0 /(1.0+exp(-%(x)s));""" % locals( ) else: raise NotImplementedError('only floatingpoint is implemented') def c_code_cache_version(self): v = super(ScalarSigmoid, self).c_code_cache_version() if v: return (2, ) + v else: return v 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 ScalarSoftplus(scalar.UnaryScalarOp): @staticmethod def static_impl(x): if x < -30.0: return 0.0
return [x > 0.0] def c_code(self, node, name, (x,), (z,), sub): """ .. todo:: WRITEME """ if node.inputs[0].type == scalar.float32: return """%(z)s = %(x)s < 0.0f ? 0.0 : %(x)s;""" % locals() elif node.inputs[0].type == scalar.float64: return """%(z)s = %(x)s < 0.0 ? 0.0 : %(x)s;""" % locals() else: raise NotImplementedError('only floatingpoint is implemented') def c_code_cache_version(self): """ .. todo:: WRITEME """ v = super(ScalarRectifier, self).c_code_cache_version() if v: return (2,) + v else: return v scalar_rectifier = ScalarRectifier(scalar.upgrade_to_float, name='scalar_rectifier') rectifier = elemwise.Elemwise(scalar_rectifier, name='rectifier')