<s> import os <EOL> import sys <EOL> import argparse <EOL> import theano . tensor as T <EOL> homepath = os . path . join ( '<STR_LIT:..>' , '<STR_LIT:..>' ) <EOL> if not homepath in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , homepath ) <EOL> from dlearn . models . layer import FullConnLayer , ConvPoolLayer <EOL> from dlearn . models . nnet import NeuralNet <EOL> from dlearn . utils import actfuncs , costfuncs <EOL> from dlearn . utils . serialize import load_data , save_data <EOL> from dlearn . optimization import sgd <EOL> desctxt = """<STR_LIT>""" <EOL> dataset_txt = """<STR_LIT>""" <EOL> output_txt = """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( description = desctxt ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , nargs = <NUM_LIT:1> , required = True , <EOL> metavar = '<STR_LIT:name>' , help = dataset_txt ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , <EOL> metavar = '<STR_LIT:name>' , help = output_txt ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> def train_model ( dataset , use_scpool ) : <EOL> """<STR_LIT>""" <EOL> def shape_constrained_pooling ( F , S ) : <EOL> masked_F = T . switch ( T . eq ( S , <NUM_LIT:1> ) , F , <NUM_LIT:0> ) <EOL> s = masked_F . sum ( axis = [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> Z = T . neq ( masked_F , <NUM_LIT:0> ) . sum ( axis = [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> return s / Z <EOL> X = T . tensor4 ( ) <EOL> A = T . matrix ( ) <EOL> S = T . tensor3 ( ) <EOL> layers = [ ] <EOL> layers . append ( ConvPoolLayer ( <EOL> input = X , <EOL> input_shape = ( <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> ) , <EOL> filter_shape = ( <NUM_LIT:32> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> ) , <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> active_func = actfuncs . tanh , <EOL> flatten = False , <EOL> b = <NUM_LIT:0.0> <EOL> ) ) <EOL> layers . append ( ConvPoolLayer ( <EOL> input = layers [ - <NUM_LIT:1> ] . output , <EOL> input_shape = layers [ - <NUM_LIT:1> ] . output_shape , <EOL> filter_shape = ( <NUM_LIT:64> , <NUM_LIT:32> , <NUM_LIT:5> , <NUM_LIT:5> ) , <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> active_func = actfuncs . tanh , <EOL> flatten = False , <EOL> b = <NUM_LIT:0.0> <EOL> ) ) <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> F = layers [ - <NUM_LIT:1> ] . output <EOL> F = shape_constrained_pooling ( F , S . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' , <NUM_LIT:1> , <NUM_LIT:2> ) ) if use_scpool else F . flatten ( <NUM_LIT:2> ) <EOL> layers . append ( FullConnLayer ( <EOL> input = F , <EOL> input_shape = layers [ - <NUM_LIT:1> ] . output_shape [ <NUM_LIT:0> ] , <EOL> output_shape = <NUM_LIT:32> , <EOL> dropout_ratio = <NUM_LIT:0.1> , <EOL> active_func = actfuncs . tanh <EOL> ) ) <EOL> layers . append ( FullConnLayer ( <EOL> input = layers [ - <NUM_LIT:1> ] . output , <EOL> input_shape = layers [ - <NUM_LIT:1> ] . output_shape , <EOL> output_shape = <NUM_LIT:11> , <EOL> dropout_input = layers [ - <NUM_LIT:1> ] . dropout_output , <EOL> active_func = actfuncs . sigmoid <EOL> ) ) <EOL> model = NeuralNet ( layers , [ X , S ] , layers [ - <NUM_LIT:1> ] . output ) <EOL> model . target = A <EOL> model . cost = costfuncs . binxent ( layers [ - <NUM_LIT:1> ] . dropout_output , A ) + <NUM_LIT> * model . get_norm ( <NUM_LIT:2> ) <EOL> model . error = costfuncs . binerr ( layers [ - <NUM_LIT:1> ] . output , A ) <EOL> sgd . train ( model , dataset , lr = <NUM_LIT> , momentum = <NUM_LIT> , <EOL> batch_size = <NUM_LIT:100> , n_epochs = <NUM_LIT> , <EOL> epoch_waiting = <NUM_LIT:10> ) <EOL> return model <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> dataset_file = '<STR_LIT>' . format ( args . dataset [ <NUM_LIT:0> ] ) <EOL> out_file = '<STR_LIT>' if args . output is None else '<STR_LIT>' . format ( args . output ) <EOL> dataset = load_data ( dataset_file ) <EOL> model = train_model ( dataset , not args . no_scpool ) <EOL> save_data ( model , out_file ) </s>
