<s> from theano . tensor . nnet import conv2d <EOL> from theano . tensor . nnet . abstract_conv import ( AbstractConv2d_gradInputs , <EOL> get_conv_output_shape ) <EOL> from theano . tensor . signal . pool import pool_2d , Pool <EOL> from blocks . bricks import ( Initializable , Feedforward , Sequence , Activation , <EOL> LinearLike ) <EOL> from blocks . bricks . base import application , Brick , lazy <EOL> from blocks . roles import add_role , FILTER , BIAS <EOL> from blocks . utils import shared_floatx_nans <EOL> class Convolutional ( LinearLike ) : <EOL> """<STR_LIT>""" <EOL> conv2d_impl = staticmethod ( conv2d ) <EOL> get_output_shape = staticmethod ( get_conv_output_shape ) <EOL> @ lazy ( allocation = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , filter_size , num_filters , num_channels , batch_size = None , <EOL> image_size = ( None , None ) , step = ( <NUM_LIT:1> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' , <EOL> tied_biases = False , ** kwargs ) : <EOL> super ( Convolutional , self ) . __init__ ( ** kwargs ) <EOL> self . filter_size = filter_size <EOL> self . num_filters = num_filters <EOL> self . batch_size = batch_size <EOL> self . num_channels = num_channels <EOL> self . image_size = image_size <EOL> self . step = step <EOL> self . border_mode = border_mode <EOL> self . tied_biases = tied_biases <EOL> def _allocate ( self ) : <EOL> W = shared_floatx_nans ( ( self . num_filters , self . num_channels ) + <EOL> self . filter_size , name = '<STR_LIT>' ) <EOL> add_role ( W , FILTER ) <EOL> self . parameters . append ( W ) <EOL> self . add_auxiliary_variable ( W . norm ( <NUM_LIT:2> ) , name = '<STR_LIT>' ) <EOL> if getattr ( self , '<STR_LIT>' , True ) : <EOL> if self . tied_biases : <EOL> b = shared_floatx_nans ( ( self . num_filters , ) , name = '<STR_LIT:b>' ) <EOL> else : <EOL> if self . image_size == ( None , None ) and not self . tied_biases : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> b = shared_floatx_nans ( self . get_dim ( '<STR_LIT>' ) , name = '<STR_LIT:b>' ) <EOL> add_role ( b , BIAS ) <EOL> self . parameters . append ( b ) <EOL> self . add_auxiliary_variable ( b . norm ( <NUM_LIT:2> ) , name = '<STR_LIT>' ) <EOL> @ application ( inputs = [ '<STR_LIT>' ] , outputs = [ '<STR_LIT>' ] ) <EOL> def apply ( self , input_ ) : <EOL> """<STR_LIT>""" <EOL> if self . image_size == ( None , None ) : <EOL> input_shape = None <EOL> else : <EOL> input_shape = ( self . batch_size , self . num_channels ) <EOL> input_shape += self . image_size <EOL> output = self . conv2d_impl ( <EOL> input_ , self . W , <EOL> input_shape = input_shape , <EOL> subsample = self . step , <EOL> border_mode = self . border_mode , <EOL> filter_shape = ( ( self . num_filters , self . num_channels ) + <EOL> self . filter_size ) ) <EOL> if getattr ( self , '<STR_LIT>' , True ) : <EOL> if self . tied_biases : <EOL> output += self . b . dimshuffle ( '<STR_LIT:x>' , <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) <EOL> else : <EOL> output += self . b . dimshuffle ( '<STR_LIT:x>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> return output <EOL> def get_dim ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return ( self . num_channels , ) + self . image_size <EOL> if name == '<STR_LIT>' : <EOL> input_shape = ( None , self . num_channels ) + self . image_size <EOL> kernel_shape = ( ( self . num_filters , self . num_channels ) + <EOL> self . filter_size ) <EOL> out_shape = self . get_output_shape ( input_shape , kernel_shape , <EOL> self . border_mode , self . step ) <EOL> assert len ( out_shape ) == <NUM_LIT:4> <EOL> return out_shape [ <NUM_LIT:1> : ] <EOL> return super ( Convolutional , self ) . get_dim ( name ) <EOL> @ property <EOL> def num_output_channels ( self ) : <EOL> return self . num_filters <EOL> class ConvolutionalTranspose ( Convolutional ) : <EOL> """<STR_LIT>""" <EOL> @ lazy ( allocation = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , filter_size , num_filters , num_channels , <EOL> original_image_size = None , unused_edge = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ** kwargs ) : <EOL> super ( ConvolutionalTranspose , self ) . __init__ ( <EOL> filter_size , num_filters , num_channels , ** kwargs ) <EOL> self . original_image_size = original_image_size <EOL> self . unused_edge = unused_edge <EOL> @ property <EOL> def original_image_size ( self ) : <EOL> if self . _original_image_size is None : <EOL> if all ( s is None for s in self . image_size ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if isinstance ( self . border_mode , tuple ) : <EOL> border = self . border_mode <EOL> elif self . border_mode == '<STR_LIT>' : <EOL> border = tuple ( k - <NUM_LIT:1> for k in self . filter_size ) <EOL> elif self . border_mode == '<STR_LIT>' : <EOL> border = tuple ( k // <NUM_LIT:2> for k in self . filter_size ) <EOL> else : <EOL> border = [ <NUM_LIT:0> ] * len ( self . image_size ) <EOL> tups = zip ( self . image_size , self . step , self . filter_size , border , <EOL> self . unused_edge ) <EOL> return tuple ( s * ( i - <NUM_LIT:1> ) + k - <NUM_LIT:2> * p + u for i , s , k , p , u in tups ) <EOL> else : <EOL> return self . _original_image_size <EOL> @ original_image_size . setter <EOL> def original_image_size ( self , value ) : <EOL> self . _original_image_size = value <EOL> def conv2d_impl ( self , input_ , W , input_shape , subsample , border_mode , <EOL> filter_shape ) : <EOL> W = W . transpose ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> imshp = ( None , ) + self . get_dim ( '<STR_LIT>' ) <EOL> kshp = ( filter_shape [ <NUM_LIT:1> ] , filter_shape [ <NUM_LIT:0> ] ) + filter_shape [ <NUM_LIT:2> : ] <EOL> return AbstractConv2d_gradInputs ( <EOL> imshp = imshp , kshp = kshp , border_mode = border_mode , <EOL> subsample = subsample ) ( W , input_ , self . get_dim ( '<STR_LIT>' ) [ <NUM_LIT:1> : ] ) <EOL> def get_dim ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return ( self . num_filters , ) + self . original_image_size <EOL> return super ( ConvolutionalTranspose , self ) . get_dim ( name ) <EOL> class Pooling ( Initializable , Feedforward ) : <EOL> """<STR_LIT>""" <EOL> @ lazy ( allocation = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , mode , pooling_size , step , input_dim , ignore_border , <EOL> padding , ** kwargs ) : <EOL> super ( Pooling , self ) . __init__ ( ** kwargs ) <EOL> self . pooling_size = pooling_size <EOL> self . mode = mode <EOL> self . step = step <EOL> self . input_dim = input_dim if input_dim is not None else ( None , ) * <NUM_LIT:3> <EOL> self . ignore_border = ignore_border <EOL> self . padding = padding <EOL> @ property <EOL> def image_size ( self ) : <EOL> return self . input_dim [ - <NUM_LIT:2> : ] <EOL> @ image_size . setter <EOL> def image_size ( self , value ) : <EOL> self . input_dim = self . input_dim [ : - <NUM_LIT:2> ] + value <EOL> @ property <EOL> def num_channels ( self ) : <EOL> return self . input_dim [ <NUM_LIT:0> ] <EOL> @ num_channels . setter <EOL> def num_channels ( self , value ) : <EOL> self . input_dim = ( value , ) + self . input_dim [ <NUM_LIT:1> : ] <EOL> @ application ( inputs = [ '<STR_LIT>' ] , outputs = [ '<STR_LIT>' ] ) <EOL> def apply ( self , input_ ) : <EOL> """<STR_LIT>""" <EOL> output = pool_2d ( input_ , self . pooling_size , st = self . step , <EOL> mode = self . mode , padding = self . padding , <EOL> ignore_border = self . ignore_border ) <EOL> return output <EOL> def get_dim ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return self . input_dim <EOL> if name == '<STR_LIT>' : <EOL> return tuple ( Pool . out_shape ( <EOL> self . input_dim , self . pooling_size , st = self . step , <EOL> ignore_border = self . ignore_border , padding = self . padding ) ) <EOL> @ property <EOL> def num_output_channels ( self ) : <EOL> return self . input_dim [ <NUM_LIT:0> ] <EOL> class MaxPooling ( Pooling ) : <EOL> """<STR_LIT>""" <EOL> @ lazy ( allocation = [ '<STR_LIT>' ] ) <EOL> def __init__ ( self , pooling_size , step = None , input_dim = None , <EOL> ignore_border = True , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ** kwargs ) : <EOL> super ( MaxPooling , self ) . __init__ ( '<STR_LIT>' , pooling_size , <EOL> step = step , input_dim = input_dim , <EOL> ignore_border = ignore_border , <EOL> padding = padding , ** kwargs ) <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> self . mode = getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . padding = getattr ( self , '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . ignore_border = getattr ( self , '<STR_LIT>' , False ) <EOL> class AveragePooling ( Pooling ) : <EOL> """<STR_LIT>""" <EOL> @ lazy ( allocation = [ '<STR_LIT>' ] ) <EOL> def __init__ ( self , pooling_size , step = None , input_dim = None , <EOL> ignore_border = True , padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> include_padding = False , ** kwargs ) : <EOL> mode = '<STR_LIT>' if include_padding else '<STR_LIT>' <EOL> super ( AveragePooling , self ) . __init__ ( mode , pooling_size , <EOL> step = step , input_dim = input_dim , <EOL> ignore_border = ignore_border , <EOL> padding = padding , ** kwargs ) <EOL> class ConvolutionalSequence ( Sequence , Initializable , Feedforward ) : <EOL> """<STR_LIT>""" <EOL> @ lazy ( allocation = [ '<STR_LIT>' ] ) <EOL> def __init__ ( self , layers , num_channels , batch_size = None , <EOL> image_size = ( None , None ) , border_mode = None , tied_biases = None , <EOL> ** kwargs ) : <EOL> self . layers = layers <EOL> self . image_size = image_size <EOL> self . num_channels = num_channels <EOL> self . batch_size = batch_size <EOL> self . border_mode = border_mode <EOL> self . tied_biases = tied_biases <EOL> application_methods = [ brick . apply for brick in layers ] <EOL> super ( ConvolutionalSequence , self ) . __init__ ( <EOL> application_methods = application_methods , ** kwargs ) <EOL> def get_dim ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return ( ( self . num_channels , ) + self . image_size ) <EOL> if name == '<STR_LIT>' : <EOL> last = len ( self . layers ) - <NUM_LIT:1> <EOL> while last >= <NUM_LIT:0> : <EOL> try : <EOL> return self . layers [ last ] . get_dim ( name ) <EOL> except ValueError : <EOL> last -= <NUM_LIT:1> <EOL> return self . get_dim ( '<STR_LIT>' ) <EOL> return super ( ConvolutionalSequence , self ) . get_dim ( name ) <EOL> def _push_allocation_config ( self ) : <EOL> num_channels = self . num_channels <EOL> image_size = self . image_size <EOL> for layer in self . layers : <EOL> if isinstance ( layer , Activation ) : <EOL> layer . push_allocation_config ( ) <EOL> continue <EOL> if self . border_mode is not None : <EOL> layer . border_mode = self . border_mode <EOL> if self . tied_biases is not None : <EOL> layer . tied_biases = self . tied_biases <EOL> layer . image_size = image_size <EOL> layer . num_channels = num_channels <EOL> layer . batch_size = self . batch_size <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None : <EOL> layer . use_bias = self . use_bias <EOL> layer . push_allocation_config ( ) <EOL> if None not in layer . image_size : <EOL> output_shape = layer . get_dim ( '<STR_LIT>' ) <EOL> image_size = output_shape [ <NUM_LIT:1> : ] <EOL> num_channels = layer . num_output_channels <EOL> class Flattener ( Brick ) : <EOL> """<STR_LIT>""" <EOL> @ application ( inputs = [ '<STR_LIT>' ] , outputs = [ '<STR_LIT>' ] ) <EOL> def apply ( self , input_ ) : <EOL> return input_ . flatten ( ndim = <NUM_LIT:2> ) </s>
