<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> import warnings <EOL> import numpy <EOL> from six import integer_types <EOL> from six . moves import xrange <EOL> import six . moves . builtins as builtins <EOL> import theano <EOL> from theano import gof , Op , tensor , Variable , Apply <EOL> def max_pool_2d_same_size ( input , patch_size ) : <EOL> """<STR_LIT>""" <EOL> output = Pool ( patch_size , True ) ( input ) <EOL> outs = MaxPoolGrad ( patch_size , True ) ( input , output , output ) <EOL> return outs <EOL> def pool_2d ( input , ds , ignore_border = None , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if input . ndim < <NUM_LIT:2> : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> if ignore_border is None : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> ignore_border = False <EOL> if input . ndim == <NUM_LIT:4> : <EOL> op = Pool ( ds , ignore_border , st = st , padding = padding , <EOL> mode = mode ) <EOL> output = op ( input ) <EOL> return output <EOL> img_shape = input . shape [ - <NUM_LIT:2> : ] <EOL> batch_size = tensor . prod ( input . shape [ : - <NUM_LIT:2> ] ) <EOL> batch_size = tensor . shape_padright ( batch_size , <NUM_LIT:1> ) <EOL> new_shape = tensor . cast ( tensor . join ( <NUM_LIT:0> , batch_size , <EOL> tensor . as_tensor ( [ <NUM_LIT:1> ] ) , <EOL> img_shape ) , '<STR_LIT>' ) <EOL> input_4D = tensor . reshape ( input , new_shape , ndim = <NUM_LIT:4> ) <EOL> op = Pool ( ds , ignore_border , st = st , padding = padding , <EOL> mode = mode ) <EOL> output = op ( input_4D ) <EOL> outshp = tensor . join ( <NUM_LIT:0> , input . shape [ : - <NUM_LIT:2> ] , output . shape [ - <NUM_LIT:2> : ] ) <EOL> return tensor . reshape ( output , outshp , ndim = input . ndim ) <EOL> class Pool ( Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def out_shape ( imgshape , ds , ignore_border = False , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> if len ( imgshape ) < <NUM_LIT:2> : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if st is None : <EOL> st = ds <EOL> r , c = imgshape [ - <NUM_LIT:2> : ] <EOL> r += padding [ <NUM_LIT:0> ] * <NUM_LIT:2> <EOL> c += padding [ <NUM_LIT:1> ] * <NUM_LIT:2> <EOL> if ignore_border : <EOL> if ds [ <NUM_LIT:0> ] == st [ <NUM_LIT:0> ] : <EOL> nr = r // st [ <NUM_LIT:0> ] <EOL> else : <EOL> out_r = ( r - ds [ <NUM_LIT:0> ] ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> if isinstance ( r , theano . Variable ) : <EOL> nr = tensor . maximum ( out_r , <NUM_LIT:0> ) <EOL> else : <EOL> nr = numpy . maximum ( out_r , <NUM_LIT:0> ) <EOL> if ds [ <NUM_LIT:1> ] == st [ <NUM_LIT:1> ] : <EOL> nc = c // st [ <NUM_LIT:1> ] <EOL> else : <EOL> out_c = ( c - ds [ <NUM_LIT:1> ] ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> if isinstance ( c , theano . Variable ) : <EOL> nc = tensor . maximum ( out_c , <NUM_LIT:0> ) <EOL> else : <EOL> nc = numpy . maximum ( out_c , <NUM_LIT:0> ) <EOL> else : <EOL> if isinstance ( r , theano . Variable ) : <EOL> nr = tensor . switch ( tensor . ge ( st [ <NUM_LIT:0> ] , ds [ <NUM_LIT:0> ] ) , <EOL> ( r - <NUM_LIT:1> ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> , <EOL> tensor . maximum ( <NUM_LIT:0> , ( r - <NUM_LIT:1> - ds [ <NUM_LIT:0> ] ) // <EOL> st [ <NUM_LIT:0> ] + <NUM_LIT:1> ) + <NUM_LIT:1> ) <EOL> elif st [ <NUM_LIT:0> ] >= ds [ <NUM_LIT:0> ] : <EOL> nr = ( r - <NUM_LIT:1> ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> else : <EOL> nr = max ( <NUM_LIT:0> , ( r - <NUM_LIT:1> - ds [ <NUM_LIT:0> ] ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> if isinstance ( c , theano . Variable ) : <EOL> nc = tensor . switch ( tensor . ge ( st [ <NUM_LIT:1> ] , ds [ <NUM_LIT:1> ] ) , <EOL> ( c - <NUM_LIT:1> ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> , <EOL> tensor . maximum ( <NUM_LIT:0> , ( c - <NUM_LIT:1> - ds [ <NUM_LIT:1> ] ) // <EOL> st [ <NUM_LIT:1> ] + <NUM_LIT:1> ) + <NUM_LIT:1> ) <EOL> elif st [ <NUM_LIT:1> ] >= ds [ <NUM_LIT:1> ] : <EOL> nc = ( c - <NUM_LIT:1> ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> else : <EOL> nc = max ( <NUM_LIT:0> , ( c - <NUM_LIT:1> - ds [ <NUM_LIT:1> ] ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> rval = list ( imgshape [ : - <NUM_LIT:2> ] ) + [ nr , nc ] <EOL> return rval <EOL> def __init__ ( self , ds , ignore_border = False , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> mode = '<STR_LIT>' ) : <EOL> self . ds = tuple ( ds ) <EOL> if not all ( [ isinstance ( d , integer_types ) for d in ds ] ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % str ( ds ) ) <EOL> if st is None : <EOL> st = ds <EOL> assert isinstance ( st , ( tuple , list ) ) <EOL> self . st = tuple ( st ) <EOL> self . ignore_border = ignore_border <EOL> self . padding = tuple ( padding ) <EOL> if self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) and not ignore_border : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> if self . padding [ <NUM_LIT:0> ] >= self . ds [ <NUM_LIT:0> ] or self . padding [ <NUM_LIT:1> ] >= self . ds [ <NUM_LIT:1> ] : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> if mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % mode ) <EOL> self . mode = mode <EOL> def make_node ( self , x ) : <EOL> if x . type . ndim != <NUM_LIT:4> : <EOL> raise TypeError ( ) <EOL> x = tensor . as_tensor_variable ( x ) <EOL> broad = x . broadcastable [ : <NUM_LIT:2> ] + ( False , False ) <EOL> out = tensor . TensorType ( x . dtype , broad ) <EOL> return gof . Apply ( self , [ x ] , [ out ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> x , = inp <EOL> z , = out <EOL> if len ( x . shape ) != <NUM_LIT:4> : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> z_shape = self . out_shape ( x . shape , self . ds , self . ignore_border , self . st , <EOL> self . padding ) <EOL> if ( z [ <NUM_LIT:0> ] is None ) or ( z [ <NUM_LIT:0> ] . shape != z_shape ) : <EOL> z [ <NUM_LIT:0> ] = numpy . empty ( z_shape , dtype = x . dtype ) <EOL> zz = z [ <NUM_LIT:0> ] <EOL> pr = zz . shape [ - <NUM_LIT:2> ] <EOL> pc = zz . shape [ - <NUM_LIT:1> ] <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pad_h = self . padding [ <NUM_LIT:0> ] <EOL> pad_w = self . padding [ <NUM_LIT:1> ] <EOL> img_rows = x . shape [ - <NUM_LIT:2> ] + <NUM_LIT:2> * pad_h <EOL> img_cols = x . shape [ - <NUM_LIT:1> ] + <NUM_LIT:2> * pad_w <EOL> inc_pad = self . mode == '<STR_LIT>' <EOL> if self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> y = numpy . zeros ( <EOL> ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , img_rows , img_cols ) , <EOL> dtype = x . dtype ) <EOL> y [ : , : , pad_h : ( img_rows - pad_h ) , pad_w : ( img_cols - pad_w ) ] = x <EOL> else : <EOL> y = x <EOL> func = numpy . max <EOL> if self . mode == '<STR_LIT>' : <EOL> func = numpy . sum <EOL> elif self . mode != '<STR_LIT>' : <EOL> func = numpy . average <EOL> for n in xrange ( x . shape [ <NUM_LIT:0> ] ) : <EOL> for k in xrange ( x . shape [ <NUM_LIT:1> ] ) : <EOL> for r in xrange ( pr ) : <EOL> row_st = r * st0 <EOL> row_end = builtins . min ( row_st + ds0 , img_rows ) <EOL> if not inc_pad : <EOL> row_st = builtins . max ( row_st , self . padding [ <NUM_LIT:0> ] ) <EOL> row_end = builtins . min ( row_end , x . shape [ - <NUM_LIT:2> ] + pad_h ) <EOL> for c in xrange ( pc ) : <EOL> col_st = c * st1 <EOL> col_end = builtins . min ( col_st + ds1 , img_cols ) <EOL> if not inc_pad : <EOL> col_st = builtins . max ( col_st , self . padding [ <NUM_LIT:1> ] ) <EOL> col_end = builtins . min ( col_end , <EOL> x . shape [ - <NUM_LIT:1> ] + pad_w ) <EOL> zz [ n , k , r , c ] = func ( y [ <EOL> n , k , row_st : row_end , col_st : col_end ] ) <EOL> def infer_shape ( self , node , in_shapes ) : <EOL> shp = self . out_shape ( in_shapes [ <NUM_LIT:0> ] , self . ds , <EOL> self . ignore_border , self . st , self . padding ) <EOL> return [ shp ] <EOL> def grad ( self , inp , grads ) : <EOL> x , = inp <EOL> gz , = grads <EOL> if self . mode == '<STR_LIT>' : <EOL> maxout = self ( x ) <EOL> return [ MaxPoolGrad ( self . ds , <EOL> ignore_border = self . ignore_border , <EOL> st = self . st , padding = self . padding ) ( <EOL> x , maxout , gz ) ] <EOL> else : <EOL> return [ AveragePoolGrad ( self . ds , <EOL> ignore_border = self . ignore_border , <EOL> st = self . st , padding = self . padding , <EOL> mode = self . mode ) ( <EOL> x , gz ) ] <EOL> def c_headers ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> if self . mode not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> x , = inp <EOL> z , = out <EOL> fail = sub [ '<STR_LIT>' ] <EOL> ignore_border = int ( self . ignore_border ) <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pd0 , pd1 = self . padding <EOL> ccode = """<STR_LIT>""" <EOL> if self . mode == '<STR_LIT>' : <EOL> ccode += """<STR_LIT>""" <EOL> elif self . mode in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> ccode += """<STR_LIT>""" <EOL> if self . mode == "<STR_LIT>" : <EOL> ccode += """<STR_LIT>""" <EOL> elif self . mode == '<STR_LIT>' and self . ignore_border : <EOL> ccode += """<STR_LIT>""" <EOL> else : <EOL> ccode += """<STR_LIT>""" <EOL> ccode += """<STR_LIT>""" <EOL> return ccode % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:0> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:3> ) <EOL> class PoolGrad ( Op ) : <EOL> __props__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def out_shape ( imgshape , ds , ignore_border = False , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> if len ( imgshape ) < <NUM_LIT:2> : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if st is None : <EOL> st = ds <EOL> r , c = imgshape [ - <NUM_LIT:2> : ] <EOL> r += padding [ <NUM_LIT:0> ] * <NUM_LIT:2> <EOL> c += padding [ <NUM_LIT:1> ] * <NUM_LIT:2> <EOL> if ignore_border : <EOL> out_r = ( r - ds [ <NUM_LIT:0> ] ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> out_c = ( c - ds [ <NUM_LIT:1> ] ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> if isinstance ( r , theano . Variable ) : <EOL> nr = tensor . maximum ( out_r , <NUM_LIT:0> ) <EOL> else : <EOL> nr = numpy . maximum ( out_r , <NUM_LIT:0> ) <EOL> if isinstance ( c , theano . Variable ) : <EOL> nc = tensor . maximum ( out_c , <NUM_LIT:0> ) <EOL> else : <EOL> nc = numpy . maximum ( out_c , <NUM_LIT:0> ) <EOL> else : <EOL> if isinstance ( r , theano . Variable ) : <EOL> nr = tensor . switch ( tensor . ge ( st [ <NUM_LIT:0> ] , ds [ <NUM_LIT:0> ] ) , <EOL> ( r - <NUM_LIT:1> ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> , <EOL> tensor . maximum ( <NUM_LIT:0> , ( r - <NUM_LIT:1> - ds [ <NUM_LIT:0> ] ) // <EOL> st [ <NUM_LIT:0> ] + <NUM_LIT:1> ) + <NUM_LIT:1> ) <EOL> elif st [ <NUM_LIT:0> ] >= ds [ <NUM_LIT:0> ] : <EOL> nr = ( r - <NUM_LIT:1> ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> else : <EOL> nr = max ( <NUM_LIT:0> , ( r - <NUM_LIT:1> - ds [ <NUM_LIT:0> ] ) // st [ <NUM_LIT:0> ] + <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> if isinstance ( c , theano . Variable ) : <EOL> nc = tensor . switch ( tensor . ge ( st [ <NUM_LIT:1> ] , ds [ <NUM_LIT:1> ] ) , <EOL> ( c - <NUM_LIT:1> ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> , <EOL> tensor . maximum ( <NUM_LIT:0> , ( c - <NUM_LIT:1> - ds [ <NUM_LIT:1> ] ) // <EOL> st [ <NUM_LIT:1> ] + <NUM_LIT:1> ) + <NUM_LIT:1> ) <EOL> elif st [ <NUM_LIT:1> ] >= ds [ <NUM_LIT:1> ] : <EOL> nc = ( c - <NUM_LIT:1> ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> else : <EOL> nc = max ( <NUM_LIT:0> , ( c - <NUM_LIT:1> - ds [ <NUM_LIT:1> ] ) // st [ <NUM_LIT:1> ] + <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> rval = list ( imgshape [ : - <NUM_LIT:2> ] ) + [ nr , nc ] <EOL> return rval <EOL> def __init__ ( self , ds , ignore_border , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , mode = '<STR_LIT>' ) : <EOL> self . ds = tuple ( ds ) <EOL> self . ignore_border = ignore_border <EOL> if st is None : <EOL> st = ds <EOL> self . st = tuple ( st ) <EOL> self . padding = tuple ( padding ) <EOL> if mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % mode ) <EOL> self . mode = mode <EOL> def infer_shape ( self , node , in_shapes ) : <EOL> return [ in_shapes [ <NUM_LIT:0> ] ] <EOL> class MaxPoolGrad ( PoolGrad ) : <EOL> def __init__ ( self , ds , ignore_border , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> PoolGrad . __init__ ( self , ds , ignore_border , st , padding , mode = '<STR_LIT>' ) <EOL> def make_node ( self , x , maxout , gz ) : <EOL> assert isinstance ( x , Variable ) and x . ndim == <NUM_LIT:4> <EOL> assert isinstance ( maxout , Variable ) and maxout . ndim == <NUM_LIT:4> <EOL> assert isinstance ( gz , Variable ) and gz . ndim == <NUM_LIT:4> <EOL> x = tensor . as_tensor_variable ( x ) <EOL> maxout = tensor . as_tensor_variable ( maxout ) <EOL> gz = tensor . as_tensor_variable ( gz ) <EOL> return Apply ( self , [ x , maxout , gz ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> assert self . mode == '<STR_LIT>' <EOL> x , maxout , gz = inp <EOL> gx_stg , = out <EOL> pr = maxout . shape [ - <NUM_LIT:2> ] <EOL> pc = maxout . shape [ - <NUM_LIT:1> ] <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pad_h = self . padding [ <NUM_LIT:0> ] <EOL> pad_w = self . padding [ <NUM_LIT:1> ] <EOL> img_rows = x . shape [ - <NUM_LIT:2> ] + <NUM_LIT:2> * pad_h <EOL> img_cols = x . shape [ - <NUM_LIT:1> ] + <NUM_LIT:2> * pad_w <EOL> if self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> y = numpy . zeros ( <EOL> ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , img_rows , img_cols ) , <EOL> dtype = x . dtype ) <EOL> y [ : , : , pad_h : ( img_rows - pad_h ) , pad_w : ( img_cols - pad_w ) ] = x <EOL> else : <EOL> y = x <EOL> gx = numpy . zeros_like ( y ) <EOL> for n in xrange ( x . shape [ <NUM_LIT:0> ] ) : <EOL> for k in xrange ( x . shape [ <NUM_LIT:1> ] ) : <EOL> for r in xrange ( pr ) : <EOL> row_st = builtins . max ( r * st0 , self . padding [ <NUM_LIT:0> ] ) <EOL> row_end = builtins . min ( row_st + ds0 , img_rows ) <EOL> for c in xrange ( pc ) : <EOL> col_st = builtins . max ( c * st1 , self . padding [ <NUM_LIT:1> ] ) <EOL> col_end = builtins . min ( col_st + ds1 , img_cols ) <EOL> for row_ind in xrange ( row_st , row_end ) : <EOL> for col_ind in xrange ( col_st , col_end ) : <EOL> if ( maxout [ n , k , r , c ] == y [ n , k , row_ind , col_ind ] ) : <EOL> gx [ n , k , row_ind , col_ind ] += gz [ n , k , r , c ] <EOL> gx = gx [ : , : , pad_h : ( img_rows - pad_h ) , pad_w : ( img_cols - pad_w ) ] <EOL> gx_stg [ <NUM_LIT:0> ] = gx <EOL> def grad ( self , inp , grads ) : <EOL> x , maxout , gz = inp <EOL> ggx , = grads <EOL> return [ theano . tensor . zeros_like ( x ) , <EOL> theano . tensor . zeros_like ( maxout ) , <EOL> DownsampleFactorMaxGradGrad ( <EOL> self . ds , ignore_border = self . ignore_border , <EOL> st = self . st , padding = self . padding ) ( x , maxout , ggx ) ] <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> assert self . mode == '<STR_LIT>' <EOL> x , z , gz = inp <EOL> gx , = out <EOL> fail = sub [ '<STR_LIT>' ] <EOL> ignore_border = int ( self . ignore_border ) <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pd0 , pd1 = self . padding <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:0> , <NUM_LIT:7> ) <EOL> class AveragePoolGrad ( PoolGrad ) : <EOL> def __init__ ( self , ds , ignore_border , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> mode = '<STR_LIT>' ) : <EOL> assert mode in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> PoolGrad . __init__ ( self , ds , ignore_border , st , padding , mode ) <EOL> def make_node ( self , x , gz , dummy = None ) : <EOL> assert isinstance ( x , Variable ) and x . ndim == <NUM_LIT:4> <EOL> assert isinstance ( gz , Variable ) and gz . ndim == <NUM_LIT:4> <EOL> x = tensor . as_tensor_variable ( x ) <EOL> gz = tensor . as_tensor_variable ( gz ) <EOL> return Apply ( self , [ x , gz ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> if self . mode == '<STR_LIT>' and self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> raise NotImplementedError ( ) <EOL> x , gz = inp <EOL> gx_stg , = out <EOL> z_shape = self . out_shape ( x . shape , self . ds , self . ignore_border , self . st , <EOL> self . padding ) <EOL> if ( gx_stg [ <NUM_LIT:0> ] is None ) or ( gx_stg [ <NUM_LIT:0> ] . shape != z_shape ) : <EOL> gx_stg [ <NUM_LIT:0> ] = numpy . empty ( z_shape , dtype = x . dtype ) <EOL> zz = gx_stg [ <NUM_LIT:0> ] <EOL> pr = zz . shape [ - <NUM_LIT:2> ] <EOL> pc = zz . shape [ - <NUM_LIT:1> ] <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pad_h = self . padding [ <NUM_LIT:0> ] <EOL> pad_w = self . padding [ <NUM_LIT:1> ] <EOL> img_rows = x . shape [ - <NUM_LIT:2> ] + <NUM_LIT:2> * pad_h <EOL> img_cols = x . shape [ - <NUM_LIT:1> ] + <NUM_LIT:2> * pad_w <EOL> inc_pad = self . mode == '<STR_LIT>' <EOL> sum_mode = self . mode == '<STR_LIT>' <EOL> if self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> y = numpy . zeros ( <EOL> ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , img_rows , img_cols ) , <EOL> dtype = x . dtype ) <EOL> y [ : , : , pad_h : ( img_rows - pad_h ) , pad_w : ( img_cols - pad_w ) ] = x <EOL> else : <EOL> y = x <EOL> gx = numpy . zeros_like ( y ) <EOL> for n in xrange ( x . shape [ <NUM_LIT:0> ] ) : <EOL> for k in xrange ( x . shape [ <NUM_LIT:1> ] ) : <EOL> for r in xrange ( pr ) : <EOL> if sum_mode or inc_pad : <EOL> row_st = r * st0 <EOL> else : <EOL> row_st = builtins . max ( r * st0 , self . padding [ <NUM_LIT:0> ] ) <EOL> row_end = builtins . min ( row_st + ds0 , img_rows ) <EOL> for c in xrange ( pc ) : <EOL> if sum_mode or inc_pad : <EOL> col_st = c * st1 <EOL> else : <EOL> col_st = builtins . max ( c * st1 , <EOL> self . padding [ <NUM_LIT:1> ] ) <EOL> col_end = builtins . min ( col_st + ds1 , img_cols ) <EOL> if sum_mode : <EOL> val = gz [ n , k , r , c ] <EOL> else : <EOL> val = gz [ n , k , r , c ] / ( ( row_end - row_st ) * <EOL> ( col_end - col_st ) ) <EOL> gx [ n , k , row_st : row_end , col_st : col_end ] += val <EOL> gx = gx [ : , : , pad_h : ( img_rows - pad_h ) , pad_w : ( img_cols - pad_w ) ] <EOL> gx_stg [ <NUM_LIT:0> ] = gx <EOL> def grad ( self , inp , grads ) : <EOL> x , gz = inp <EOL> ggx , = grads <EOL> return [ theano . tensor . zeros_like ( x ) , <EOL> Pool ( self . ds , ignore_border = self . ignore_border , <EOL> st = self . st , padding = self . padding , mode = self . mode ) ( ggx ) ] <EOL> class DownsampleFactorMaxGradGrad ( Op ) : <EOL> __props__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , ds , ignore_border , st = None , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , mode = '<STR_LIT>' ) : <EOL> self . ds = tuple ( ds ) <EOL> if not all ( [ isinstance ( d , integer_types ) for d in ds ] ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % str ( ds ) ) <EOL> if st is None : <EOL> st = ds <EOL> assert isinstance ( st , ( tuple , list ) ) <EOL> self . st = tuple ( st ) <EOL> self . ignore_border = ignore_border <EOL> self . padding = tuple ( padding ) <EOL> if self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) and not ignore_border : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> if self . padding [ <NUM_LIT:0> ] >= self . ds [ <NUM_LIT:0> ] or self . padding [ <NUM_LIT:1> ] >= self . ds [ <NUM_LIT:1> ] : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> self . mode = mode <EOL> assert self . mode == '<STR_LIT>' <EOL> def make_node ( self , x , maxout , gz ) : <EOL> x = tensor . as_tensor_variable ( x ) <EOL> maxout = tensor . as_tensor_variable ( maxout ) <EOL> gz = tensor . as_tensor_variable ( gz ) <EOL> assert x . ndim == <NUM_LIT:4> <EOL> assert maxout . ndim == <NUM_LIT:4> <EOL> assert gz . ndim == <NUM_LIT:4> <EOL> return Apply ( self , [ x , maxout , gz ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> x , maxout , ggx = inp <EOL> z , = out <EOL> if len ( x . shape ) != <NUM_LIT:4> : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> if ( z [ <NUM_LIT:0> ] is None ) or ( z [ <NUM_LIT:0> ] . shape != maxout . shape ) : <EOL> z [ <NUM_LIT:0> ] = numpy . zeros ( maxout . shape , dtype = x . dtype ) <EOL> ggz = z [ <NUM_LIT:0> ] <EOL> pr = ggz . shape [ - <NUM_LIT:2> ] <EOL> pc = ggz . shape [ - <NUM_LIT:1> ] <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pd0 , pd1 = self . padding <EOL> img_rows = x . shape [ - <NUM_LIT:2> ] + <NUM_LIT:2> * pd0 <EOL> img_cols = x . shape [ - <NUM_LIT:1> ] + <NUM_LIT:2> * pd1 <EOL> if self . padding != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> y_padded = numpy . zeros ( <EOL> ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , img_rows , img_cols ) , <EOL> dtype = x . dtype ) + x . min ( ) - <NUM_LIT:1> <EOL> y_padded [ : , : , pd0 : ( img_rows - pd0 ) , pd1 : ( img_cols - pd1 ) ] = x <EOL> ggx_padded = numpy . zeros ( <EOL> ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , img_rows , img_cols ) , <EOL> dtype = x . dtype ) <EOL> ggx_padded [ : , : , pd0 : ( img_rows - pd0 ) , pd1 : ( img_cols - pd1 ) ] = ggx <EOL> else : <EOL> y_padded = x <EOL> ggx_padded = ggx <EOL> for n in xrange ( x . shape [ <NUM_LIT:0> ] ) : <EOL> for k in xrange ( x . shape [ <NUM_LIT:1> ] ) : <EOL> for r in xrange ( pr ) : <EOL> row_st = r * st0 <EOL> row_end = builtins . min ( row_st + ds0 , img_rows ) <EOL> for c in xrange ( pc ) : <EOL> col_st = c * st1 <EOL> col_end = builtins . min ( col_st + ds1 , img_cols ) <EOL> for row_ind in xrange ( row_st , row_end ) : <EOL> for col_ind in xrange ( col_st , col_end ) : <EOL> if ( maxout [ n , k , r , c ] == y_padded [ n , k , row_ind , col_ind ] ) : <EOL> ggz [ n , k , r , c ] = ggx_padded [ n , k , row_ind , col_ind ] <EOL> def infer_shape ( self , node , in_shapes ) : <EOL> return [ in_shapes [ <NUM_LIT:1> ] ] <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> if self . mode != '<STR_LIT>' : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> x , maxout , ggx = inp <EOL> z , = out <EOL> fail = sub [ '<STR_LIT>' ] <EOL> ignore_border = int ( self . ignore_border ) <EOL> ds0 , ds1 = self . ds <EOL> st0 , st1 = self . st <EOL> pd0 , pd1 = self . padding <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:0> , <NUM_LIT:1> ) </s>
