<s> '''<STR_LIT>''' <EOL> import os <EOL> import theano <EOL> import theano . tensor as tensor <EOL> import cPickle as pkl <EOL> import numpy <EOL> import copy <EOL> import nltk <EOL> from collections import OrderedDict , defaultdict <EOL> from scipy . linalg import norm <EOL> from nltk . tokenize import word_tokenize <EOL> profile = False <EOL> path_to_models = '<STR_LIT>' <EOL> path_to_tables = '<STR_LIT>' <EOL> path_to_umodel = path_to_models + '<STR_LIT>' <EOL> path_to_bmodel = path_to_models + '<STR_LIT>' <EOL> def load_model ( ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' <EOL> with open ( '<STR_LIT>' % path_to_umodel , '<STR_LIT:rb>' ) as f : <EOL> uoptions = pkl . load ( f ) <EOL> with open ( '<STR_LIT>' % path_to_bmodel , '<STR_LIT:rb>' ) as f : <EOL> boptions = pkl . load ( f ) <EOL> uparams = init_params ( uoptions ) <EOL> uparams = load_params ( path_to_umodel , uparams ) <EOL> utparams = init_tparams ( uparams ) <EOL> bparams = init_params_bi ( boptions ) <EOL> bparams = load_params ( path_to_bmodel , bparams ) <EOL> btparams = init_tparams ( bparams ) <EOL> print '<STR_LIT>' <EOL> embedding , x_mask , ctxw2v = build_encoder ( utparams , uoptions ) <EOL> f_w2v = theano . function ( [ embedding , x_mask ] , ctxw2v , name = '<STR_LIT>' ) <EOL> embedding , x_mask , ctxw2v = build_encoder_bi ( btparams , boptions ) <EOL> f_w2v2 = theano . function ( [ embedding , x_mask ] , ctxw2v , name = '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> utable , btable = load_tables ( ) <EOL> print '<STR_LIT>' <EOL> model = { } <EOL> model [ '<STR_LIT>' ] = uoptions <EOL> model [ '<STR_LIT>' ] = boptions <EOL> model [ '<STR_LIT>' ] = utable <EOL> model [ '<STR_LIT>' ] = btable <EOL> model [ '<STR_LIT>' ] = f_w2v <EOL> model [ '<STR_LIT>' ] = f_w2v2 <EOL> return model <EOL> def load_tables ( ) : <EOL> """<STR_LIT>""" <EOL> words = [ ] <EOL> utable = numpy . load ( path_to_tables + '<STR_LIT>' ) <EOL> btable = numpy . load ( path_to_tables + '<STR_LIT>' ) <EOL> f = open ( path_to_tables + '<STR_LIT>' , '<STR_LIT:rb>' ) <EOL> for line in f : <EOL> words . append ( line . decode ( '<STR_LIT:utf-8>' ) . strip ( ) ) <EOL> f . close ( ) <EOL> utable = OrderedDict ( zip ( words , utable ) ) <EOL> btable = OrderedDict ( zip ( words , btable ) ) <EOL> return utable , btable <EOL> def encode ( model , X , use_norm = True , verbose = True , batch_size = <NUM_LIT> , use_eos = False ) : <EOL> """<STR_LIT>""" <EOL> X = preprocess ( X ) <EOL> d = defaultdict ( lambda : <NUM_LIT:0> ) <EOL> for w in model [ '<STR_LIT>' ] . keys ( ) : <EOL> d [ w ] = <NUM_LIT:1> <EOL> ufeatures = numpy . zeros ( ( len ( X ) , model [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , dtype = '<STR_LIT>' ) <EOL> bfeatures = numpy . zeros ( ( len ( X ) , <NUM_LIT:2> * model [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , dtype = '<STR_LIT>' ) <EOL> ds = defaultdict ( list ) <EOL> captions = [ s . split ( ) for s in X ] <EOL> for i , s in enumerate ( captions ) : <EOL> ds [ len ( s ) ] . append ( i ) <EOL> for k in ds . keys ( ) : <EOL> if verbose : <EOL> print k <EOL> numbatches = len ( ds [ k ] ) / batch_size + <NUM_LIT:1> <EOL> for minibatch in range ( numbatches ) : <EOL> caps = ds [ k ] [ minibatch : : numbatches ] <EOL> if use_eos : <EOL> uembedding = numpy . zeros ( ( k + <NUM_LIT:1> , len ( caps ) , model [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , dtype = '<STR_LIT>' ) <EOL> bembedding = numpy . zeros ( ( k + <NUM_LIT:1> , len ( caps ) , model [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , dtype = '<STR_LIT>' ) <EOL> else : <EOL> uembedding = numpy . zeros ( ( k , len ( caps ) , model [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , dtype = '<STR_LIT>' ) <EOL> bembedding = numpy . zeros ( ( k , len ( caps ) , model [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , dtype = '<STR_LIT>' ) <EOL> for ind , c in enumerate ( caps ) : <EOL> caption = captions [ c ] <EOL> for j in range ( len ( caption ) ) : <EOL> if d [ caption [ j ] ] > <NUM_LIT:0> : <EOL> uembedding [ j , ind ] = model [ '<STR_LIT>' ] [ caption [ j ] ] <EOL> bembedding [ j , ind ] = model [ '<STR_LIT>' ] [ caption [ j ] ] <EOL> else : <EOL> uembedding [ j , ind ] = model [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> bembedding [ j , ind ] = model [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if use_eos : <EOL> uembedding [ - <NUM_LIT:1> , ind ] = model [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> bembedding [ - <NUM_LIT:1> , ind ] = model [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if use_eos : <EOL> uff = model [ '<STR_LIT>' ] ( uembedding , numpy . ones ( ( len ( caption ) + <NUM_LIT:1> , len ( caps ) ) , dtype = '<STR_LIT>' ) ) <EOL> bff = model [ '<STR_LIT>' ] ( bembedding , numpy . ones ( ( len ( caption ) + <NUM_LIT:1> , len ( caps ) ) , dtype = '<STR_LIT>' ) ) <EOL> else : <EOL> uff = model [ '<STR_LIT>' ] ( uembedding , numpy . ones ( ( len ( caption ) , len ( caps ) ) , dtype = '<STR_LIT>' ) ) <EOL> bff = model [ '<STR_LIT>' ] ( bembedding , numpy . ones ( ( len ( caption ) , len ( caps ) ) , dtype = '<STR_LIT>' ) ) <EOL> if use_norm : <EOL> for j in range ( len ( uff ) ) : <EOL> uff [ j ] /= norm ( uff [ j ] ) <EOL> bff [ j ] /= norm ( bff [ j ] ) <EOL> for ind , c in enumerate ( caps ) : <EOL> ufeatures [ c ] = uff [ ind ] <EOL> bfeatures [ c ] = bff [ ind ] <EOL> features = numpy . c_ [ ufeatures , bfeatures ] <EOL> return features <EOL> def preprocess ( text ) : <EOL> """<STR_LIT>""" <EOL> X = [ ] <EOL> sent_detector = nltk . data . load ( '<STR_LIT>' ) <EOL> for t in text : <EOL> sents = sent_detector . tokenize ( t ) <EOL> result = '<STR_LIT>' <EOL> for s in sents : <EOL> tokens = word_tokenize ( s ) <EOL> result += '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>' . join ( tokens ) <EOL> X . append ( result ) <EOL> return X <EOL> def nn ( model , text , vectors , query , k = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> qf = encode ( model , [ query ] ) <EOL> qf /= norm ( qf ) <EOL> scores = numpy . dot ( qf , vectors . T ) . flatten ( ) <EOL> sorted_args = numpy . argsort ( scores ) [ : : - <NUM_LIT:1> ] <EOL> sentences = [ text [ a ] for a in sorted_args [ : k ] ] <EOL> print '<STR_LIT>' + query <EOL> print '<STR_LIT>' <EOL> for i , s in enumerate ( sentences ) : <EOL> print s , sorted_args [ i ] <EOL> def word_features ( table ) : <EOL> """<STR_LIT>""" <EOL> features = numpy . zeros ( ( len ( table ) , <NUM_LIT> ) , dtype = '<STR_LIT>' ) <EOL> keys = table . keys ( ) <EOL> for i in range ( len ( table ) ) : <EOL> f = table [ keys [ i ] ] <EOL> features [ i ] = f / norm ( f ) <EOL> return features <EOL> def nn_words ( table , wordvecs , query , k = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> keys = table . keys ( ) <EOL> qf = table [ query ] <EOL> scores = numpy . dot ( qf , wordvecs . T ) . flatten ( ) <EOL> sorted_args = numpy . argsort ( scores ) [ : : - <NUM_LIT:1> ] <EOL> words = [ keys [ a ] for a in sorted_args [ : k ] ] <EOL> print '<STR_LIT>' + query <EOL> print '<STR_LIT>' <EOL> for i , w in enumerate ( words ) : <EOL> print w <EOL> def _p ( pp , name ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( pp , name ) <EOL> def init_tparams ( params ) : <EOL> """<STR_LIT>""" <EOL> tparams = OrderedDict ( ) <EOL> for kk , pp in params . iteritems ( ) : <EOL> tparams [ kk ] = theano . shared ( params [ kk ] , name = kk ) <EOL> return tparams <EOL> def load_params ( path , params ) : <EOL> """<STR_LIT>""" <EOL> pp = numpy . load ( path ) <EOL> for kk , vv in params . iteritems ( ) : <EOL> if kk not in pp : <EOL> warnings . warn ( '<STR_LIT>' % kk ) <EOL> continue <EOL> params [ kk ] = pp [ kk ] <EOL> return params <EOL> layers = { '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) } <EOL> def get_layer ( name ) : <EOL> fns = layers [ name ] <EOL> return ( eval ( fns [ <NUM_LIT:0> ] ) , eval ( fns [ <NUM_LIT:1> ] ) ) <EOL> def init_params ( options ) : <EOL> """<STR_LIT>""" <EOL> params = OrderedDict ( ) <EOL> params [ '<STR_LIT>' ] = norm_weight ( options [ '<STR_LIT>' ] , options [ '<STR_LIT>' ] ) <EOL> params = get_layer ( options [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] ( options , params , prefix = '<STR_LIT>' , <EOL> nin = options [ '<STR_LIT>' ] , dim = options [ '<STR_LIT>' ] ) <EOL> return params <EOL> def init_params_bi ( options ) : <EOL> """<STR_LIT>""" <EOL> params = OrderedDict ( ) <EOL> params [ '<STR_LIT>' ] = norm_weight ( options [ '<STR_LIT>' ] , options [ '<STR_LIT>' ] ) <EOL> params = get_layer ( options [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] ( options , params , prefix = '<STR_LIT>' , <EOL> nin = options [ '<STR_LIT>' ] , dim = options [ '<STR_LIT>' ] ) <EOL> params = get_layer ( options [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] ( options , params , prefix = '<STR_LIT>' , <EOL> nin = options [ '<STR_LIT>' ] , dim = options [ '<STR_LIT>' ] ) <EOL> return params <EOL> def build_encoder ( tparams , options ) : <EOL> """<STR_LIT>""" <EOL> embedding = tensor . tensor3 ( '<STR_LIT>' , dtype = '<STR_LIT>' ) <EOL> x_mask = tensor . matrix ( '<STR_LIT>' , dtype = '<STR_LIT>' ) <EOL> proj = get_layer ( options [ '<STR_LIT>' ] ) [ <NUM_LIT:1> ] ( tparams , embedding , options , <EOL> prefix = '<STR_LIT>' , <EOL> mask = x_mask ) <EOL> ctx = proj [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] <EOL> return embedding , x_mask , ctx <EOL> def build_encoder_bi ( tparams , options ) : <EOL> """<STR_LIT>""" <EOL> embedding = tensor . tensor3 ( '<STR_LIT>' , dtype = '<STR_LIT>' ) <EOL> embeddingr = embedding [ : : - <NUM_LIT:1> ] <EOL> x_mask = tensor . matrix ( '<STR_LIT>' , dtype = '<STR_LIT>' ) <EOL> xr_mask = x_mask [ : : - <NUM_LIT:1> ] <EOL> proj = get_layer ( options [ '<STR_LIT>' ] ) [ <NUM_LIT:1> ] ( tparams , embedding , options , <EOL> prefix = '<STR_LIT>' , <EOL> mask = x_mask ) <EOL> projr = get_layer ( options [ '<STR_LIT>' ] ) [ <NUM_LIT:1> ] ( tparams , embeddingr , options , <EOL> prefix = '<STR_LIT>' , <EOL> mask = xr_mask ) <EOL> ctx = tensor . concatenate ( [ proj [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] , projr [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] ] , axis = <NUM_LIT:1> ) <EOL> return embedding , x_mask , ctx <EOL> def ortho_weight ( ndim ) : <EOL> W = numpy . random . randn ( ndim , ndim ) <EOL> u , s , v = numpy . linalg . svd ( W ) <EOL> return u . astype ( '<STR_LIT>' ) <EOL> def norm_weight ( nin , nout = None , scale = <NUM_LIT:0.1> , ortho = True ) : <EOL> if nout == None : <EOL> nout = nin <EOL> if nout == nin and ortho : <EOL> W = ortho_weight ( nin ) <EOL> else : <EOL> W = numpy . random . uniform ( low = - scale , high = scale , size = ( nin , nout ) ) <EOL> return W . astype ( '<STR_LIT>' ) <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 , 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 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 = [ tensor . alloc ( <NUM_LIT:0.> , n_samples , dim ) ] , <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 = profile , <EOL> strict = True ) <EOL> rval = [ rval ] <EOL> return rval </s>
