<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import functools <EOL> import numpy as np <EOL> from theano . compat . six . moves import xrange <EOL> import theano <EOL> import theano . tensor as T <EOL> from theano . tensor . nnet . conv import conv2d <EOL> from pylearn2 . packaged_dependencies . theano_linear . conv2d import Conv2d as OrigConv2D <EOL> from pylearn2 . linear . linear_transform import LinearTransform as P2LT <EOL> from pylearn2 . utils import sharedX <EOL> from pylearn2 . utils . rng import make_np_rng <EOL> default_seed = [ <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:6> , <NUM_LIT:9> ] <EOL> default_sparse_seed = [ <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:6> ] <EOL> class Conv2D ( OrigConv2D ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filters , batch_size , input_space , <EOL> output_axes = ( '<STR_LIT:b>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' ) , subsample = ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> border_mode = '<STR_LIT>' , filters_shape = None , message = '<STR_LIT>' ) : <EOL> assert batch_size is None or batch_size > <NUM_LIT:0> <EOL> self . input_space = input_space <EOL> self . output_axes = output_axes <EOL> super ( Conv2D , self ) . __init__ ( <EOL> filters = filters , <EOL> img_shape = ( batch_size , input_space . num_channels , <EOL> input_space . shape [ <NUM_LIT:0> ] , input_space . shape [ <NUM_LIT:1> ] ) , <EOL> subsample = subsample , <EOL> border_mode = border_mode , <EOL> filters_shape = filters . get_value ( borrow = True ) . shape , <EOL> message = message <EOL> ) <EOL> @ functools . wraps ( P2LT . get_params ) <EOL> def get_params ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _filters ] <EOL> @ functools . wraps ( P2LT . get_weights_topo ) <EOL> def get_weights_topo ( self , borrow ) : <EOL> """<STR_LIT>""" <EOL> return np . transpose ( self . _filters . get_value ( borrow = borrow ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> def lmul ( self , x ) : <EOL> """<STR_LIT>""" <EOL> assert x . ndim == <NUM_LIT:4> <EOL> axes = self . input_space . axes <EOL> assert len ( axes ) == <NUM_LIT:4> <EOL> op_axes = ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> if tuple ( axes ) != op_axes : <EOL> x = x . dimshuffle ( <EOL> axes . index ( '<STR_LIT:b>' ) , <EOL> axes . index ( '<STR_LIT:c>' ) , <EOL> axes . index ( <NUM_LIT:0> ) , <EOL> axes . index ( <NUM_LIT:1> ) ) <EOL> rval = conv2d ( <EOL> x , self . _filters , <EOL> image_shape = self . _img_shape , <EOL> filter_shape = self . _filters_shape , <EOL> subsample = self . _subsample , <EOL> border_mode = self . _border_mode , <EOL> ) <EOL> axes = self . output_axes <EOL> assert len ( axes ) == <NUM_LIT:4> <EOL> if tuple ( axes ) != op_axes : <EOL> rval = rval . dimshuffle ( <EOL> op_axes . index ( axes [ <NUM_LIT:0> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:1> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:2> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:3> ] ) <EOL> ) <EOL> return rval <EOL> def lmul_T ( self , x ) : <EOL> """<STR_LIT>""" <EOL> assert x . dtype == self . _filters . dtype <EOL> op_axes = ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> axes = self . output_axes <EOL> if tuple ( axes ) != op_axes : <EOL> x = x . dimshuffle ( <EOL> axes . index ( '<STR_LIT:b>' ) , <EOL> axes . index ( '<STR_LIT:c>' ) , <EOL> axes . index ( <NUM_LIT:0> ) , <EOL> axes . index ( <NUM_LIT:1> ) <EOL> ) <EOL> dummy_v = T . tensor4 ( ) <EOL> dummy_v . name = '<STR_LIT>' <EOL> if theano . config . compute_test_value == '<STR_LIT>' : <EOL> dummy_v . tag . test_value = np . zeros ( <EOL> ( x . tag . test_value . shape [ <NUM_LIT:0> ] , <EOL> self . input_space . num_channels , <EOL> self . input_space . shape [ <NUM_LIT:0> ] , <EOL> self . input_space . shape [ <NUM_LIT:1> ] ) , <EOL> dtype = dummy_v . dtype <EOL> ) <EOL> z_hs = conv2d ( <EOL> dummy_v , self . _filters , <EOL> image_shape = self . _img_shape , <EOL> filter_shape = self . _filters_shape , <EOL> subsample = self . _subsample , <EOL> border_mode = self . _border_mode , <EOL> ) <EOL> rval , xdummy = z_hs . owner . op . grad ( ( dummy_v , self . _filters ) , ( x , ) ) <EOL> axes = self . input_space . axes <EOL> assert len ( axes ) == <NUM_LIT:4> <EOL> if tuple ( axes ) != op_axes : <EOL> rval = rval . dimshuffle ( <EOL> op_axes . index ( axes [ <NUM_LIT:0> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:1> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:2> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:3> ] ) <EOL> ) <EOL> return rval <EOL> def lmul_sq_T ( self , x ) : <EOL> """<STR_LIT>""" <EOL> assert x . dtype == self . _filters . dtype <EOL> op_axes = ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> axes = self . output_axes <EOL> if tuple ( axes ) != op_axes : <EOL> x = x . dimshuffle ( <EOL> axes . index ( '<STR_LIT:b>' ) , <EOL> axes . index ( '<STR_LIT:c>' ) , <EOL> axes . index ( <NUM_LIT:0> ) , <EOL> axes . index ( <NUM_LIT:1> ) <EOL> ) <EOL> dummy_v = T . tensor4 ( ) <EOL> sqfilt = T . square ( self . _filters ) <EOL> z_hs = conv2d ( <EOL> dummy_v , sqfilt , <EOL> image_shape = self . _img_shape , <EOL> filter_shape = self . _filters_shape , <EOL> subsample = self . _subsample , <EOL> border_mode = self . _border_mode , <EOL> ) <EOL> rval , xdummy = z_hs . owner . op . grad ( ( dummy_v , sqfilt ) , ( x , ) ) <EOL> axes = self . input_space . axes <EOL> assert len ( axes ) == <NUM_LIT:4> <EOL> if tuple ( axes ) != op_axes : <EOL> rval = rval . dimshuffle ( <EOL> op_axes . index ( axes [ <NUM_LIT:0> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:1> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:2> ] ) , <EOL> op_axes . index ( axes [ <NUM_LIT:3> ] ) <EOL> ) <EOL> return rval <EOL> def set_batch_size ( self , batch_size ) : <EOL> """<STR_LIT>""" <EOL> self . _img_shape = tuple ( [ batch_size ] + list ( self . _img_shape [ <NUM_LIT:1> : ] ) ) <EOL> def make_random_conv2D ( irange , input_space , output_space , <EOL> kernel_shape , batch_size = None , subsample = ( <NUM_LIT:1> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' , <EOL> message = "<STR_LIT>" , rng = None ) : <EOL> """<STR_LIT>""" <EOL> rng = make_np_rng ( rng , default_seed , which_method = '<STR_LIT>' ) <EOL> W = sharedX ( rng . uniform ( <EOL> - irange , irange , <EOL> ( output_space . num_channels , input_space . num_channels , <EOL> kernel_shape [ <NUM_LIT:0> ] , kernel_shape [ <NUM_LIT:1> ] ) <EOL> ) ) <EOL> return Conv2D ( <EOL> filters = W , <EOL> batch_size = batch_size , <EOL> input_space = input_space , <EOL> output_axes = output_space . axes , <EOL> subsample = subsample , border_mode = border_mode , <EOL> filters_shape = W . get_value ( borrow = True ) . shape , message = message <EOL> ) <EOL> def make_sparse_random_conv2D ( num_nonzero , input_space , output_space , <EOL> kernel_shape , batch_size , subsample = ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> border_mode = '<STR_LIT>' , message = "<STR_LIT>" , rng = None ) : <EOL> """<STR_LIT>""" <EOL> raise AssertionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> rng = make_np_rng ( rng , default_sparse_seed , <EOL> which_method = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> W = np . zeros ( ( output_space . num_channels , input_space . num_channels , <EOL> kernel_shape [ <NUM_LIT:0> ] , kernel_shape [ <NUM_LIT:1> ] ) ) <EOL> def random_coord ( ) : <EOL> return [ rng . randint ( dim ) for dim in W . shape ] <EOL> for i in xrange ( num_nonzero ) : <EOL> o , ch , r , c = random_coord ( ) <EOL> while W [ o , ch , r , c ] != <NUM_LIT:0> : <EOL> o , ch , r , c = random_coord ( ) <EOL> W [ o , ch , r , c ] = rng . randn ( ) <EOL> W = sharedX ( W ) <EOL> return Conv2D ( <EOL> filters = W , <EOL> batch_size = batch_size , <EOL> input_space = input_space , <EOL> output_axes = output_space . axes , <EOL> subsample = subsample , border_mode = border_mode , <EOL> filters_shape = W . get_value ( borrow = True ) . shape , message = message <EOL> ) </s>
