<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import theano . tensor as T <EOL> def conv1d_sc ( input , filters , image_shape = None , filter_shape = None , <EOL> border_mode = '<STR_LIT>' , subsample = ( <NUM_LIT:1> , ) , filter_flip = True ) : <EOL> """<STR_LIT>""" <EOL> if border_mode not in ( '<STR_LIT>' , <NUM_LIT:0> , ( <NUM_LIT:0> , ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" % border_mode ) <EOL> if image_shape is None : <EOL> image_shape_sc = None <EOL> else : <EOL> image_shape_sc = ( image_shape [ <NUM_LIT:0> ] , <NUM_LIT:1> , image_shape [ <NUM_LIT:1> ] , image_shape [ <NUM_LIT:2> ] ) <EOL> if filter_shape is None : <EOL> filter_shape_sc = None <EOL> else : <EOL> filter_shape_sc = ( filter_shape [ <NUM_LIT:0> ] , <NUM_LIT:1> , filter_shape [ <NUM_LIT:1> ] , <EOL> filter_shape [ <NUM_LIT:2> ] ) <EOL> input_sc = input . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> filters_sc = filters . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' , <NUM_LIT:1> , <NUM_LIT:2> ) [ : , : , : : - <NUM_LIT:1> , : ] <EOL> conved = T . nnet . conv2d ( input_sc , filters_sc , image_shape_sc , <EOL> filter_shape_sc , subsample = ( <NUM_LIT:1> , subsample [ <NUM_LIT:0> ] ) , <EOL> filter_flip = filter_flip ) <EOL> return conved [ : , : , <NUM_LIT:0> , : ] <EOL> def conv1d_mc0 ( input , filters , image_shape = None , filter_shape = None , <EOL> border_mode = '<STR_LIT>' , subsample = ( <NUM_LIT:1> , ) , filter_flip = True ) : <EOL> """<STR_LIT>""" <EOL> if image_shape is None : <EOL> image_shape_mc0 = None <EOL> else : <EOL> image_shape_mc0 = ( image_shape [ <NUM_LIT:0> ] , image_shape [ <NUM_LIT:1> ] , <NUM_LIT:1> , image_shape [ <NUM_LIT:2> ] ) <EOL> if filter_shape is None : <EOL> filter_shape_mc0 = None <EOL> else : <EOL> filter_shape_mc0 = ( filter_shape [ <NUM_LIT:0> ] , filter_shape [ <NUM_LIT:1> ] , <NUM_LIT:1> , <EOL> filter_shape [ <NUM_LIT:2> ] ) <EOL> if isinstance ( border_mode , tuple ) : <EOL> ( border_mode , ) = border_mode <EOL> if isinstance ( border_mode , int ) : <EOL> border_mode = ( <NUM_LIT:0> , border_mode ) <EOL> input_mc0 = input . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:x>' , <NUM_LIT:2> ) <EOL> filters_mc0 = filters . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:x>' , <NUM_LIT:2> ) <EOL> conved = T . nnet . conv2d ( <EOL> input_mc0 , filters_mc0 , image_shape_mc0 , filter_shape_mc0 , <EOL> subsample = ( <NUM_LIT:1> , subsample [ <NUM_LIT:0> ] ) , border_mode = border_mode , <EOL> filter_flip = filter_flip ) <EOL> return conved [ : , : , <NUM_LIT:0> , : ] <EOL> def conv1d_mc1 ( input , filters , image_shape = None , filter_shape = None , <EOL> border_mode = '<STR_LIT>' , subsample = ( <NUM_LIT:1> , ) , filter_flip = True ) : <EOL> """<STR_LIT>""" <EOL> if image_shape is None : <EOL> image_shape_mc1 = None <EOL> else : <EOL> image_shape_mc1 = ( image_shape [ <NUM_LIT:0> ] , image_shape [ <NUM_LIT:1> ] , image_shape [ <NUM_LIT:2> ] , <NUM_LIT:1> ) <EOL> if filter_shape is None : <EOL> filter_shape_mc1 = None <EOL> else : <EOL> filter_shape_mc1 = ( filter_shape [ <NUM_LIT:0> ] , filter_shape [ <NUM_LIT:1> ] , <EOL> filter_shape [ <NUM_LIT:2> ] , <NUM_LIT:1> ) <EOL> if isinstance ( border_mode , tuple ) : <EOL> ( border_mode , ) = border_mode <EOL> if isinstance ( border_mode , int ) : <EOL> border_mode = ( border_mode , <NUM_LIT:0> ) <EOL> input_mc1 = input . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT:x>' ) <EOL> filters_mc1 = filters . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT:x>' ) <EOL> conved = T . nnet . conv2d ( <EOL> input_mc1 , filters_mc1 , image_shape_mc1 , filter_shape_mc1 , <EOL> subsample = ( subsample [ <NUM_LIT:0> ] , <NUM_LIT:1> ) , border_mode = border_mode , <EOL> filter_flip = filter_flip ) <EOL> return conved [ : , : , : , <NUM_LIT:0> ] <EOL> def conv1d_unstrided ( input , filters , image_shape , filter_shape , <EOL> border_mode = '<STR_LIT>' , subsample = ( <NUM_LIT:1> , ) , filter_flip = True , <EOL> implementation = conv1d_sc ) : <EOL> """<STR_LIT>""" <EOL> batch_size , num_input_channels , input_length = image_shape <EOL> num_filters , num_input_channels_ , filter_length = filter_shape <EOL> stride = subsample [ <NUM_LIT:0> ] <EOL> if filter_length % stride > <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( filter_length , stride ) ) <EOL> if border_mode not in ( '<STR_LIT>' , <NUM_LIT:0> , ( <NUM_LIT:0> , ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" % border_mode ) <EOL> num_steps = filter_length // stride <EOL> truncated_length = ( input_length // stride ) * stride <EOL> input_truncated = input [ : , : , : truncated_length ] <EOL> r_input_shape = ( batch_size , num_input_channels , <EOL> truncated_length // stride , stride ) <EOL> r_input = input_truncated . reshape ( r_input_shape ) <EOL> r_input_folded_shape = ( batch_size , num_input_channels * stride , <EOL> truncated_length // stride ) <EOL> r_input_folded = r_input . dimshuffle ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> ) . reshape ( r_input_folded_shape ) <EOL> r_filter_shape = ( num_filters , num_input_channels , num_steps , stride ) <EOL> r_filters_flipped = filters [ : , : , : : - <NUM_LIT:1> ] . reshape ( r_filter_shape ) <EOL> r_filter_folded_shape = ( num_filters , num_input_channels * stride , <EOL> num_steps ) <EOL> r_filters_flipped_folded = r_filters_flipped . dimshuffle ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> ) . reshape ( r_filter_folded_shape ) <EOL> r_filters_folded = r_filters_flipped_folded [ : , : , : : - <NUM_LIT:1> ] <EOL> return implementation ( r_input_folded , r_filters_folded , <EOL> r_input_folded_shape , r_filter_folded_shape , <EOL> border_mode , subsample = ( <NUM_LIT:1> , ) , filter_flip = filter_flip ) <EOL> def conv1d_sd ( input , filters , image_shape , filter_shape , border_mode = '<STR_LIT>' , <EOL> subsample = ( <NUM_LIT:1> , ) , filter_flip = True ) : <EOL> """<STR_LIT>""" <EOL> if border_mode not in ( '<STR_LIT>' , <NUM_LIT:0> , ( <NUM_LIT:0> , ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" % border_mode ) <EOL> batch_size , num_input_channels , input_length = image_shape <EOL> num_filters , num_input_channels_ , filter_length = filter_shape <EOL> stride = subsample [ <NUM_LIT:0> ] <EOL> if filter_length % stride > <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( filter_length , stride ) ) <EOL> num_steps = filter_length // stride <EOL> output_length = ( input_length - filter_length + stride ) // stride <EOL> padded_length = ( ( input_length // filter_length ) * filter_length + <EOL> ( num_steps - <NUM_LIT:1> ) * stride ) <EOL> truncated_length = min ( input_length , padded_length ) <EOL> input_truncated = input [ : , : , : truncated_length ] <EOL> input_padded_shape = ( batch_size , num_input_channels , padded_length ) <EOL> input_padded = T . zeros ( input_padded_shape ) <EOL> input_padded = T . set_subtensor ( input_padded [ : , : , : truncated_length ] , <EOL> input_truncated ) <EOL> inputs = [ ] <EOL> for num in range ( num_steps ) : <EOL> shift = num * stride <EOL> length = ( padded_length - shift ) // filter_length <EOL> r_input_shape = ( batch_size , num_input_channels , length , filter_length ) <EOL> r_input = input_padded [ <EOL> : , : , shift : length * filter_length + shift ] . reshape ( r_input_shape ) <EOL> inputs . append ( r_input ) <EOL> inputs_stacked = T . stack ( * inputs ) <EOL> filters_flipped = filters [ : , : , : : - <NUM_LIT:1> ] if filter_flip else filters <EOL> r_conved = T . tensordot ( inputs_stacked , filters_flipped , <EOL> np . asarray ( [ [ <NUM_LIT:2> , <NUM_LIT:4> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) ) <EOL> r_conved = r_conved . dimshuffle ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> conved = r_conved . reshape ( ( r_conved . shape [ <NUM_LIT:0> ] , r_conved . shape [ <NUM_LIT:1> ] , <EOL> r_conved . shape [ <NUM_LIT:2> ] * r_conved . shape [ <NUM_LIT:3> ] ) ) <EOL> return conved [ : , : , : output_length ] <EOL> def conv1d_md ( input , filters , image_shape , filter_shape , border_mode = '<STR_LIT>' , <EOL> subsample = ( <NUM_LIT:1> , ) , filter_flip = True ) : <EOL> """<STR_LIT>""" <EOL> if border_mode not in ( '<STR_LIT>' , <NUM_LIT:0> , ( <NUM_LIT:0> , ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" % border_mode ) <EOL> batch_size , num_input_channels , input_length = image_shape <EOL> num_filters , num_input_channels_ , filter_length = filter_shape <EOL> stride = subsample [ <NUM_LIT:0> ] <EOL> if filter_length % stride > <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( filter_length , stride ) ) <EOL> num_steps = filter_length // stride <EOL> output_length = ( input_length - filter_length + stride ) // stride <EOL> output_shape = ( batch_size , num_filters , output_length ) <EOL> filters_flipped = filters [ : , : , : : - <NUM_LIT:1> ] if filter_flip else filters <EOL> conved = T . zeros ( output_shape ) <EOL> for num in range ( num_steps ) : <EOL> shift = num * stride <EOL> length = ( input_length - shift ) // filter_length <EOL> if length == <NUM_LIT:0> : <EOL> continue <EOL> r_input_shape = ( batch_size , num_input_channels , length , filter_length ) <EOL> r_input = input [ <EOL> : , : , shift : length * filter_length + shift ] . reshape ( r_input_shape ) <EOL> r_conved = T . tensordot ( r_input , filters_flipped , <EOL> np . asarray ( [ [ <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) ) <EOL> r_conved = r_conved . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> conved = T . set_subtensor ( conved [ : , : , num : : num_steps ] , r_conved ) <EOL> return conved </s>
