<s> """<STR_LIT>""" <EOL> import theano <EOL> import theano . tensor as tensor <EOL> import numpy <EOL> from utils import _p , ortho_weight , norm_weight , tanh , linear <EOL> layers = { '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> def get_layer ( name ) : <EOL> """<STR_LIT>""" <EOL> fns = layers [ name ] <EOL> return ( eval ( fns [ <NUM_LIT:0> ] ) , eval ( fns [ <NUM_LIT:1> ] ) ) <EOL> def param_init_fflayer ( options , params , prefix = '<STR_LIT>' , nin = None , nout = None , ortho = True ) : <EOL> """<STR_LIT>""" <EOL> if nin == None : <EOL> nin = options [ '<STR_LIT>' ] <EOL> if nout == None : <EOL> nout = options [ '<STR_LIT>' ] <EOL> params [ _p ( prefix , '<STR_LIT>' ) ] = norm_weight ( nin , nout , ortho = ortho ) <EOL> params [ _p ( prefix , '<STR_LIT:b>' ) ] = numpy . zeros ( ( nout , ) ) . astype ( '<STR_LIT>' ) <EOL> return params <EOL> def fflayer ( tparams , state_below , options , prefix = '<STR_LIT>' , activ = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return eval ( activ ) ( tensor . dot ( state_below , tparams [ _p ( prefix , '<STR_LIT>' ) ] ) + tparams [ _p ( prefix , '<STR_LIT:b>' ) ] ) <EOL> def param_init_gru ( options , params , prefix = '<STR_LIT>' , nin = None , dim = None ) : <EOL> """<STR_LIT>""" <EOL> if nin == None : <EOL> nin = options [ '<STR_LIT>' ] <EOL> if dim == None : <EOL> dim = options [ '<STR_LIT>' ] <EOL> W = numpy . concatenate ( [ norm_weight ( nin , dim ) , <EOL> norm_weight ( nin , dim ) ] , axis = <NUM_LIT:1> ) <EOL> params [ _p ( prefix , '<STR_LIT>' ) ] = W <EOL> params [ _p ( prefix , '<STR_LIT:b>' ) ] = numpy . zeros ( ( <NUM_LIT:2> * dim , ) ) . astype ( '<STR_LIT>' ) <EOL> U = numpy . concatenate ( [ ortho_weight ( dim ) , <EOL> ortho_weight ( dim ) ] , axis = <NUM_LIT:1> ) <EOL> params [ _p ( prefix , '<STR_LIT>' ) ] = U <EOL> Wx = norm_weight ( nin , dim ) <EOL> params [ _p ( prefix , '<STR_LIT>' ) ] = Wx <EOL> Ux = ortho_weight ( dim ) <EOL> params [ _p ( prefix , '<STR_LIT>' ) ] = Ux <EOL> params [ _p ( prefix , '<STR_LIT>' ) ] = numpy . zeros ( ( dim , ) ) . astype ( '<STR_LIT>' ) <EOL> return params <EOL> def gru_layer ( tparams , state_below , init_state , options , prefix = '<STR_LIT>' , mask = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> nsteps = state_below . shape [ <NUM_LIT:0> ] <EOL> if state_below . ndim == <NUM_LIT:3> : <EOL> n_samples = state_below . shape [ <NUM_LIT:1> ] <EOL> else : <EOL> n_samples = <NUM_LIT:1> <EOL> dim = tparams [ _p ( prefix , '<STR_LIT>' ) ] . shape [ <NUM_LIT:1> ] <EOL> if init_state == None : <EOL> init_state = tensor . alloc ( <NUM_LIT:0.> , n_samples , dim ) <EOL> if mask == None : <EOL> mask = tensor . alloc ( <NUM_LIT:1.> , state_below . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> def _slice ( _x , n , dim ) : <EOL> if _x . ndim == <NUM_LIT:3> : <EOL> return _x [ : , : , n * dim : ( n + <NUM_LIT:1> ) * dim ] <EOL> return _x [ : , n * dim : ( n + <NUM_LIT:1> ) * dim ] <EOL> state_below_ = tensor . dot ( state_below , tparams [ _p ( prefix , '<STR_LIT>' ) ] ) + tparams [ _p ( prefix , '<STR_LIT:b>' ) ] <EOL> state_belowx = tensor . dot ( state_below , tparams [ _p ( prefix , '<STR_LIT>' ) ] ) + tparams [ _p ( prefix , '<STR_LIT>' ) ] <EOL> U = tparams [ _p ( prefix , '<STR_LIT>' ) ] <EOL> Ux = tparams [ _p ( prefix , '<STR_LIT>' ) ] <EOL> def _step_slice ( m_ , x_ , xx_ , h_ , U , Ux ) : <EOL> preact = tensor . dot ( h_ , U ) <EOL> preact += x_ <EOL> r = tensor . nnet . sigmoid ( _slice ( preact , <NUM_LIT:0> , dim ) ) <EOL> u = tensor . nnet . sigmoid ( _slice ( preact , <NUM_LIT:1> , dim ) ) <EOL> preactx = tensor . dot ( h_ , Ux ) <EOL> preactx = preactx * r <EOL> preactx = preactx + xx_ <EOL> h = tensor . tanh ( preactx ) <EOL> h = u * h_ + ( <NUM_LIT:1.> - u ) * h <EOL> h = m_ [ : , None ] * h + ( <NUM_LIT:1.> - m_ ) [ : , None ] * h_ <EOL> return h <EOL> seqs = [ mask , state_below_ , state_belowx ] <EOL> _step = _step_slice <EOL> rval , updates = theano . scan ( _step , <EOL> sequences = seqs , <EOL> outputs_info = [ init_state ] , <EOL> non_sequences = [ tparams [ _p ( prefix , '<STR_LIT>' ) ] , <EOL> tparams [ _p ( prefix , '<STR_LIT>' ) ] ] , <EOL> name = _p ( prefix , '<STR_LIT>' ) , <EOL> n_steps = nsteps , <EOL> profile = False , <EOL> strict = True ) <EOL> rval = [ rval ] <EOL> return rval </s>
