<s> import theano . tensor as T <EOL> from . base import Layer <EOL> from . . utils import as_tuple <EOL> from theano . tensor . signal . pool import pool_2d <EOL> __all__ = [ <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> ] <EOL> def pool_output_length ( input_length , pool_size , stride , pad , ignore_border ) : <EOL> """<STR_LIT>""" <EOL> if input_length is None or pool_size is None : <EOL> return None <EOL> if ignore_border : <EOL> output_length = input_length + <NUM_LIT:2> * pad - pool_size + <NUM_LIT:1> <EOL> output_length = ( output_length + stride - <NUM_LIT:1> ) // stride <EOL> else : <EOL> assert pad == <NUM_LIT:0> <EOL> if stride >= pool_size : <EOL> output_length = ( input_length + stride - <NUM_LIT:1> ) // stride <EOL> else : <EOL> output_length = max ( <EOL> <NUM_LIT:0> , ( input_length - pool_size + stride - <NUM_LIT:1> ) // stride ) + <NUM_LIT:1> <EOL> return output_length <EOL> class Pool1DLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_size , stride = None , pad = <NUM_LIT:0> , <EOL> ignore_border = True , mode = '<STR_LIT>' , ** kwargs ) : <EOL> super ( Pool1DLayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> if len ( self . input_shape ) != <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . input_shape , ) ) <EOL> self . pool_size = as_tuple ( pool_size , <NUM_LIT:1> ) <EOL> self . stride = self . pool_size if stride is None else as_tuple ( stride , <NUM_LIT:1> ) <EOL> self . pad = as_tuple ( pad , <NUM_LIT:1> ) <EOL> self . ignore_border = ignore_border <EOL> self . mode = mode <EOL> def get_output_shape_for ( self , input_shape ) : <EOL> output_shape = list ( input_shape ) <EOL> output_shape [ - <NUM_LIT:1> ] = pool_output_length ( input_shape [ - <NUM_LIT:1> ] , <EOL> pool_size = self . pool_size [ <NUM_LIT:0> ] , <EOL> stride = self . stride [ <NUM_LIT:0> ] , <EOL> pad = self . pad [ <NUM_LIT:0> ] , <EOL> ignore_border = self . ignore_border , <EOL> ) <EOL> return tuple ( output_shape ) <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> input_4d = T . shape_padright ( input , <NUM_LIT:1> ) <EOL> pooled = pool_2d ( input_4d , <EOL> ds = ( self . pool_size [ <NUM_LIT:0> ] , <NUM_LIT:1> ) , <EOL> st = ( self . stride [ <NUM_LIT:0> ] , <NUM_LIT:1> ) , <EOL> ignore_border = self . ignore_border , <EOL> padding = ( self . pad [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , <EOL> mode = self . mode , <EOL> ) <EOL> return pooled [ : , : , : , <NUM_LIT:0> ] <EOL> class Pool2DLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_size , stride = None , pad = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ignore_border = True , mode = '<STR_LIT>' , ** kwargs ) : <EOL> super ( Pool2DLayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> self . pool_size = as_tuple ( pool_size , <NUM_LIT:2> ) <EOL> if len ( self . input_shape ) != <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . input_shape , ) ) <EOL> if stride is None : <EOL> self . stride = self . pool_size <EOL> else : <EOL> self . stride = as_tuple ( stride , <NUM_LIT:2> ) <EOL> self . pad = as_tuple ( pad , <NUM_LIT:2> ) <EOL> self . ignore_border = ignore_border <EOL> self . mode = mode <EOL> def get_output_shape_for ( self , input_shape ) : <EOL> output_shape = list ( input_shape ) <EOL> output_shape [ <NUM_LIT:2> ] = pool_output_length ( input_shape [ <NUM_LIT:2> ] , <EOL> pool_size = self . pool_size [ <NUM_LIT:0> ] , <EOL> stride = self . stride [ <NUM_LIT:0> ] , <EOL> pad = self . pad [ <NUM_LIT:0> ] , <EOL> ignore_border = self . ignore_border , <EOL> ) <EOL> output_shape [ <NUM_LIT:3> ] = pool_output_length ( input_shape [ <NUM_LIT:3> ] , <EOL> pool_size = self . pool_size [ <NUM_LIT:1> ] , <EOL> stride = self . stride [ <NUM_LIT:1> ] , <EOL> pad = self . pad [ <NUM_LIT:1> ] , <EOL> ignore_border = self . ignore_border , <EOL> ) <EOL> return tuple ( output_shape ) <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> pooled = pool_2d ( input , <EOL> ds = self . pool_size , <EOL> st = self . stride , <EOL> ignore_border = self . ignore_border , <EOL> padding = self . pad , <EOL> mode = self . mode , <EOL> ) <EOL> return pooled <EOL> class MaxPool1DLayer ( Pool1DLayer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_size , stride = None , pad = <NUM_LIT:0> , <EOL> ignore_border = True , ** kwargs ) : <EOL> super ( MaxPool1DLayer , self ) . __init__ ( incoming , <EOL> pool_size , <EOL> stride , <EOL> pad , <EOL> ignore_border , <EOL> mode = '<STR_LIT>' , <EOL> ** kwargs ) <EOL> class MaxPool2DLayer ( Pool2DLayer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_size , stride = None , pad = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ignore_border = True , ** kwargs ) : <EOL> super ( MaxPool2DLayer , self ) . __init__ ( incoming , <EOL> pool_size , <EOL> stride , <EOL> pad , <EOL> ignore_border , <EOL> mode = '<STR_LIT>' , <EOL> ** kwargs ) <EOL> class Upscale1DLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , scale_factor , ** kwargs ) : <EOL> super ( Upscale1DLayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> self . scale_factor = as_tuple ( scale_factor , <NUM_LIT:1> ) <EOL> if self . scale_factor [ <NUM_LIT:0> ] < <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> self . scale_factor ) ) <EOL> def get_output_shape_for ( self , input_shape ) : <EOL> output_shape = list ( input_shape ) <EOL> if output_shape [ <NUM_LIT:2> ] is not None : <EOL> output_shape [ <NUM_LIT:2> ] *= self . scale_factor [ <NUM_LIT:0> ] <EOL> return tuple ( output_shape ) <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> a , = self . scale_factor <EOL> upscaled = input <EOL> if a > <NUM_LIT:1> : <EOL> upscaled = T . extra_ops . repeat ( upscaled , a , <NUM_LIT:2> ) <EOL> return upscaled <EOL> class Upscale2DLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , scale_factor , ** kwargs ) : <EOL> super ( Upscale2DLayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> self . scale_factor = as_tuple ( scale_factor , <NUM_LIT:2> ) <EOL> if self . scale_factor [ <NUM_LIT:0> ] < <NUM_LIT:1> or self . scale_factor [ <NUM_LIT:1> ] < <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> self . scale_factor ) ) <EOL> def get_output_shape_for ( self , input_shape ) : <EOL> output_shape = list ( input_shape ) <EOL> if output_shape [ <NUM_LIT:2> ] is not None : <EOL> output_shape [ <NUM_LIT:2> ] *= self . scale_factor [ <NUM_LIT:0> ] <EOL> if output_shape [ <NUM_LIT:3> ] is not None : <EOL> output_shape [ <NUM_LIT:3> ] *= self . scale_factor [ <NUM_LIT:1> ] <EOL> return tuple ( output_shape ) <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> a , b = self . scale_factor <EOL> upscaled = input <EOL> if b > <NUM_LIT:1> : <EOL> upscaled = T . extra_ops . repeat ( upscaled , b , <NUM_LIT:3> ) <EOL> if a > <NUM_LIT:1> : <EOL> upscaled = T . extra_ops . repeat ( upscaled , a , <NUM_LIT:2> ) <EOL> return upscaled <EOL> class FeaturePoolLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_size , axis = <NUM_LIT:1> , pool_function = T . max , <EOL> ** kwargs ) : <EOL> super ( FeaturePoolLayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> self . pool_size = pool_size <EOL> self . axis = axis <EOL> self . pool_function = pool_function <EOL> num_feature_maps = self . input_shape [ self . axis ] <EOL> if num_feature_maps % self . pool_size != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( num_feature_maps , self . pool_size ) ) <EOL> def get_output_shape_for ( self , input_shape ) : <EOL> output_shape = list ( input_shape ) <EOL> output_shape [ self . axis ] = input_shape [ self . axis ] // self . pool_size <EOL> return tuple ( output_shape ) <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> input_shape = tuple ( input . shape ) <EOL> num_feature_maps = input_shape [ self . axis ] <EOL> num_feature_maps_out = num_feature_maps // self . pool_size <EOL> pool_shape = ( input_shape [ : self . axis ] + <EOL> ( num_feature_maps_out , self . pool_size ) + <EOL> input_shape [ self . axis + <NUM_LIT:1> : ] ) <EOL> input_reshaped = input . reshape ( pool_shape ) <EOL> return self . pool_function ( input_reshaped , axis = self . axis + <NUM_LIT:1> ) <EOL> class FeatureWTALayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_size , axis = <NUM_LIT:1> , ** kwargs ) : <EOL> super ( FeatureWTALayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> self . pool_size = pool_size <EOL> self . axis = axis <EOL> num_feature_maps = self . input_shape [ self . axis ] <EOL> if num_feature_maps % self . pool_size != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( num_feature_maps , self . pool_size ) ) <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> num_feature_maps = input . shape [ self . axis ] <EOL> num_pools = num_feature_maps // self . pool_size <EOL> pool_shape = ( ) <EOL> arange_shuffle_pattern = ( ) <EOL> for k in range ( self . axis ) : <EOL> pool_shape += ( input . shape [ k ] , ) <EOL> arange_shuffle_pattern += ( '<STR_LIT:x>' , ) <EOL> pool_shape += ( num_pools , self . pool_size ) <EOL> arange_shuffle_pattern += ( '<STR_LIT:x>' , <NUM_LIT:0> ) <EOL> for k in range ( self . axis + <NUM_LIT:1> , input . ndim ) : <EOL> pool_shape += ( input . shape [ k ] , ) <EOL> arange_shuffle_pattern += ( '<STR_LIT:x>' , ) <EOL> input_reshaped = input . reshape ( pool_shape ) <EOL> max_indices = T . argmax ( input_reshaped , axis = self . axis + <NUM_LIT:1> , <EOL> keepdims = True ) <EOL> arange = T . arange ( self . pool_size ) . dimshuffle ( * arange_shuffle_pattern ) <EOL> mask = T . eq ( max_indices , arange ) . reshape ( input . shape ) <EOL> return input * mask <EOL> class GlobalPoolLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , incoming , pool_function = T . mean , ** kwargs ) : <EOL> super ( GlobalPoolLayer , self ) . __init__ ( incoming , ** kwargs ) <EOL> self . pool_function = pool_function <EOL> def get_output_shape_for ( self , input_shape ) : <EOL> return input_shape [ : <NUM_LIT:2> ] <EOL> def get_output_for ( self , input , ** kwargs ) : <EOL> return self . pool_function ( input . flatten ( <NUM_LIT:3> ) , axis = <NUM_LIT:2> ) </s>
