def load_model(trainf, model_name, weights_file): params = [] module = importlib.import_module('.'+modelname, 'models') conf, ps, h = anssel_train.config(module.config, params) s0, s1, y, vocab, gr = anssel_train.load_set(trainf) model = anssel_train.build_model(glove, vocab, module.prep_model, conf) model.load_weights(weights_file) return model, vocab
def load_model(trainf, model_name, weights_file): params = [] module = importlib.import_module('.' + modelname, 'models') conf, ps, h = anssel_train.config(module.config, params) s0, s1, y, vocab, gr = anssel_train.load_set(trainf) model = anssel_train.build_model(glove, vocab, module.prep_model, conf) model.load_weights(weights_file) return model, vocab
def get_score(): if (request.json['atext'] == []): return jsonify({'score': []}), 200 f = tempfile.NamedTemporaryFile(mode='w') # FIXME: Avoid temporary files!!! write_csv(f.file, load_samples(request.json['qtext'], request.json['atext'])) f.file.close() s0t, s1t, yt, _, grt = anssel_train.load_set(f.name, vocab, skip_oneclass=False) res = model.predict(grt)['score'][:,0] return jsonify({'score': res.tolist()}), 200
def get_score(): if (request.json['atext'] == []): return jsonify({'score': []}), 200 f = tempfile.NamedTemporaryFile(mode='w') # FIXME: Avoid temporary files!!! write_csv(f.file, load_samples(request.json['qtext'], request.json['atext'])) f.file.close() s0t, s1t, yt, _, grt = anssel_train.load_set(f.name, vocab, skip_oneclass=False) res = model.predict(grt)['score'][:, 0] return jsonify({'score': res.tolist()}), 200
print('Predict&Eval (best epoch)') model.load_weights('weights-'+runid+'-bestval.h5') ev.eval_anssel(model.predict(grv)['score'][:,0], grv['si0'], grv['score'], 'anssel Val') if __name__ == "__main__": modelname, weightsf, vocabf, trainf, valf = sys.argv[1:6] params = sys.argv[6:] module = importlib.import_module('.'+modelname, 'models') conf, ps, h = config(module.config, params) runid = '%s-%x' % (modelname, h) print('RunID: %s (%s)' % (runid, ps)) print('GloVe') glove = emb.GloVe(N=conf['embdim']) print('Dataset (vocab)') vocab = pickle.load(open(vocabf, "rb")) # use plain pickle because unicode print('Dataset (anssel train)') s0, s1, y, _, gr_ = anssel_train.load_set(trainf, vocab, s0pad=s0pad, s1pad=s1pad) gr = loader.graph_adapt_ubuntu(gr_, vocab) print('Dataset (anssel val)') s0v, s1v, yv, _, grv_ = anssel_train.load_set(valf, vocab, s0pad=s0pad, s1pad=s1pad) grv = loader.graph_adapt_ubuntu(grv_, vocab) transfer_eval(runid, weightsf, module.prep_model, conf, glove, vocab, gr, grv)
print('%d 0 %d 1 %f %s' % (n, m, iy, code), file=f) m += 1 if __name__ == "__main__": modelname, weightsfile, trainf, valf, trec_qrels_file, trec_top_file = sys.argv[1:7] params = sys.argv[7:] module = importlib.import_module('.'+modelname, 'models') conf, ps, h = anssel_train.config(module.config, params) print('GloVe') glove = emb.GloVe(N=conf['embdim']) print('Dataset') s0, s1, y, vocab, gr = anssel_train.load_set(trainf) s0t, s1t, yt, _, grt = anssel_train.load_set(valf, vocab) print('Model') model = anssel_train.build_model(glove, vocab, module.prep_model, conf) print('Weights') model.load_weights(weightsfile) print('Prediction') ypred = model.predict(gr)['score'][:,0] ypredt = model.predict(grt)['score'][:,0] ev.eval_anssel(ypred, s0, y, trainf) ev.eval_anssel(ypredt, s0t, yt, valf)
from keras.layers.recurrent import SimpleRNN, GRU, LSTM import pysts.embedding as emb import pysts.eval as ev import pysts.kerasts.blocks as B from pysts.kerasts.callbacks import AnsSelCB from pysts.kerasts.objectives import ranknet from pysts.hyperparam import RandomSearch import anssel_train if __name__ == "__main__": modelname, trainf, valf = sys.argv[1:4] module = importlib.import_module('.' + modelname, 'models') s0, s1, y, vocab, gr = anssel_train.load_set(trainf) s0t, s1t, yt, _, grt = anssel_train.load_set(valf, vocab) glove = emb.GloVe(300) # XXX hardcoded N rs = RandomSearch(modelname + '_rlog.txt', dropout=[1 / 2, 2 / 3, 3 / 4], inp_e_dropout=[1 / 2, 3 / 4, 4 / 5], l2reg=[1e-4, 1e-3, 1e-2], cnnact=['tanh', 'tanh', 'relu'], cnninit=['glorot_uniform', 'glorot_uniform', 'normal'], cdim={ 1: [0, 0, 1 / 2, 1, 2], 2: [0, 0, 1 / 2, 1, 2, 0], 3: [0, 0, 1 / 2, 1, 2, 0], 4: [0, 0, 1 / 2, 1, 2, 0],
if __name__ == "__main__": niter, modelname, trainf, valf, testf = sys.argv[1:6] params = sys.argv[6:] niter = int(niter) module = importlib.import_module('.'+modelname, 'models') conf, ps, h = anssel_train.config(module.config, params) if conf['embdim'] is not None: print('GloVe') glove = emb.GloVe(N=conf['embdim']) else: glove = None print('Dataset') s0, s1, y, vocab, gr = anssel_train.load_set(trainf) s0v, s1v, yv, _, grv = anssel_train.load_set(valf, vocab) s0t, s1t, yt, _, grt = anssel_train.load_set(testf, vocab) mrr = [] mrrv = [] mrrt = [] mapt = [] for i in range(niter): runid = '%s-%x-%02d' % (modelname, h, i) print('RunID: %s (%s)' % (runid, ps)) model = anssel_train.train_and_eval(runid, module.prep_model, conf, glove, vocab, gr, s0, grv, s0v, do_eval=False) print('Predict&Eval (best val epoch)') ypred = model.predict(gr)['score'][:,0]