<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> from itertools import chain <EOL> import math <EOL> import warnings <EOL> from copy import copy <EOL> from textwrap import dedent <EOL> import numpy <EOL> from six . moves import xrange <EOL> import theano <EOL> from theano . compat import imap , izip <EOL> from theano import gof , printing <EOL> from theano . gof import ( Op , utils , Variable , Constant , Type , Apply , <EOL> FunctionGraph ) <EOL> from functools import partial <EOL> from theano . configparser import config <EOL> from theano . gradient import DisconnectedType <EOL> from theano . gradient import grad_undefined <EOL> from theano . printing import pprint <EOL> import collections <EOL> builtin_complex = complex <EOL> builtin_int = int <EOL> builtin_float = float <EOL> class ComplexError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class IntegerDivisionError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def upcast ( dtype , * dtypes ) : <EOL> keep_float32 = [ ( config . cast_policy == '<STR_LIT>' and <EOL> config . floatX == '<STR_LIT>' ) ] <EOL> keep_float16 = [ ( config . cast_policy == '<STR_LIT>' and <EOL> config . floatX == '<STR_LIT>' ) ] <EOL> def make_array ( dt ) : <EOL> if dt == '<STR_LIT>' : <EOL> keep_float32 [ <NUM_LIT:0> ] = False <EOL> keep_float16 [ <NUM_LIT:0> ] = False <EOL> if dt == '<STR_LIT>' : <EOL> keep_float16 [ <NUM_LIT:0> ] = False <EOL> return numpy . zeros ( ( ) , dtype = dt ) <EOL> z = make_array ( dtype ) <EOL> for dt in dtypes : <EOL> z = z + make_array ( dt = dt ) <EOL> rval = str ( z . dtype ) <EOL> if rval == '<STR_LIT>' : <EOL> if keep_float16 [ <NUM_LIT:0> ] : <EOL> return '<STR_LIT>' <EOL> if keep_float32 [ <NUM_LIT:0> ] : <EOL> return '<STR_LIT>' <EOL> elif rval == '<STR_LIT>' : <EOL> if keep_float16 [ <NUM_LIT:0> ] : <EOL> return '<STR_LIT>' <EOL> return rval <EOL> def get_scalar_type ( dtype ) : <EOL> """<STR_LIT>""" <EOL> if dtype not in get_scalar_type . cache : <EOL> get_scalar_type . cache [ dtype ] = Scalar ( dtype = dtype ) <EOL> return get_scalar_type . cache [ dtype ] <EOL> get_scalar_type . cache = { } <EOL> def as_scalar ( x , name = None ) : <EOL> from . . tensor import TensorType , scalar_from_tensor <EOL> if isinstance ( x , gof . Apply ) : <EOL> if len ( x . outputs ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" , x ) <EOL> else : <EOL> x = x . outputs [ <NUM_LIT:0> ] <EOL> if isinstance ( x , Variable ) : <EOL> if isinstance ( x . type , Scalar ) : <EOL> return x <EOL> elif isinstance ( x . type , TensorType ) and x . ndim == <NUM_LIT:0> : <EOL> return scalar_from_tensor ( x ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" , x , x . type ) <EOL> try : <EOL> return constant ( x ) <EOL> except TypeError : <EOL> raise TypeError ( "<STR_LIT>" % x , type ( x ) ) <EOL> def constant ( x ) : <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> assert x . ndim == <NUM_LIT:0> <EOL> return ScalarConstant ( get_scalar_type ( str ( x . dtype ) ) , x ) <EOL> if isinstance ( x , builtin_float ) : <EOL> for dtype in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> x_ = theano . _asarray ( x , dtype = dtype ) <EOL> if numpy . all ( x == x_ ) : <EOL> break <EOL> x_ = None <EOL> assert x_ is not None <EOL> return ScalarConstant ( get_scalar_type ( str ( x_ . dtype ) ) , x ) <EOL> if isinstance ( x , builtin_int ) : <EOL> for dtype in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> x_ = theano . _asarray ( x , dtype = dtype ) <EOL> if numpy . all ( x == x_ ) : <EOL> break <EOL> x_ = None <EOL> assert x_ is not None <EOL> return ScalarConstant ( get_scalar_type ( str ( x_ . dtype ) ) , x ) <EOL> if isinstance ( x , builtin_complex ) : <EOL> raise NotImplementedError ( ) <EOL> raise TypeError ( x ) <EOL> class Scalar ( Type ) : <EOL> """<STR_LIT>""" <EOL> ndim = <NUM_LIT:0> <EOL> def __init__ ( self , dtype ) : <EOL> if dtype == '<STR_LIT>' : <EOL> dtype = config . floatX <EOL> self . dtype = dtype <EOL> self . dtype_specs ( ) <EOL> @ staticmethod <EOL> def may_share_memory ( a , b ) : <EOL> return False <EOL> def filter ( self , data , strict = False , allow_downcast = None ) : <EOL> py_type = self . dtype_specs ( ) [ <NUM_LIT:0> ] <EOL> if strict and not isinstance ( data , py_type ) : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self , py_type , data , type ( data ) ) , data ) <EOL> try : <EOL> converted_data = py_type ( data ) <EOL> if ( allow_downcast or <EOL> ( allow_downcast is None and <EOL> type ( data ) is float and <EOL> self . dtype == theano . config . floatX ) or <EOL> data == converted_data ) : <EOL> return py_type ( data ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> self . dtype ) <EOL> except Exception as e : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> type ( data ) , data , self . dtype ) , e ) <EOL> def values_eq_approx ( self , a , b , tolerance = <NUM_LIT> ) : <EOL> diff = a - b <EOL> if diff == <NUM_LIT:0> : <EOL> return True <EOL> return abs ( diff ) <= ( abs ( a ) * tolerance ) + ( abs ( b ) * tolerance ) <EOL> def c_headers ( self , c_compiler ) : <EOL> l = [ '<STR_LIT>' ] <EOL> l . append ( '<STR_LIT>' ) <EOL> l . append ( '<STR_LIT>' ) <EOL> if config . lib . amdlibm and c_compiler . supports_amdlibm : <EOL> l += [ '<STR_LIT>' ] <EOL> return l <EOL> def c_libraries ( self , c_compiler ) : <EOL> l = [ ] <EOL> if config . lib . amdlibm and c_compiler . supports_amdlibm : <EOL> l += [ '<STR_LIT>' ] <EOL> return l <EOL> def c_compile_args ( self , c_compiler ) : <EOL> if config . lib . amdlibm and c_compiler . supports_amdlibm : <EOL> return [ '<STR_LIT>' ] <EOL> else : <EOL> return [ ] <EOL> def __eq__ ( self , other ) : <EOL> return type ( self ) == type ( other ) and other . dtype == self . dtype <EOL> def __hash__ ( self ) : <EOL> return hash ( '<STR_LIT>' ) ^ hash ( self . dtype ) <EOL> def dtype_specs ( self ) : <EOL> try : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : ( numpy . float16 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . float32 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . float64 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . complex128 , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . complex64 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . uint8 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . int8 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . uint16 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . int16 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . uint32 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . int32 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . uint64 , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( numpy . int64 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> } [ self . dtype ] <EOL> except KeyError : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , self . dtype ) ) <EOL> def upcast ( self , * others ) : <EOL> return upcast ( * [ x . dtype for x in [ self ] + list ( others ) ] ) <EOL> def make_variable ( self , name = None ) : <EOL> return ScalarVariable ( self , name = name ) <EOL> def __str__ ( self ) : <EOL> return str ( self . dtype ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . dtype <EOL> def c_literal ( self , data ) : <EOL> if '<STR_LIT>' in self . dtype : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> return str ( data ) <EOL> def c_declare ( self , name , sub , check_input = True ) : <EOL> if ( check_input ) : <EOL> pre = """<STR_LIT>""" % dict ( name = name , dtype = self . dtype_specs ( ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> pre = "<STR_LIT>" <EOL> return pre + """<STR_LIT>""" % dict ( name = name , dtype = self . dtype_specs ( ) [ <NUM_LIT:1> ] ) <EOL> def c_init ( self , name , sub ) : <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_extract ( self , name , sub , check_input = True ) : <EOL> specs = self . dtype_specs ( ) <EOL> if ( check_input ) : <EOL> pre = """<STR_LIT>""" % dict ( sub , <EOL> name = name , <EOL> dtype = specs [ <NUM_LIT:1> ] , <EOL> pyarr_type = '<STR_LIT>' % specs [ <NUM_LIT:2> ] ) <EOL> else : <EOL> pre = "<STR_LIT>" <EOL> return pre + """<STR_LIT>""" % dict ( sub , name = name ) <EOL> def c_sync ( self , name , sub ) : <EOL> specs = self . dtype_specs ( ) <EOL> return """<STR_LIT>""" % dict ( sub , <EOL> name = name , <EOL> dtype = specs [ <NUM_LIT:1> ] , <EOL> cls = specs [ <NUM_LIT:2> ] ) <EOL> def c_cleanup ( self , name , sub ) : <EOL> return "<STR_LIT>" <EOL> def c_support_code ( self ) : <EOL> if self . dtype . startswith ( '<STR_LIT>' ) : <EOL> cplx_types = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> real_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> if ( numpy . dtype ( '<STR_LIT>' ) . num not in <EOL> [ numpy . dtype ( d [ <NUM_LIT:4> : ] ) . num for d in real_types ] ) : <EOL> real_types . append ( '<STR_LIT:int>' ) <EOL> template = """<STR_LIT>""" <EOL> def operator_eq_real ( mytype , othertype ) : <EOL> return '''<STR_LIT>''' % dict ( mytype = mytype , othertype = othertype ) <EOL> def operator_eq_cplx ( mytype , othertype ) : <EOL> return '''<STR_LIT>''' % dict ( mytype = mytype , othertype = othertype ) <EOL> operator_eq = ( '<STR_LIT>' . join ( operator_eq_real ( ctype , rtype ) <EOL> for ctype in cplx_types <EOL> for rtype in real_types ) + <EOL> '<STR_LIT>' . join ( operator_eq_cplx ( ctype1 , ctype2 ) <EOL> for ctype1 in cplx_types <EOL> for ctype2 in cplx_types ) ) <EOL> def operator_plus_real ( mytype , othertype ) : <EOL> return '''<STR_LIT>''' % dict ( mytype = mytype , othertype = othertype ) <EOL> operator_plus = '<STR_LIT>' . join ( operator_plus_real ( ctype , rtype ) <EOL> for ctype in cplx_types <EOL> for rtype in real_types ) <EOL> def operator_minus_real ( mytype , othertype ) : <EOL> return '''<STR_LIT>''' % dict ( mytype = mytype , othertype = othertype ) <EOL> operator_minus = '<STR_LIT>' . join ( operator_minus_real ( ctype , rtype ) <EOL> for ctype in cplx_types <EOL> for rtype in real_types ) <EOL> def operator_mul_real ( mytype , othertype ) : <EOL> return '''<STR_LIT>''' % dict ( mytype = mytype , othertype = othertype ) <EOL> operator_mul = '<STR_LIT>' . join ( operator_mul_real ( ctype , rtype ) <EOL> for ctype in cplx_types <EOL> for rtype in real_types ) <EOL> return ( template % dict ( nbits = <NUM_LIT:64> , half_nbits = <NUM_LIT:32> ) + <EOL> template % dict ( nbits = <NUM_LIT> , half_nbits = <NUM_LIT:64> ) + <EOL> operator_eq + <EOL> operator_plus + <EOL> operator_minus + <EOL> operator_mul ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def c_init_code ( self ) : <EOL> return [ "<STR_LIT>" ] <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT> , numpy . __version__ ) <EOL> def get_shape_info ( self , obj ) : <EOL> return obj . itemsize <EOL> def get_size ( self , shape_info ) : <EOL> return shape_info <EOL> theano . compile . register_view_op_c_code ( <EOL> Scalar , <EOL> """<STR_LIT>""" , <EOL> <NUM_LIT:1> ) <EOL> int8 = get_scalar_type ( '<STR_LIT>' ) <EOL> int16 = get_scalar_type ( '<STR_LIT>' ) <EOL> int32 = get_scalar_type ( '<STR_LIT>' ) <EOL> int64 = get_scalar_type ( '<STR_LIT>' ) <EOL> uint8 = get_scalar_type ( '<STR_LIT>' ) <EOL> uint16 = get_scalar_type ( '<STR_LIT>' ) <EOL> uint32 = get_scalar_type ( '<STR_LIT>' ) <EOL> uint64 = get_scalar_type ( '<STR_LIT>' ) <EOL> float16 = get_scalar_type ( '<STR_LIT>' ) <EOL> float32 = get_scalar_type ( '<STR_LIT>' ) <EOL> float64 = get_scalar_type ( '<STR_LIT>' ) <EOL> complex64 = get_scalar_type ( '<STR_LIT>' ) <EOL> complex128 = get_scalar_type ( '<STR_LIT>' ) <EOL> int_types = int8 , int16 , int32 , int64 <EOL> uint_types = uint8 , uint16 , uint32 , uint64 <EOL> float_types = float16 , float32 , float64 <EOL> complex_types = complex64 , complex128 <EOL> discrete_types = int_types + uint_types <EOL> continuous_types = float_types + complex_types <EOL> all_types = discrete_types + continuous_types <EOL> class _scalar_py_operators : <EOL> ndim = <NUM_LIT:0> <EOL> dtype = property ( lambda self : self . type . dtype ) <EOL> """<STR_LIT>""" <EOL> def __abs__ ( self ) : <EOL> return abs_ ( self ) <EOL> def __neg__ ( self ) : <EOL> return neg ( self ) <EOL> def __invert__ ( self ) : <EOL> return invert ( self ) <EOL> def __and__ ( self , other ) : <EOL> return and_ ( self , other ) <EOL> def __or__ ( self , other ) : <EOL> return or_ ( self , other ) <EOL> def __xor__ ( self , other ) : <EOL> return xor ( self , other ) <EOL> def __rand__ ( self , other ) : <EOL> return and_ ( other , self ) <EOL> def __ror__ ( self , other ) : <EOL> return or_ ( other , self ) <EOL> def __rxor__ ( self , other ) : <EOL> return xor ( other , self ) <EOL> def __lt__ ( self , other ) : <EOL> return lt ( self , other ) <EOL> def __le__ ( self , other ) : <EOL> return le ( self , other ) <EOL> def __gt__ ( self , other ) : <EOL> return gt ( self , other ) <EOL> def __ge__ ( self , other ) : <EOL> return ge ( self , other ) <EOL> def __add__ ( self , other ) : <EOL> return add ( self , other ) <EOL> def __sub__ ( self , other ) : <EOL> return sub ( self , other ) <EOL> def __mul__ ( self , other ) : <EOL> return mul ( self , other ) <EOL> def __truediv__ ( self , other ) : <EOL> return div_proxy ( self , other ) <EOL> def __div__ ( self , other ) : <EOL> return div_proxy ( self , other ) <EOL> def __floordiv__ ( self , other ) : <EOL> return int_div ( self , other ) <EOL> def __mod__ ( self , other ) : <EOL> return mod_check ( self , other ) <EOL> def __pow__ ( self , other ) : <EOL> return pow ( self , other ) <EOL> def __radd__ ( self , other ) : <EOL> return add ( other , self ) <EOL> def __rsub__ ( self , other ) : <EOL> return sub ( other , self ) <EOL> def __rmul__ ( self , other ) : <EOL> return mul ( other , self ) <EOL> def __rdiv__ ( self , other ) : <EOL> return div_proxy ( other , self ) <EOL> def __rmod__ ( self , other ) : <EOL> return mod ( other , self ) <EOL> def __rpow__ ( self , other ) : <EOL> return pow ( other , self ) <EOL> def zeros_like ( self , dtype = None ) : <EOL> if dtype is None : <EOL> dtype = str ( self . type . dtype ) <EOL> return second ( self , ScalarConstant ( get_scalar_type ( dtype ) , <NUM_LIT:0> ) ) <EOL> def astype ( self , dtype ) : <EOL> return cast ( self , dtype ) <EOL> class ScalarVariable ( _scalar_py_operators , Variable ) : <EOL> pass <EOL> class ScalarConstant ( _scalar_py_operators , Constant ) : <EOL> pass <EOL> Scalar . Constant = ScalarConstant <EOL> def _multi ( * fns ) : <EOL> def f2 ( f , names ) : <EOL> if len ( names ) == <NUM_LIT:1> : <EOL> return f ( names ) <EOL> else : <EOL> return [ f ( name ) for name in names ] <EOL> if len ( fns ) == <NUM_LIT:1> : <EOL> return partial ( f2 , fns [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return [ partial ( f2 , f ) for f in fns ] <EOL> ints = _multi ( int64 ) <EOL> floats = _multi ( float64 ) <EOL> complexs = _multi ( complex128 ) <EOL> complexs64 = _multi ( complex64 ) <EOL> complexs128 = _multi ( complex128 ) <EOL> class upcast_out ( object ) : <EOL> def __new__ ( self , * types ) : <EOL> dtype = Scalar . upcast ( * types ) <EOL> return get_scalar_type ( dtype ) , <EOL> class upgrade_to_float ( object ) : <EOL> def __new__ ( self , * types ) : <EOL> """<STR_LIT>""" <EOL> conv = { int8 : float32 , <EOL> int16 : float32 , <EOL> int32 : float64 , <EOL> int64 : float64 , <EOL> uint8 : float32 , <EOL> uint16 : float32 , <EOL> uint32 : float64 , <EOL> uint64 : float64 } <EOL> return get_scalar_type ( Scalar . upcast ( * [ conv . get ( type , type ) <EOL> for type in types ] ) ) , <EOL> class same_out ( object ) : <EOL> def __new__ ( self , type ) : <EOL> return type , <EOL> def upcast_out_no_complex ( * types ) : <EOL> if any ( [ type in complex_types for type in types ] ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return get_scalar_type ( dtype = Scalar . upcast ( * types ) ) , <EOL> def same_out_float_only ( type ) : <EOL> if type not in float_types : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return type , <EOL> class transfer_type ( gof . utils . object2 ) : <EOL> def __init__ ( self , * transfer ) : <EOL> assert all ( type ( x ) in [ int , str ] or x is None for x in transfer ) <EOL> self . transfer = transfer <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . transfer <EOL> def __call__ ( self , * types ) : <EOL> upcast = upcast_out ( * types ) <EOL> retval = [ ] <EOL> for i in self . transfer : <EOL> if i is None : <EOL> retval += [ upcast ] <EOL> elif isinstance ( i , str ) : <EOL> retval += [ i ] <EOL> else : <EOL> retval += [ types [ i ] ] <EOL> return retval <EOL> def __eq__ ( self , other ) : <EOL> return type ( self ) == type ( other ) and self . transfer == other . transfer <EOL> def __hash__ ( self ) : <EOL> return hash ( self . transfer ) <EOL> class specific_out ( gof . utils . object2 ) : <EOL> def __init__ ( self , * spec ) : <EOL> self . spec = spec <EOL> def __call__ ( self , * types ) : <EOL> return self . spec <EOL> def __eq__ ( self , other ) : <EOL> return type ( self ) == type ( other ) and self . spec == other . spec <EOL> def __hash__ ( self ) : <EOL> return hash ( self . spec ) <EOL> def int_out ( * types ) : <EOL> return int64 , <EOL> def float_out ( * types ) : <EOL> return float64 , <EOL> def upgrade_to_float_no_complex ( * types ) : <EOL> """<STR_LIT>""" <EOL> for type in types : <EOL> if type in complex_types : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return upgrade_to_float ( * types ) <EOL> def same_out_nocomplex ( type ) : <EOL> if type in complex_types : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return type , <EOL> def int_out_nocomplex ( * types ) : <EOL> for type in types : <EOL> if type in complex_types : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return int64 , <EOL> def float_out_nocomplex ( * types ) : <EOL> for type in types : <EOL> if type in complex_types : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return float64 , <EOL> class unary_out_lookup ( gof . utils . object2 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type_table ) : <EOL> self . tbl = type_table <EOL> def __call__ ( self , * types ) : <EOL> if len ( types ) == <NUM_LIT:1> : <EOL> types = types [ <NUM_LIT:0> ] <EOL> try : <EOL> rval = self . tbl [ types ] <EOL> except Exception : <EOL> raise TypeError ( types ) <EOL> if isinstance ( types , ( list , tuple ) ) : <EOL> return rval <EOL> else : <EOL> return [ rval ] <EOL> def __eq__ ( self , other ) : <EOL> return type ( self ) == type ( other ) and self . tbl == other . tbl <EOL> def __hash__ ( self ) : <EOL> return hash ( type ( self ) ) <EOL> def real_out ( type ) : <EOL> if type == complex64 : <EOL> return float32 , <EOL> if type == complex128 : <EOL> return float64 , <EOL> return type , <EOL> class ScalarOp ( Op ) : <EOL> nin = - <NUM_LIT:1> <EOL> nout = <NUM_LIT:1> <EOL> def __init__ ( self , output_types_preference = None , name = None ) : <EOL> self . name = name <EOL> if output_types_preference is not None : <EOL> if not isinstance ( output_types_preference , collections . Callable ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % <EOL> ( self . __class__ , output_types_preference ) ) <EOL> self . output_types_preference = output_types_preference <EOL> def make_node ( self , * inputs ) : <EOL> if self . nin >= <NUM_LIT:0> : <EOL> if len ( inputs ) != self . nin : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( self , len ( inputs ) , str ( inputs ) , self . nin ) ) <EOL> inputs = [ as_scalar ( input ) for input in inputs ] <EOL> outputs = [ t ( ) for t in self . output_types ( [ input . type <EOL> for input in inputs ] ) ] <EOL> if len ( outputs ) != self . nout : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % ( self , "<STR_LIT:U+002CU+0020>" . join ( str ( input ) for input in inputs ) , self . nout , len ( outputs ) ) ) <EOL> return Apply ( self , inputs , outputs ) <EOL> def output_types ( self , types ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> variables = self . output_types_preference ( * types ) <EOL> if not isinstance ( variables , ( list , tuple ) ) or any ( not isinstance ( x , Type ) for x in variables ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" , self . output_types_preference , variables ) <EOL> if len ( variables ) != self . nout : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % ( self , "<STR_LIT:U+002CU+0020>" . join ( str ( type ) for type in variables ) , <EOL> self . output_types_preference , self . nout , len ( variables ) ) ) <EOL> return variables <EOL> else : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % self ) <EOL> def perform ( self , node , inputs , output_storage ) : <EOL> if self . nout == <NUM_LIT:1> : <EOL> output_storage [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = self . impl ( * inputs ) <EOL> else : <EOL> variables = utils . from_return_values ( self . impl ( * inputs ) ) <EOL> assert len ( variables ) == len ( output_storage ) <EOL> for storage , variable in zip ( output_storage , variables ) : <EOL> storage [ <NUM_LIT:0> ] = variable <EOL> def impl ( self , * inputs ) : <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def grad ( self , inputs , output_gradients ) : <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def __eq__ ( self , other ) : <EOL> test = ( type ( self ) == type ( other ) and <EOL> getattr ( self , '<STR_LIT>' , None ) == <EOL> getattr ( other , '<STR_LIT>' , None ) ) <EOL> return test <EOL> def __hash__ ( self ) : <EOL> return hash ( type ( self ) . __name__ ) ^ hash ( <EOL> getattr ( self , '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> def __str__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT:name>' ) and self . name : <EOL> return self . name <EOL> else : <EOL> param = [ ( k , v ) for k , v in self . __dict__ . items ( ) <EOL> if k not in [ "<STR_LIT:name>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ] <EOL> if param : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> "<STR_LIT:U+002CU+0020>" . join ( "<STR_LIT>" % ( k , v ) <EOL> for k , v in param ) ) <EOL> else : <EOL> return self . __class__ . __name__ <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:4> , ) <EOL> def c_code_contiguous ( self , node , name , inp , out , sub ) : <EOL> """<STR_LIT>""" <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> class UnaryScalarOp ( ScalarOp ) : <EOL> nin = <NUM_LIT:1> <EOL> amd_float32 = None <EOL> amd_float64 = None <EOL> def c_code_contiguous ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if ( not theano . config . lib . amdlibm or <EOL> node . inputs [ <NUM_LIT:0> ] . type != node . outputs [ <NUM_LIT:0> ] . type ) : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> dtype = node . inputs [ <NUM_LIT:0> ] . type . dtype_specs ( ) [ <NUM_LIT:1> ] <EOL> fct_call = self . c_code_contiguous_raw ( dtype , '<STR_LIT:n>' , '<STR_LIT:x>' , '<STR_LIT:z>' ) <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_contiguous_raw ( self , dtype , n , i , o ) : <EOL> if not config . lib . amdlibm : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> if dtype . startswith ( '<STR_LIT>' ) : <EOL> dtype = dtype [ <NUM_LIT:4> : ] <EOL> if dtype == '<STR_LIT>' and self . amd_float32 is not None : <EOL> dtype = '<STR_LIT:float>' <EOL> fct = self . amd_float32 <EOL> elif dtype == '<STR_LIT>' and self . amd_float64 is not None : <EOL> dtype = '<STR_LIT>' <EOL> fct = self . amd_float64 <EOL> else : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> class BinaryScalarOp ( ScalarOp ) : <EOL> nin = <NUM_LIT:2> <EOL> class LogicalComparison ( BinaryScalarOp ) : <EOL> def output_types ( self , * input_dtypes ) : <EOL> return [ int8 ] <EOL> def grad ( self , inputs , output_gradients ) : <EOL> x , y = inputs <EOL> out = self ( x , y ) <EOL> assert str ( out . type . dtype ) . find ( '<STR_LIT:int>' ) != - <NUM_LIT:1> <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) , <EOL> y . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> class FixedLogicalComparison ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> def output_types ( self , * input_dtypes ) : <EOL> return [ int8 ] <EOL> def grad ( self , inputs , output_gradients ) : <EOL> x , = inputs <EOL> out = self ( x ) <EOL> assert str ( out . type . dtype ) . find ( '<STR_LIT:int>' ) != - <NUM_LIT:1> <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> class LT ( LogicalComparison ) : <EOL> identity = False <EOL> commutative = False <EOL> associative = False <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return numpy . less ( x , y ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> lt = LT ( ) <EOL> class GT ( LogicalComparison ) : <EOL> identity = False <EOL> commutative = False <EOL> associative = False <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return numpy . greater ( x , y ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> gt = GT ( ) <EOL> class LE ( LogicalComparison ) : <EOL> identity = False <EOL> commutative = False <EOL> associative = False <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return numpy . less_equal ( x , y ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> le = LE ( ) <EOL> class GE ( LogicalComparison ) : <EOL> identity = False <EOL> commutative = False <EOL> associative = False <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return numpy . greater_equal ( x , y ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> ge = GE ( ) <EOL> class EQ ( LogicalComparison ) : <EOL> identity = False <EOL> commutative = True <EOL> associative = False <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x == y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> eq = EQ ( ) <EOL> class NEQ ( LogicalComparison ) : <EOL> identity = False <EOL> commutative = True <EOL> associative = False <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x != y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> neq = NEQ ( ) <EOL> class IsNan ( FixedLogicalComparison ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . isnan ( x ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> scalarop_version = super ( IsNan , self ) . c_code_cache_version ( ) <EOL> return tuple ( scalarop_version ) + ( <NUM_LIT:2> , ) <EOL> isnan = IsNan ( ) <EOL> class IsInf ( FixedLogicalComparison ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . isinf ( x ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> isinf = IsInf ( ) <EOL> class InRange ( LogicalComparison ) : <EOL> nin = <NUM_LIT:3> <EOL> def __init__ ( self , openlow , openhi ) : <EOL> self . openlow = openlow <EOL> self . openhi = openhi <EOL> def impl ( self , x , low , hi ) : <EOL> if self . openlow and x <= low : <EOL> return False <EOL> elif not self . openlow and x < low : <EOL> return False <EOL> if self . openhi and x >= hi : <EOL> return False <EOL> elif not self . openhi and x > hi : <EOL> return False <EOL> return True <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , low , hi ) = inputs <EOL> ( z , ) = outputs <EOL> if self . openlow : <EOL> cmp1 = '<STR_LIT:>>' <EOL> else : <EOL> cmp1 = '<STR_LIT>' <EOL> if self . openhi : <EOL> cmp2 = '<STR_LIT:<>' <EOL> else : <EOL> cmp2 = '<STR_LIT>' <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" % locals ( ) ) <EOL> def get_grad ( self , elem ) : <EOL> if elem . type in complex_types : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise NotImplementedError ( msg ) <EOL> elif elem . type in discrete_types : <EOL> return elem . zeros_like ( ) . astype ( theano . config . floatX ) <EOL> else : <EOL> return elem . zeros_like ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , low , hi ) = inputs <EOL> ( gz , ) = gout <EOL> grads = [ ] <EOL> for elem in [ x , low , hi ] : <EOL> grads . append ( self . get_grad ( elem ) ) <EOL> return grads <EOL> inopenrange = InRange ( True , True ) <EOL> inclosedrange = InRange ( False , False ) <EOL> class Switch ( ScalarOp ) : <EOL> nin = <NUM_LIT:3> <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> def impl ( self , cond , ift , iff ) : <EOL> if cond : <EOL> return ift <EOL> else : <EOL> return iff <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( cond , ift , iff ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( cond , ift , iff ) = inputs <EOL> ( gz , ) = gout <EOL> first_part = switch ( cond , gz , <NUM_LIT:0.> ) <EOL> second_part = switch ( cond , <NUM_LIT:0.> , gz ) <EOL> out = self ( cond , ift , iff ) <EOL> if out . type . dtype in discrete_types : <EOL> first_part = <NUM_LIT:0.> <EOL> second_part = <NUM_LIT:0.> <EOL> condition_grad = cond . zeros_like ( ) . astype ( theano . config . floatX ) <EOL> return ( condition_grad , first_part , second_part ) <EOL> def output_types ( self , types ) : <EOL> ( cond_t , ift_t , iff_t ) = types <EOL> return upcast_out ( ift_t , iff_t ) <EOL> switch = Switch ( ) <EOL> class UnaryBitOp ( UnaryScalarOp ) : <EOL> def output_types ( self , * input_types ) : <EOL> for i in input_types [ <NUM_LIT:0> ] : <EOL> if i not in ( int8 , int16 , int32 , int64 ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % i ) <EOL> return upcast_out ( * input_types [ <NUM_LIT:0> ] ) <EOL> def grad ( self , inputs , output_gradients ) : <EOL> return [ inputs [ <NUM_LIT:0> ] . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> class BinaryBitOp ( BinaryScalarOp ) : <EOL> def output_types ( self , * input_types ) : <EOL> t0 , t1 = input_types [ <NUM_LIT:0> ] <EOL> for i in input_types [ <NUM_LIT:0> ] : <EOL> if i not in ( int8 , int16 , int32 , int64 ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % i ) <EOL> return upcast_out ( * input_types [ <NUM_LIT:0> ] ) <EOL> def grad ( self , inputs , output_gradients ) : <EOL> a , b = inputs <EOL> return [ a . zeros_like ( ) . astype ( theano . config . floatX ) , <EOL> b . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> class OR ( BinaryBitOp ) : <EOL> identity = <NUM_LIT:0> <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x | y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> or_ = OR ( ) <EOL> class XOR ( BinaryBitOp ) : <EOL> identity = <NUM_LIT:0> <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x ^ y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> xor = XOR ( ) <EOL> class AND ( BinaryBitOp ) : <EOL> identity = <NUM_LIT:1> <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x & y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> and_ = AND ( ) <EOL> class Invert ( UnaryBitOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return ~ x <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> invert = Invert ( ) <EOL> class Maximum ( BinaryScalarOp ) : <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , * inputs ) : <EOL> return numpy . maximum ( * inputs ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if any ( [ i . type in complex_types for i in node . inputs ] ) : <EOL> raise NotImplementedError ( ) <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> output = self ( x , y ) <EOL> if output . type in discrete_types : <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) , <EOL> y . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> gx = eq ( output , x ) * gz <EOL> gy = eq ( output , y ) * gz <EOL> return ( gx , gy ) <EOL> maximum = Maximum ( upcast_out , name = '<STR_LIT>' ) <EOL> class Minimum ( BinaryScalarOp ) : <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , * inputs ) : <EOL> return numpy . minimum ( * inputs ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if any ( [ i . type in complex_types for i in node . inputs ] ) : <EOL> raise NotImplementedError ( ) <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> output = minimum ( x , y ) <EOL> if output . type in discrete_types : <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) , <EOL> y . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> gx = eq ( output , x ) * gz <EOL> gy = eq ( output , y ) * gz <EOL> return ( gx , gy ) <EOL> minimum = Minimum ( upcast_out , name = '<STR_LIT>' ) <EOL> class Add ( ScalarOp ) : <EOL> identity = <NUM_LIT:0> <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , * inputs ) : <EOL> return sum ( inputs ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( z , ) = outputs <EOL> if not inputs : <EOL> return z + "<STR_LIT>" <EOL> else : <EOL> return z + "<STR_LIT>" + "<STR_LIT>" . join ( inputs ) + "<STR_LIT:;>" <EOL> def grad ( self , inputs , gout ) : <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( * inputs ) . type in discrete_types : <EOL> assert gz is not None <EOL> retval = [ ] <EOL> for ii , inp in enumerate ( inputs ) : <EOL> if hasattr ( inp , '<STR_LIT>' ) : <EOL> retval . append ( <EOL> inp . zeros_like ( ) . astype ( theano . config . floatX ) ) <EOL> else : <EOL> retval . append ( grad_undefined ( self , ii , inp ) ) <EOL> else : <EOL> retval = [ ] <EOL> for i in inputs : <EOL> retval += [ gz ] <EOL> return retval <EOL> add = Add ( upcast_out , name = '<STR_LIT>' ) <EOL> class Mul ( ScalarOp ) : <EOL> identity = <NUM_LIT:1> <EOL> commutative = True <EOL> associative = True <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , * inputs ) : <EOL> return numpy . product ( inputs ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( z , ) = outputs <EOL> if not inputs : <EOL> return z + "<STR_LIT>" <EOL> else : <EOL> return z + "<STR_LIT>" + "<STR_LIT>" . join ( inputs ) + "<STR_LIT:;>" <EOL> def grad ( self , inputs , gout ) : <EOL> ( gz , ) = gout <EOL> retval = [ ] <EOL> output_type = self . output_types ( [ i . type for i in inputs ] ) [ <NUM_LIT:0> ] <EOL> if output_type in complex_types : <EOL> if gz . type not in complex_types : <EOL> raise TypeError ( <EOL> '<STR_LIT>' + str ( output_type ) + <EOL> '<STR_LIT>' + <EOL> str ( gz . type ) ) <EOL> if output_type in discrete_types : <EOL> return [ ipt . zeros_like ( ) . astype ( theano . config . floatX ) <EOL> for ipt in inputs ] <EOL> for input in inputs : <EOL> if gz . type in complex_types : <EOL> otherprod = mul ( * ( utils . difference ( inputs , [ input ] ) ) ) <EOL> yr = real ( otherprod ) <EOL> yi = imag ( otherprod ) <EOL> if input . type in complex_types : <EOL> retval += [ complex ( yr * real ( gz ) + yi * imag ( gz ) , <EOL> yr * imag ( gz ) - yi * real ( gz ) ) ] <EOL> else : <EOL> retval += [ yr * real ( gz ) + yi * imag ( gz ) ] <EOL> else : <EOL> retval += [ mul ( * ( [ gz ] + utils . difference ( inputs , <EOL> [ input ] ) ) ) ] <EOL> return retval <EOL> mul = Mul ( upcast_out , name = '<STR_LIT>' ) <EOL> class Sub ( BinaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x - y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if ( x - y ) . type in discrete_types : <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) , <EOL> y . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> first_part = gz <EOL> second_part = - gz <EOL> return first_part , second_part <EOL> sub = Sub ( upcast_out , name = '<STR_LIT>' ) <EOL> def int_or_true_div ( x_discrete , y_discrete ) : <EOL> """<STR_LIT>""" <EOL> if ( x_discrete and y_discrete ) : <EOL> if config . int_division == '<STR_LIT>' : <EOL> raise IntegerDivisionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif config . int_division == '<STR_LIT:int>' : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , <EOL> stacklevel = <NUM_LIT:4> ) <EOL> return int_div <EOL> elif config . int_division == '<STR_LIT>' : <EOL> return true_div <EOL> else : <EOL> raise NotImplementedError ( config . int_division ) <EOL> else : <EOL> return true_div <EOL> def div_proxy ( x , y ) : <EOL> """<STR_LIT>""" <EOL> f = int_or_true_div ( as_scalar ( x ) . type in discrete_types , <EOL> as_scalar ( y ) . type in discrete_types ) <EOL> return f ( x , y ) <EOL> class TrueDiv ( BinaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def output_types ( self , types ) : <EOL> if all ( t in discrete_types for t in types ) : <EOL> return [ get_scalar_type ( config . floatX ) ] <EOL> else : <EOL> return super ( TrueDiv , self ) . output_types ( types ) <EOL> def impl ( self , x , y ) : <EOL> x = numpy . asarray ( x ) <EOL> y = numpy . asarray ( y ) <EOL> if all ( a . dtype in discrete_types for a in ( x , y ) ) : <EOL> return numpy . sctypeDict [ config . floatX ] ( float ( x ) / y ) <EOL> else : <EOL> return x / y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if sum ( [ node . inputs [ <NUM_LIT:0> ] . type in complex_types , <EOL> node . inputs [ <NUM_LIT:1> ] . type in complex_types ] ) == <NUM_LIT:1> : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> if ( node . inputs [ <NUM_LIT:0> ] . type in discrete_types and <EOL> node . inputs [ <NUM_LIT:1> ] . type in discrete_types ) : <EOL> return "<STR_LIT>" % locals ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if all ( a . dtype in discrete_types for a in ( x , y ) ) : <EOL> return [ x . zeros_like ( ) , y . zeros_like ( ) ] <EOL> first_part = gz / y <EOL> if y . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> second_part = - ( gz * x ) / ( y * y ) <EOL> return first_part , second_part <EOL> true_div = TrueDiv ( upcast_out , name = '<STR_LIT>' ) <EOL> class IntDiv ( BinaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> complex_error = ComplexError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def impl ( self , x , y ) : <EOL> return x // y <EOL> def c_support_code ( self ) : <EOL> return "<STR_LIT>" <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> t = node . inputs [ <NUM_LIT:0> ] . type . upcast ( * [ i . type for i in node . inputs [ <NUM_LIT:1> : ] ] ) <EOL> if t in imap ( str , discrete_types ) : <EOL> x_div_y_pp = '<STR_LIT>' % locals ( ) <EOL> x_div_y_mp = '<STR_LIT>' % locals ( ) <EOL> x_mod_y_mp = '<STR_LIT>' % locals ( ) <EOL> x_div_y_pm = '<STR_LIT>' % locals ( ) <EOL> x_mod_y_pm = '<STR_LIT>' % locals ( ) <EOL> x_div_y_mm = '<STR_LIT>' % locals ( ) <EOL> elif t in imap ( str , float_types ) : <EOL> if t == '<STR_LIT>' : <EOL> floor = '<STR_LIT>' <EOL> fmod = '<STR_LIT>' <EOL> elif t == '<STR_LIT>' : <EOL> floor = '<STR_LIT>' <EOL> fmod = '<STR_LIT>' <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' , t ) <EOL> x_div_y_pp = '<STR_LIT>' % locals ( ) <EOL> x_div_y_mp = '<STR_LIT>' % locals ( ) <EOL> x_mod_y_mp = '<STR_LIT>' % locals ( ) <EOL> x_div_y_pm = '<STR_LIT>' % locals ( ) <EOL> x_mod_y_pm = '<STR_LIT>' % locals ( ) <EOL> x_div_y_mm = '<STR_LIT>' % locals ( ) <EOL> elif t in complex_types : <EOL> raise self . complex_error <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' , t ) <EOL> return dedent ( """<STR_LIT>""" ) % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:2> , ) <EOL> def grad ( self , inputs , g_output ) : <EOL> return [ inp . zeros_like ( dtype = theano . config . floatX ) <EOL> for inp in inputs ] <EOL> int_div = IntDiv ( upcast_out , name = '<STR_LIT>' ) <EOL> floor_div = int_div <EOL> def mod_check ( x , y ) : <EOL> if ( as_scalar ( x ) . type in complex_types or <EOL> as_scalar ( y ) . type in complex_types ) : <EOL> raise Mod . complex_error <EOL> else : <EOL> return mod ( x , y ) <EOL> class Mod ( BinaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> complex_error = ComplexError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def impl ( self , x , y ) : <EOL> if isinstance ( x , numpy . complex ) or isinstance ( y , numpy . complex ) : <EOL> raise self . complex_error <EOL> return x % y <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:5> , ) <EOL> def c_support_code ( self ) : <EOL> return "<STR_LIT>" <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> """<STR_LIT>""" <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> t = node . inputs [ <NUM_LIT:0> ] . type . upcast ( * [ i . type for i in node . inputs [ <NUM_LIT:1> : ] ] ) <EOL> if ( str ( t ) in imap ( str , discrete_types ) or <EOL> t in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] or <EOL> t in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] or <EOL> t in discrete_types ) : <EOL> assert str ( t ) in imap ( str , discrete_types ) <EOL> x_mod_y = "<STR_LIT>" % locals ( ) <EOL> x_mod_ymm = "<STR_LIT>" % locals ( ) <EOL> x_mod_ypm = "<STR_LIT>" % locals ( ) <EOL> x_mod_ymp = "<STR_LIT>" % locals ( ) <EOL> elif ( str ( t ) in imap ( str , float_types ) or <EOL> t in [ '<STR_LIT>' , '<STR_LIT>' ] or <EOL> t in float_types ) : <EOL> assert str ( t ) in imap ( str , float_types ) <EOL> x_mod_y = "<STR_LIT>" % locals ( ) <EOL> x_mod_ymm = "<STR_LIT>" % locals ( ) <EOL> x_mod_ypm = "<STR_LIT>" % locals ( ) <EOL> x_mod_ymp = "<STR_LIT>" % locals ( ) <EOL> elif str ( t ) in imap ( str , complex_types ) : <EOL> raise self . complex_error <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' , t ) <EOL> return dedent ( """<STR_LIT>""" ) % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> z = self ( x , y ) <EOL> if z . type . dtype in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) , <EOL> y . zeros_like ( dtype = theano . config . floatX ) ] <EOL> return [ gz , <EOL> - ( x // y ) * gz ] <EOL> mod = Mod ( upcast_out , name = '<STR_LIT>' ) <EOL> class Pow ( BinaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def impl ( self , x , y ) : <EOL> return x ** y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if ( node . inputs [ <NUM_LIT:0> ] . type in complex_types or <EOL> node . inputs [ <NUM_LIT:1> ] . type in complex_types ) : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x , y ) . type in discrete_types : <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) , <EOL> y . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> first_part = gz * y * x ** ( y - <NUM_LIT:1> ) <EOL> second_part = gz * log ( x ) * x ** y <EOL> second_part = switch ( eq ( x , <NUM_LIT:0> ) , <NUM_LIT:0> , second_part ) <EOL> return ( first_part , second_part ) <EOL> def c_code_contiguous ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> if not theano . config . lib . amdlibm : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> if ( node . inputs [ <NUM_LIT:0> ] . type == node . outputs [ <NUM_LIT:0> ] . type and <EOL> node . inputs [ <NUM_LIT:1> ] . type == node . outputs [ <NUM_LIT:0> ] . type and <EOL> node . inputs [ <NUM_LIT:0> ] . dtype == '<STR_LIT>' and <EOL> node . inputs [ <NUM_LIT:1> ] . dtype == '<STR_LIT>' ) : <EOL> dtype = '<STR_LIT:float>' <EOL> fct = "<STR_LIT>" <EOL> return """<STR_LIT>""" % locals ( ) <EOL> elif ( node . inputs [ <NUM_LIT:0> ] . type == node . outputs [ <NUM_LIT:0> ] . type and <EOL> node . inputs [ <NUM_LIT:1> ] . dtype == node . outputs [ <NUM_LIT:0> ] . dtype and <EOL> all ( node . inputs [ <NUM_LIT:1> ] . broadcastable ) and <EOL> node . inputs [ <NUM_LIT:0> ] . dtype == '<STR_LIT>' and <EOL> node . inputs [ <NUM_LIT:1> ] . dtype == '<STR_LIT>' ) : <EOL> dtype = '<STR_LIT:float>' <EOL> fct = "<STR_LIT>" <EOL> return """<STR_LIT>""" % locals ( ) <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> pow = Pow ( upcast_out , name = '<STR_LIT>' ) <EOL> class Clip ( ScalarOp ) : <EOL> nin = <NUM_LIT:3> <EOL> def impl ( self , x , min , max ) : <EOL> if x < min : <EOL> return min <EOL> elif x > max : <EOL> return max <EOL> else : <EOL> return x <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , min , max ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , mn , mx ) = inputs <EOL> ( gz , ) = gout <EOL> assert gz . type not in complex_types <EOL> gx = ( ( x >= mn ) & ( x <= mx ) ) * gz <EOL> gmn = ( x < mn ) * gz <EOL> gmx = ( x > mx ) * gz <EOL> out = self ( x , mn , mx ) <EOL> def handle_int ( v ) : <EOL> if out . type in int_types : <EOL> return v . zeros_like ( ) . astype ( config . floatX ) <EOL> return v <EOL> return list ( map ( handle_int , [ gx , gmn , gmx ] ) ) <EOL> clip = Clip ( upcast_out_no_complex , name = '<STR_LIT>' ) <EOL> class Second ( BinaryScalarOp ) : <EOL> def impl ( self , x , y ) : <EOL> return y <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , y ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> def connection_pattern ( self , node ) : <EOL> return [ [ False ] , [ True ] ] <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> if y . type in continuous_types : <EOL> return DisconnectedType ( ) ( ) , gz <EOL> else : <EOL> return DisconnectedType ( ) ( ) , y . zeros_like ( ) <EOL> second = Second ( transfer_type ( <NUM_LIT:1> ) , name = '<STR_LIT>' ) <EOL> class Identity ( UnaryScalarOp ) : <EOL> def impl ( self , input ) : <EOL> return input <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in continuous_types : <EOL> return gz , <EOL> else : <EOL> return x . zeros_like ( dtype = theano . config . floatX ) , <EOL> identity = Identity ( same_out , name = '<STR_LIT>' ) <EOL> class Cast ( UnaryScalarOp ) : <EOL> def __init__ ( self , o_type , name = None ) : <EOL> if not isinstance ( o_type , Scalar ) : <EOL> raise TypeError ( o_type ) <EOL> super ( Cast , self ) . __init__ ( specific_out ( o_type ) , name = name ) <EOL> self . o_type = o_type <EOL> self . ctor = getattr ( numpy , o_type . dtype ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . o_type . dtype ) <EOL> def make_new_inplace ( self , output_types_preference = None , name = None ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . o_type , name ) <EOL> def impl ( self , input ) : <EOL> return self . ctor ( input ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % ( z , node . outputs [ <NUM_LIT:0> ] . type . dtype_specs ( ) [ <NUM_LIT:1> ] , x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if self . o_type in continuous_types : <EOL> return [ gz ] <EOL> else : <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> def c_code_cache_version ( self ) : <EOL> s = super ( Cast , self ) . c_code_cache_version ( ) <EOL> if s : <EOL> return ( <NUM_LIT:3> , ) + s <EOL> else : <EOL> return s <EOL> convert_to_int8 = Cast ( int8 , name = '<STR_LIT>' ) <EOL> convert_to_int16 = Cast ( int16 , name = '<STR_LIT>' ) <EOL> convert_to_int32 = Cast ( int32 , name = '<STR_LIT>' ) <EOL> convert_to_int64 = Cast ( int64 , name = '<STR_LIT>' ) <EOL> convert_to_uint8 = Cast ( uint8 , name = '<STR_LIT>' ) <EOL> convert_to_uint16 = Cast ( uint16 , name = '<STR_LIT>' ) <EOL> convert_to_uint32 = Cast ( uint32 , name = '<STR_LIT>' ) <EOL> convert_to_uint64 = Cast ( uint64 , name = '<STR_LIT>' ) <EOL> convert_to_float16 = Cast ( float16 , name = '<STR_LIT>' ) <EOL> convert_to_float32 = Cast ( float32 , name = '<STR_LIT>' ) <EOL> convert_to_float64 = Cast ( float64 , name = '<STR_LIT>' ) <EOL> convert_to_complex64 = Cast ( complex64 , name = '<STR_LIT>' ) <EOL> convert_to_complex128 = Cast ( complex128 , name = '<STR_LIT>' ) <EOL> _cast_mapping = { <EOL> '<STR_LIT>' : convert_to_int8 , <EOL> '<STR_LIT>' : convert_to_int16 , <EOL> '<STR_LIT>' : convert_to_int32 , <EOL> '<STR_LIT>' : convert_to_int64 , <EOL> '<STR_LIT>' : convert_to_uint8 , <EOL> '<STR_LIT>' : convert_to_uint16 , <EOL> '<STR_LIT>' : convert_to_uint32 , <EOL> '<STR_LIT>' : convert_to_uint64 , <EOL> '<STR_LIT>' : convert_to_float16 , <EOL> '<STR_LIT>' : convert_to_float32 , <EOL> '<STR_LIT>' : convert_to_float64 , <EOL> '<STR_LIT>' : convert_to_complex64 , <EOL> '<STR_LIT>' : convert_to_complex128 } <EOL> def cast ( x , dtype ) : <EOL> """<STR_LIT>""" <EOL> if dtype == '<STR_LIT>' : <EOL> dtype = config . floatX <EOL> _x = as_scalar ( x ) <EOL> if _x . type . dtype == dtype : <EOL> return _x <EOL> if _x . type . dtype . startswith ( '<STR_LIT>' ) and not dtype . startswith ( '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return _cast_mapping [ dtype ] ( _x ) <EOL> class Abs ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def make_node ( self , x ) : <EOL> inputs = [ as_scalar ( input ) for input in [ x ] ] <EOL> if inputs [ <NUM_LIT:0> ] . type == complex64 : <EOL> outputs = [ float32 ( ) ] <EOL> elif inputs [ <NUM_LIT:0> ] . type == complex128 : <EOL> outputs = [ float64 ( ) ] <EOL> else : <EOL> outputs = [ t ( ) for t in self . output_types ( <EOL> [ input . type for input in inputs ] ) ] <EOL> return Apply ( self , inputs , outputs ) <EOL> def impl ( self , x ) : <EOL> return numpy . abs ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * x / abs ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> type = node . inputs [ <NUM_LIT:0> ] . type <EOL> if type in int_types : <EOL> return "<STR_LIT>" % locals ( ) <EOL> if type in float_types : <EOL> return "<STR_LIT>" % locals ( ) <EOL> if type in complex_types : <EOL> return "<STR_LIT>" % locals ( ) <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> abs_ = Abs ( same_out ) <EOL> class Sgn ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . sign ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> rval = x . zeros_like ( ) <EOL> if rval . type . dtype in discrete_types : <EOL> rval = rval . astype ( theano . config . floatX ) <EOL> return [ rval ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> type = node . inputs [ <NUM_LIT:0> ] . type <EOL> if type in float_types : <EOL> return '<STR_LIT>' % locals ( ) <EOL> if type in int_types : <EOL> return "<STR_LIT>" % locals ( ) <EOL> raise TypeError ( ) <EOL> def c_code_cache_version ( self ) : <EOL> s = super ( Sgn , self ) . c_code_cache_version ( ) <EOL> if s : <EOL> return ( <NUM_LIT:4> , ) + s <EOL> else : <EOL> return s <EOL> sgn = Sgn ( same_out_nocomplex , name = '<STR_LIT>' ) <EOL> class Ceil ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . ceil ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> rval = x . zeros_like ( ) <EOL> if rval . type . dtype in discrete_types : <EOL> rval = rval . astype ( theano . config . floatX ) <EOL> return [ rval ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> ceil = Ceil ( same_out_nocomplex , name = '<STR_LIT>' ) <EOL> class Floor ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . floor ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> rval = x . zeros_like ( ) <EOL> if rval . type . dtype in discrete_types : <EOL> rval = rval . astype ( theano . config . floatX ) <EOL> return [ rval ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> floor = Floor ( same_out_nocomplex , name = '<STR_LIT>' ) <EOL> class Trunc ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . trunc ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> return [ x . zeros_like ( ) . astype ( theano . config . floatX ) ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> trunc = Trunc ( same_out_nocomplex , name = '<STR_LIT>' ) <EOL> class RoundHalfToEven ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . round ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> rval = x . zeros_like ( ) <EOL> if rval . type . dtype in discrete_types : <EOL> rval = rval . astype ( theano . config . floatX ) <EOL> return [ rval ] <EOL> def c_code___ ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> typ = node . outputs [ <NUM_LIT:0> ] . type . dtype <EOL> if typ not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> Exception ( "<STR_LIT>" ) <EOL> return dedent ( """<STR_LIT>""" % locals ( ) ) <EOL> round_half_to_even = RoundHalfToEven ( same_out_float_only ) <EOL> def round_half_away_from_zero_ ( a ) : <EOL> if a > <NUM_LIT:0> : <EOL> return numpy . floor ( a + <NUM_LIT:0.5> ) <EOL> else : <EOL> return numpy . ceil ( a - <NUM_LIT:0.5> ) <EOL> round_half_away_from_zero_vec64 = numpy . vectorize ( <EOL> round_half_away_from_zero_ , <EOL> doc = '<STR_LIT>' ) <EOL> round_half_away_from_zero_vec32 = numpy . vectorize ( <EOL> round_half_away_from_zero_ , <EOL> doc = '<STR_LIT>' , <EOL> otypes = [ '<STR_LIT>' ] ) <EOL> def round_half_away_from_zero_vec ( a ) : <EOL> if getattr ( a , '<STR_LIT>' , None ) == numpy . float32 : <EOL> return round_half_away_from_zero_vec32 ( a ) <EOL> return round_half_away_from_zero_vec64 ( a ) <EOL> class RoundHalfAwayFromZero ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> def impl ( self , x ) : <EOL> return round_half_away_from_zero_vec ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> rval = x . zeros_like ( ) <EOL> if rval . type . dtype in discrete_types : <EOL> rval = rval . astype ( theano . config . floatX ) <EOL> return [ rval ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . outputs [ <NUM_LIT:0> ] . type . dtype in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return "<STR_LIT>" % locals ( ) <EOL> else : <EOL> Exception ( "<STR_LIT>" ) <EOL> round_half_away_from_zero = RoundHalfAwayFromZero ( same_out_float_only ) <EOL> class Neg ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> if isinstance ( x , numpy . bool_ ) : <EOL> x = numpy . int8 ( x ) <EOL> return - x <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return - gz , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> neg = Neg ( same_out , name = '<STR_LIT>' ) <EOL> pprint . assign ( add , printing . OperatorPrinter ( '<STR_LIT:+>' , - <NUM_LIT:2> , '<STR_LIT>' ) ) <EOL> pprint . assign ( mul , printing . OperatorPrinter ( '<STR_LIT:*>' , - <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> pprint . assign ( sub , printing . OperatorPrinter ( '<STR_LIT:->' , - <NUM_LIT:2> , '<STR_LIT:left>' ) ) <EOL> pprint . assign ( neg , printing . OperatorPrinter ( '<STR_LIT:->' , <NUM_LIT:0> , '<STR_LIT>' ) ) <EOL> pprint . assign ( true_div , printing . OperatorPrinter ( '<STR_LIT:/>' , - <NUM_LIT:1> , '<STR_LIT:left>' ) ) <EOL> pprint . assign ( int_div , printing . OperatorPrinter ( '<STR_LIT>' , - <NUM_LIT:1> , '<STR_LIT:left>' ) ) <EOL> pprint . assign ( pow , printing . OperatorPrinter ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT:right>' ) ) <EOL> pprint . assign ( mod , printing . OperatorPrinter ( '<STR_LIT:%>' , - <NUM_LIT:1> , '<STR_LIT:left>' ) ) <EOL> class Inv ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> def impl ( self , x ) : <EOL> return numpy . float32 ( <NUM_LIT:1.0> ) / x <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return - gz / ( x * x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> inv = Inv ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Log ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> amd_float32 = "<STR_LIT>" <EOL> amd_float64 = "<STR_LIT>" <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . log ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . log ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / x , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> log = Log ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Log2 ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> amd_float32 = "<STR_LIT>" <EOL> amd_float64 = "<STR_LIT>" <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . log2 ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . log2 ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / ( x * math . log ( <NUM_LIT> ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> log2 = Log2 ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Log10 ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> amd_float32 = "<STR_LIT>" <EOL> amd_float64 = "<STR_LIT>" <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . log10 ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . log10 ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / ( x * numpy . log ( <NUM_LIT> ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> log10 = Log10 ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Log1p ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . log1p ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . log1p ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return [ gz / ( <NUM_LIT:1> + x ) ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> log1p = Log1p ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Exp ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> amd_float32 = "<STR_LIT>" <EOL> amd_float64 = "<STR_LIT>" <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . exp ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . exp ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * exp ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> exp = Exp ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Exp2 ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . exp2 ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . exp2 ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * exp2 ( x ) * log ( numpy . cast [ x . type ] ( <NUM_LIT:2> ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> exp2 = Exp2 ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Expm1 ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . expm1 ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . expm1 ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * exp ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:5> , ) <EOL> expm1 = Expm1 ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Sqr ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return x * x <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * x * <NUM_LIT:2> , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> return "<STR_LIT>" % locals ( ) <EOL> sqr = Sqr ( same_out , name = '<STR_LIT>' ) <EOL> class Sqrt ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . sqrt ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . sqrt ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return ( gz * <NUM_LIT:0.5> ) / sqrt ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> sqrt = Sqrt ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Deg2Rad ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . deg2rad ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . deg2rad ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * numpy . asarray ( numpy . pi / <NUM_LIT> , gz . type ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> deg2rad = Deg2Rad ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Rad2Deg ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . rad2deg ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . rad2deg ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * numpy . asarray ( <NUM_LIT> / numpy . pi , gz . type ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> rad2deg = Rad2Deg ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Cos ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> amd_float32 = "<STR_LIT>" <EOL> amd_float64 = "<STR_LIT>" <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . cos ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . cos ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return - gz * sin ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> cos = Cos ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcCos ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arccos ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arccos ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return - gz / sqrt ( numpy . cast [ x . type ] ( <NUM_LIT:1> ) - sqr ( x ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arccos = ArcCos ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Sin ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> amd_float32 = "<STR_LIT>" <EOL> amd_float64 = "<STR_LIT>" <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . sin ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . sin ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * cos ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> sin = Sin ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcSin ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arcsin ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arcsin ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / sqrt ( numpy . cast [ x . type ] ( <NUM_LIT:1> ) - sqr ( x ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arcsin = ArcSin ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Tan ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . tan ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . tan ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / sqr ( cos ( x ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> tan = Tan ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcTan ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arctan ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arctan ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / ( numpy . cast [ x . type ] ( <NUM_LIT:1> ) + sqr ( x ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arctan = ArcTan ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcTan2 ( BinaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , y , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> y_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if y_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arctan2 ( y , x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arctan2 ( y , x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( y , x ) = inputs <EOL> ( gz , ) = gout <EOL> if gz . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> else : <EOL> if self ( x , y ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> gx = x . zeros_like ( dtype = theano . config . floatX ) <EOL> else : <EOL> gx = x . zeros_like ( ) <EOL> if y . type in discrete_types : <EOL> gy = y . zeros_like ( dtype = theano . config . floatX ) <EOL> else : <EOL> gy = y . zeros_like ( ) <EOL> return [ gx , gy ] <EOL> return [ gz * x / ( sqr ( x ) + sqr ( y ) ) , <EOL> gz * neg ( y ) / ( sqr ( x ) + sqr ( y ) ) ] <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( y , x ) = inputs <EOL> ( z , ) = outputs <EOL> if ( node . inputs [ <NUM_LIT:0> ] . type in complex_types or <EOL> node . inputs [ <NUM_LIT:1> ] . type in complex_types ) : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arctan2 = ArcTan2 ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Cosh ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . cosh ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . cosh ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * sinh ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> cosh = Cosh ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcCosh ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arccosh ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arccosh ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / sqrt ( sqr ( x ) - numpy . cast [ x . type ] ( <NUM_LIT:1> ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arccosh = ArcCosh ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Sinh ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . sinh ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . sinh ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * cosh ( x ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> sinh = Sinh ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcSinh ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arcsinh ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arcsinh ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / sqrt ( sqr ( x ) + numpy . cast [ x . type ] ( <NUM_LIT:1> ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arcsinh = ArcSinh ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Tanh ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . tanh ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . tanh ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz * ( <NUM_LIT:1> - sqr ( tanh ( x ) ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> tanh = Tanh ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class ArcTanh ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> x_dtype = str ( getattr ( x , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if x_dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return numpy . arctanh ( x , sig = '<STR_LIT:f>' ) <EOL> return numpy . arctanh ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> raise NotImplementedError ( ) <EOL> if self ( x ) . type in discrete_types : <EOL> if x . type in discrete_types : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> else : <EOL> return [ x . zeros_like ( ) ] <EOL> return gz / ( numpy . cast [ x . type ] ( <NUM_LIT:1> ) - sqr ( x ) ) , <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( x , ) = inputs <EOL> ( z , ) = outputs <EOL> if node . inputs [ <NUM_LIT:0> ] . type in complex_types : <EOL> raise NotImplementedError ( '<STR_LIT>' , type ) <EOL> return "<STR_LIT>" % locals ( ) <EOL> arctanh = ArcTanh ( upgrade_to_float , name = '<STR_LIT>' ) <EOL> class Real ( UnaryScalarOp ) : <EOL> """<STR_LIT>""" <EOL> def impl ( self , x ) : <EOL> return numpy . real ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> return [ complex ( gz , <NUM_LIT:0> ) ] <EOL> real = Real ( real_out , name = '<STR_LIT>' ) <EOL> class Imag ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . imag ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , ) = inputs <EOL> ( gz , ) = gout <EOL> if x . type in complex_types : <EOL> return [ complex ( <NUM_LIT:0> , gz ) ] <EOL> elif x . type in float_types : <EOL> return [ second ( x , <NUM_LIT:0> ) ] <EOL> else : <EOL> return [ x . zeros_like ( dtype = theano . config . floatX ) ] <EOL> imag = Imag ( real_out , name = '<STR_LIT>' ) <EOL> class Angle ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . angle ( x ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( c , ) = inputs <EOL> ( gtheta , ) = gout <EOL> x = real ( c ) <EOL> y = imag ( c ) <EOL> r = abs ( c ) <EOL> gr = - gtheta * y / ( r ** <NUM_LIT:2> * sqrt ( <NUM_LIT:1> - ( y / r ) ** <NUM_LIT:2> ) ) <EOL> gx = gr * x / r <EOL> gy = gr * y / r <EOL> if c in complex_types : <EOL> return [ cast ( complex ( gx , gy ) , x . type . dtype ) ] <EOL> elif c in float_types : <EOL> return [ cast ( second ( x , <NUM_LIT:0> ) , x . type . dtype ) ] <EOL> else : <EOL> return [ c . zeros_like ( dtype = theano . config . floatX ) ] <EOL> angle = Angle ( specific_out ( float64 ) , name = '<STR_LIT>' ) <EOL> class Complex ( BinaryScalarOp ) : <EOL> @ staticmethod <EOL> def output_types_preference ( x , y ) : <EOL> if x in complex_types : <EOL> raise TypeError ( x ) <EOL> if y in complex_types : <EOL> raise TypeError ( y ) <EOL> up = Scalar . upcast ( x , y ) <EOL> if up in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ complex128 ] <EOL> else : <EOL> return [ complex64 ] <EOL> def impl ( self , x , y ) : <EOL> return numpy . complex ( x , y ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( x , y ) = inputs <EOL> ( gz , ) = gout <EOL> return [ cast ( real ( gz ) , x . type . dtype ) , <EOL> cast ( imag ( gz ) , y . type . dtype ) ] <EOL> complex = Complex ( name = '<STR_LIT>' ) <EOL> class Conj ( UnaryScalarOp ) : <EOL> nfunc_spec = ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def impl ( self , x ) : <EOL> return numpy . conj ( x ) <EOL> conj = Conj ( same_out , name = '<STR_LIT>' ) <EOL> class ComplexFromPolar ( BinaryScalarOp ) : <EOL> @ staticmethod <EOL> def output_types_preference ( x , y ) : <EOL> return Complex . output_types_preference ( x , y ) <EOL> def impl ( self , r , theta ) : <EOL> if r < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' , r ) <EOL> x = r * numpy . cos ( theta ) <EOL> y = r * numpy . sin ( theta ) <EOL> if x . dtype == '<STR_LIT>' : <EOL> return numpy . complex64 ( numpy . complex ( x , y ) ) <EOL> else : <EOL> return numpy . complex128 ( numpy . complex ( x , y ) ) <EOL> def grad ( self , inputs , gout ) : <EOL> ( r , theta ) = inputs <EOL> ( gz , ) = gout <EOL> gr = gz * complex_from_polar ( <NUM_LIT:1> , theta ) <EOL> gtheta = gz * complex_from_polar ( r , - theta ) <EOL> return [ gr , gtheta ] <EOL> complex_from_polar = ComplexFromPolar ( name = '<STR_LIT>' ) <EOL> class Composite ( ScalarOp ) : <EOL> """<STR_LIT>""" <EOL> init_param = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def make_new_inplace ( self , output_types_preference = None , name = None ) : <EOL> """<STR_LIT>""" <EOL> d = dict ( [ ( k , getattr ( self , k ) ) for k in self . init_param ] ) <EOL> out = self . __class__ ( ** d ) <EOL> if name : <EOL> out . name = name <EOL> else : <EOL> name = out . name <EOL> super ( Composite , out ) . __init__ ( output_types_preference , name ) <EOL> return out <EOL> def init_c_code ( self ) : <EOL> """<STR_LIT>""" <EOL> subd = dict ( chain ( <EOL> ( ( e , "<STR_LIT>" % i ) for i , e in enumerate ( self . fgraph . inputs ) ) , <EOL> ( ( e , "<STR_LIT>" % i ) for i , e in enumerate ( self . fgraph . outputs ) ) ) ) <EOL> for var in self . fgraph . variables : <EOL> if var . owner is None : <EOL> if var not in self . fgraph . inputs : <EOL> if isinstance ( var , Constant ) : <EOL> subd [ var ] = var . type . c_literal ( var . data ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif ( any ( i . dtype == '<STR_LIT>' for i in var . owner . inputs ) or <EOL> any ( o . dtype == '<STR_LIT>' for o in var . owner . outputs ) ) : <EOL> self . inner_float16 = True <EOL> _c_code = "<STR_LIT>" <EOL> self . nodenames = [ "<STR_LIT>" + ( '<STR_LIT>' % j ) <EOL> for j , n in enumerate ( self . fgraph . toposort ( ) ) ] <EOL> i = <NUM_LIT:0> <EOL> for j , node in enumerate ( self . fgraph . toposort ( ) ) : <EOL> for output in node . outputs : <EOL> if output not in subd : <EOL> i += <NUM_LIT:1> <EOL> name = "<STR_LIT>" % i <EOL> subd [ output ] = name <EOL> _c_code += "<STR_LIT>" % ( <EOL> output . type . dtype_specs ( ) [ <NUM_LIT:1> ] , name ) <EOL> s = node . op . c_code ( <EOL> node , <EOL> self . nodenames [ j ] , <EOL> [ subd [ input ] for input in node . inputs ] , <EOL> [ subd [ output ] for output in node . outputs ] , <EOL> dict ( fail = "<STR_LIT>" , id = "<STR_LIT>" % j ) ) <EOL> _c_code += s <EOL> _c_code += "<STR_LIT:\n>" <EOL> _c_code += "<STR_LIT>" <EOL> self . _c_code = _c_code <EOL> def init_py_impls ( self ) : <EOL> """<STR_LIT>""" <EOL> def compose_impl ( r ) : <EOL> if r in self . fgraph . inputs : <EOL> idx = self . fgraph . inputs . index ( r ) <EOL> return lambda inputs : inputs [ idx ] <EOL> elif r . owner is None : <EOL> return lambda inputs : r . data <EOL> node = r . owner <EOL> producers = [ compose_impl ( input ) for input in node . inputs ] <EOL> def f ( inputs ) : <EOL> return node . op . impl ( * [ p ( inputs ) for p in producers ] ) <EOL> return f <EOL> self . _impls = [ compose_impl ( r ) for r in self . fgraph . outputs ] <EOL> def init_name ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rval = self . name <EOL> except AttributeError : <EOL> if <NUM_LIT:0> : <EOL> l = [ ] <EOL> for n in self . fgraph . toposort ( ) : <EOL> if hasattr ( n . op , "<STR_LIT:name>" ) and n . op . name is not None : <EOL> v = n . op . name <EOL> if v . startswith ( "<STR_LIT>" ) : <EOL> v = v [ len ( "<STR_LIT>" ) : ] <EOL> else : <EOL> v = n . op . __class__ . __name__ <EOL> l . append ( v ) <EOL> rval = "<STR_LIT>" + "<STR_LIT:U+002C>" . join ( l ) + "<STR_LIT:}>" <EOL> else : <EOL> for i , r in enumerate ( self . fgraph . inputs ) : <EOL> r . name = '<STR_LIT>' % i <EOL> for i , r in enumerate ( self . fgraph . outputs ) : <EOL> r . name = '<STR_LIT>' % i <EOL> io = set ( self . fgraph . inputs + self . fgraph . outputs ) <EOL> for i , r in enumerate ( self . fgraph . variables ) : <EOL> if r not in io and len ( r . clients ) > <NUM_LIT:1> : <EOL> r . name = '<STR_LIT>' % i <EOL> rval = "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( [ pprint ( output ) for output <EOL> in self . fgraph . outputs ] ) <EOL> self . name = rval <EOL> def init_fgraph ( self ) : <EOL> fgraph = FunctionGraph ( self . inputs , self . outputs ) <EOL> gof . MergeOptimizer ( ) . optimize ( fgraph ) <EOL> for node in fgraph . apply_nodes : <EOL> if not isinstance ( node . op , ScalarOp ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . fgraph = fgraph <EOL> def __init__ ( self , inputs , outputs ) : <EOL> for i in inputs : <EOL> assert i not in outputs <EOL> if len ( outputs ) > <NUM_LIT:1> or not any ( [ isinstance ( var . owner . op , Composite ) <EOL> for var in outputs ] ) : <EOL> inputs , outputs = gof . graph . clone ( inputs , outputs ) <EOL> else : <EOL> assert len ( outputs ) == <NUM_LIT:1> <EOL> res = theano . compile . rebuild_collect_shared ( <EOL> inputs = inputs , <EOL> outputs = outputs [ <NUM_LIT:0> ] . owner . inputs , <EOL> copy_inputs_over = False ) <EOL> res2 = theano . compile . rebuild_collect_shared ( <EOL> inputs = outputs [ <NUM_LIT:0> ] . owner . op . inputs , <EOL> outputs = outputs [ <NUM_LIT:0> ] . owner . op . outputs , <EOL> replace = dict ( izip ( outputs [ <NUM_LIT:0> ] . owner . op . inputs , res [ <NUM_LIT:1> ] ) ) <EOL> ) <EOL> assert len ( res2 [ <NUM_LIT:1> ] ) == len ( outputs ) <EOL> assert len ( res [ <NUM_LIT:0> ] ) == len ( inputs ) <EOL> assert res [ <NUM_LIT:0> ] != inputs <EOL> inputs , outputs = res [ <NUM_LIT:0> ] , res2 [ <NUM_LIT:1> ] <EOL> self . inputs = copy ( inputs ) <EOL> self . outputs = copy ( outputs ) <EOL> self . inputs_type = tuple ( [ input . type for input in inputs ] ) <EOL> self . outputs_type = tuple ( [ output . type for output in outputs ] ) <EOL> self . nin = len ( inputs ) <EOL> self . nout = len ( outputs ) <EOL> self . init_fgraph ( ) <EOL> self . init_name ( ) <EOL> self . init_c_code ( ) <EOL> self . init_py_impls ( ) <EOL> def output_types ( self , input_types ) : <EOL> if tuple ( input_types ) != self . inputs_type : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % ( self . inputs_type , tuple ( input_types ) ) ) <EOL> return self . outputs_type <EOL> def make_node ( self , * inputs ) : <EOL> if ( tuple ( [ i . type for i in self . inputs ] ) == <EOL> tuple ( [ i . type for i in inputs ] ) ) : <EOL> return super ( Composite , self ) . make_node ( * inputs ) <EOL> else : <EOL> assert len ( inputs ) == self . nin <EOL> res = theano . compile . rebuild_collect_shared ( <EOL> self . outputs , <EOL> replace = dict ( izip ( self . inputs , inputs ) ) , <EOL> rebuild_strict = False ) <EOL> cloned_inputs = [ res [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] [ i ] for i in inputs ] <EOL> node = Composite ( cloned_inputs , res [ <NUM_LIT:1> ] ) . make_node ( * inputs ) <EOL> return node <EOL> def perform ( self , node , inputs , output_storage ) : <EOL> for storage , impl in zip ( output_storage , self . _impls ) : <EOL> storage [ <NUM_LIT:0> ] = impl ( inputs ) <EOL> def impl ( self , * inputs ) : <EOL> output_storage = [ [ None ] for i in xrange ( self . nout ) ] <EOL> self . perform ( None , inputs , output_storage ) <EOL> ret = utils . to_return_values ( [ storage [ <NUM_LIT:0> ] for storage in <EOL> output_storage ] ) <EOL> if self . nout > <NUM_LIT:1> : <EOL> ret = tuple ( ret ) <EOL> return ret <EOL> def grad ( self , inputs , output_grads ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def c_code ( self , node , nodename , inames , onames , sub ) : <EOL> d = dict ( chain ( izip ( ( "<STR_LIT>" % i for i in xrange ( len ( inames ) ) ) , inames ) , <EOL> izip ( ( "<STR_LIT>" % i for i in xrange ( len ( onames ) ) ) , <EOL> onames ) ) , ** sub ) <EOL> d [ '<STR_LIT>' ] = nodename <EOL> if '<STR_LIT:id>' not in sub : <EOL> d [ '<STR_LIT:id>' ] = '<STR_LIT>' <EOL> return self . _c_code % d <EOL> def c_code_cache_version ( self ) : <EOL> rval = [ <NUM_LIT:3> ] <EOL> for x in self . fgraph . toposort ( ) : <EOL> xv = x . op . c_code_cache_version ( ) <EOL> if xv : <EOL> rval . append ( xv ) <EOL> else : <EOL> return ( ) <EOL> return tuple ( rval ) <EOL> def c_support_code ( self ) : <EOL> rval = [ ] <EOL> for subnode in self . fgraph . toposort ( ) : <EOL> try : <EOL> rval . append ( subnode . op . c_support_code ( ) . strip ( ) ) <EOL> except gof . utils . MethodNotDefined : <EOL> pass <EOL> return "<STR_LIT:\n>" . join ( sorted ( set ( rval ) ) ) <EOL> def c_support_code_apply ( self , node , name ) : <EOL> rval = [ ] <EOL> for subnode , subnodename in zip ( self . fgraph . toposort ( ) , self . nodenames ) : <EOL> try : <EOL> subnode_support_code = subnode . op . c_support_code_apply ( <EOL> subnode , <EOL> subnodename % dict ( nodename = name ) ) <EOL> if subnode_support_code : <EOL> rval . append ( subnode_support_code ) <EOL> except gof . utils . MethodNotDefined : <EOL> pass <EOL> return "<STR_LIT:\n>" . join ( rval ) <EOL> def __eq__ ( self , other ) : <EOL> if self is other : <EOL> return True <EOL> if ( type ( self ) != type ( other ) or <EOL> self . nin != other . nin or <EOL> self . nout != other . nout ) : <EOL> return False <EOL> return ( self . _c_code == other . _c_code ) <EOL> def __hash__ ( self ) : <EOL> rval = hash ( ( type ( self ) , <EOL> self . nin , <EOL> self . nout , <EOL> self . _c_code ) ) <EOL> return rval <EOL> def __getstate__ ( self ) : <EOL> rval = dict ( self . __dict__ ) <EOL> del rval [ '<STR_LIT>' ] <EOL> del rval [ '<STR_LIT>' ] <EOL> return rval <EOL> def __setstate__ ( self , d ) : <EOL> self . __dict__ . update ( d ) <EOL> self . init_fgraph ( ) <EOL> self . init_py_impls ( ) <EOL> assert self . _c_code </s>
