def load(cls, word_embeddings, base_file, embedding_dim=None, hidden_dim=None, classes_dim=None): pc = dy.ParameterCollection() matrices = dy.load(base_file, pc) # matrices = matrices[1:] # for now, skip "E" return cls(word_embeddings, embedding_dim, hidden_dim, classes_dim, pc, matrices)
def load(self, fname): params = dy.load(fname, self._model) for name, param in zip(self._parameters, params): self.__setattr__(name, param)
def main(): parser = argparse.ArgumentParser( description= 'Deep Recurrent Generative Decoder for Abstractive Text Summarization in DyNet' ) parser.add_argument('--gpu', type=str, default='0', help='GPU ID to use. For cpu, set -1 [default: -]') parser.add_argument('--n_test', type=int, default=189651, help='Number of test examples [default: 189651]') parser.add_argument('--beam_size', type=int, default=5, help='Beam size [default: 5]') parser.add_argument('--max_len', type=int, default=100, help='Maximum length of decoding [default: 100]') parser.add_argument('--model_file', type=str, default='./model_e1', help='Trained model file path [default: ./model_e1]') parser.add_argument( '--input_file', type=str, default='./data/valid.article.filter.txt', help='Test file path [default: ./data/valid.article.filter.txt]') parser.add_argument('--output_file', type=str, default='./pred_y.txt', help='Output file path [default: ./pred_y.txt]') parser.add_argument('--w2i_file', type=str, default='./w2i.dump', help='Word2Index file path [default: ./w2i.dump]') parser.add_argument('--i2w_file', type=str, default='./i2w.dump', help='Index2Word file path [default: ./i2w.dump]') parser.add_argument( '--alloc_mem', type=int, default=1024, help='Amount of memory to allocate [mb] [default: 1024]') args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu N_TEST = args.n_test K = args.beam_size MAX_LEN = args.max_len ALLOC_MEM = args.alloc_mem # File paths MODEL_FILE = args.model_file INPUT_FILE = args.input_file OUTPUT_FILE = args.output_file W2I_FILE = args.w2i_file I2W_FILE = args.i2w_file # DyNet setting dyparams = dy.DynetParams() dyparams.set_autobatch(True) dyparams.set_random_seed(RANDOM_SEED) dyparams.set_mem(ALLOC_MEM) dyparams.init() # Load trained model ============================================================================================== with open(W2I_FILE, 'rb') as f_w2i, open(I2W_FILE, 'rb') as f_i2w: w2i = pickle.load(f_w2i) i2w = pickle.load(f_i2w) test_X, _, _ = build_dataset(INPUT_FILE, w2i=w2i, n_data=N_TEST, target=False) model = dy.Model() V, encoder, decoder = dy.load(MODEL_FILE, model) # Decode pred_y = [] for x in tqdm(test_X): dy.renew_cg() associate_parameters([encoder, decoder]) # Initial states x_embs = [dy.lookup(V, x_t) for x_t in x] hp, hb_1 = encoder(x_embs) decoder.set_initial_states(hp, hb_1) s_0, c_0 = decoder.s_0, decoder.c_0 # candidates candidates = [[0, w2i['<s>'], s_0, c_0, []]] t = 0 while t < MAX_LEN: t += 1 tmp_candidates = [] end_flag = True for score_tm1, y_tm1, s_tm1, c_tm1, y_02tm1 in candidates: if y_tm1 == w2i['</s>']: tmp_candidates.append( [score_tm1, y_tm1, s_tm1, c_tm1, y_02tm1]) else: end_flag = False y_tm1_emb = dy.lookup(V, y_tm1) s_t, c_t, _q_t = decoder(y_tm1_emb, tm1s=[s_tm1, c_tm1], test=True) _q_t = np.log(_q_t.npvalue()) # Calculate log probs q_t, y_t = np.sort(_q_t)[::-1][:K], np.argsort( _q_t )[::-1][:K] # Pick K highest log probs and their ids score_t = score_tm1 + q_t # Accumulate log probs tmp_candidates.extend( [[score_tk, y_tk, s_t, c_t, y_02tm1 + [y_tk]] for score_tk, y_tk in zip(score_t, y_t)]) if end_flag: break candidates = sorted( tmp_candidates, key=lambda x: -x[0] / len(x[-1]) )[:K] # Sort in normalized log probs and pick K highest candidates # Pick the candidate with the highest score pred = candidates[0][-1] if w2i['</s>'] in pred: pred.remove(w2i['</s>']) pred_y.append(pred) pred_y_txt = '' for pred in pred_y: pred_y_txt += ' '.join([i2w[com] for com in pred]) + '\n' with open(OUTPUT_FILE, 'w') as f: f.write(pred_y_txt)
def main(): parser = argparse.ArgumentParser( description= 'A Neural Attention Model for Abstractive Sentence Summarization in DyNet' ) parser.add_argument('--gpu', type=str, default='0', help='GPU ID to use. For cpu, set -1 [default: `-`]') parser.add_argument('--n_test', type=int, default=189651, help='Number of test examples [default: `189651`]') parser.add_argument('--beam_size', type=int, default=5, help='Beam size [default: `5`]') parser.add_argument('--max_len', type=int, default=100, help='Maximum length of decoding [default: `100`]') parser.add_argument('--model_file', type=str, default='./model_e1', help='Trained model file path [default: `./model_e1`]') parser.add_argument( '--input_file', type=str, default='./data/valid.article.filter.txt', help='Test file path [default: `./data/valid.article.filter.txt`]') parser.add_argument('--output_file', type=str, default='./pred_y.txt', help='Output file path [default: `./pred_y.txt`]') parser.add_argument('--w2i_file', type=str, default='./w2i.dump', help='Word2Index file path [default: `./w2i.dump`]') parser.add_argument('--i2w_file', type=str, default='./i2w.dump', help='Index2Word file path [default: `./i2w.dump`]') parser.add_argument( '--alloc_mem', type=int, default=1024, help='Amount of memory to allocate [mb] [default: `1024`]') args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu N_TEST = args.n_test K = args.beam_size MAX_LEN = args.max_len ALLOC_MEM = args.alloc_mem # File paths MODEL_FILE = args.model_file INPUT_FILE = args.input_file OUTPUT_FILE = args.output_file W2I_FILE = args.w2i_file I2W_FILE = args.i2w_file # DyNet setting dyparams = dy.DynetParams() dyparams.set_autobatch(True) dyparams.set_random_seed(RANDOM_STATE) dyparams.set_mem(ALLOC_MEM) dyparams.init() # Load trained model ============================================================================================== with open(W2I_FILE, 'rb') as f_w2i, open(I2W_FILE, 'rb') as f_i2w: w2i = pickle.load(f_w2i) i2w = pickle.load(f_i2w) test_X, _, _ = build_dataset(INPUT_FILE, w2i=w2i, n_data=N_TEST) model = dy.Model() rush_abs = dy.load(MODEL_FILE, model)[0] ENCODER_TYPE = rush_abs.encoder_type C = rush_abs.c # Decode pred_y = [] for x in tqdm(test_X): dy.renew_cg() rush_abs.associate_parameters() # Initial states rush_abs.set_initial_states(x) # [accum log prob, BOS, t_c, decoded sequence] candidates = [[0, w2i['<s>'], [w2i['<s>']] * C, []]] t = 0 while t < MAX_LEN: t += 1 tmp_candidates = [] end_flag = True for score_tm1, y_tm1, y_c, y_02tm1 in candidates: if y_tm1 == w2i['</s>']: tmp_candidates.append([score_tm1, y_tm1, y_c, y_02tm1]) else: end_flag = False _q_t = rush_abs(t=y_c, test=True) _q_t = np.log(_q_t.npvalue()) # Log probs q_t, y_t = np.sort(_q_t)[::-1][:K], np.argsort( _q_t )[::-1][:K] # Pick K highest log probs and their ids score_t = score_tm1 + q_t # Accum log probs tmp_candidates.extend( [[score_tk, y_tk, y_c[1:] + [y_tk], y_02tm1 + [y_tk]] for score_tk, y_tk in zip(score_t, y_t)]) if end_flag: break candidates = sorted( tmp_candidates, key=lambda x: -x[0] / len(x[-1]) )[:K] # Sort in normalized score and pick K highest candidates # Pick the highest-scored candidate pred_y.append(candidates[0][-1]) pred_y_txt = '' for pred in pred_y: pred_y_txt += ' '.join([i2w[com] for com in pred]) + '\n' with open(OUTPUT_FILE, 'w') as f: f.write(pred_y_txt)
def main(): parser = argparse.ArgumentParser(description='Convolutional Neural Networks for Sentence Classification in DyNet') parser.add_argument('--gpu', type=int, default=-1, help='GPU ID to use. For cpu, set -1 [default: -1]') parser.add_argument('--model_file', type=str, default='./model', help='Model to use for prediction [default: ./model]') parser.add_argument('--input_file', type=str, default='./data/valid_x.txt', help='Input file path [default: ./data/valid_x.txt]') parser.add_argument('--output_file', type=str, default='./pred_y.txt', help='Output file path [default: ./pred_y.txt]') parser.add_argument('--w2i_file', type=str, default='./w2i.dump', help='Word2Index file path [default: ./w2i.dump]') parser.add_argument('--i2w_file', type=str, default='./i2w.dump', help='Index2Word file path [default: ./i2w.dump]') parser.add_argument('--alloc_mem', type=int, default=1024, help='Amount of memory to allocate [mb] [default: 1024]') args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu) MODEL_FILE = args.model_file INPUT_FILE = args.input_file OUTPUT_FILE = args.output_file W2I_FILE = args.w2i_file I2W_FILE = args.i2w_file ALLOC_MEM = args.alloc_mem # DyNet setting dyparams = dy.DynetParams() dyparams.set_mem(ALLOC_MEM) dyparams.init() # Load model model = dy.Model() pretrained_model = dy.load(MODEL_FILE, model) if len(pretrained_model) == 3: V1, layers = pretrained_model[0], pretrained_model[1:] MULTICHANNEL = False else: V1, V2, layers = pretrained_model[0], pretrained_model[1], pretrained_model[2:] MULTICHANNEL = True EMB_DIM = V1.shape()[0] WIN_SIZES = layers[0].win_sizes # Load test data with open(W2I_FILE, 'rb') as f_w2i, open(I2W_FILE, 'rb') as f_i2w: w2i = pickle.load(f_w2i) i2w = pickle.load(f_i2w) max_win = max(WIN_SIZES) test_X, _, _ = build_dataset(INPUT_FILE, w2i=w2i, unksym='unk') test_X = [[0]*max_win + instance_x + [0]*max_win for instance_x in test_X] # Pred pred_y = [] for instance_x in tqdm(test_X): # Create a new computation graph dy.renew_cg() associate_parameters(layers) sen_len = len(instance_x) if MULTICHANNEL: x_embs1 = dy.concatenate([dy.lookup(V1, x_t, update=False) for x_t in instance_x], d=1) x_embs2 = dy.concatenate([dy.lookup(V2, x_t, update=False) for x_t in instance_x], d=1) x_embs1 = dy.transpose(x_embs1) x_embs2 = dy.transpose(x_embs2) x_embs = dy.concatenate([x_embs1, x_embs2], d=2) else: x_embs = dy.concatenate([dy.lookup(V1, x_t, update=False) for x_t in instance_x], d=1) x_embs = dy.transpose(x_embs) x_embs = dy.reshape(x_embs, (sen_len, EMB_DIM, 1)) y = f_props(layers, x_embs, train=False) pred_y.append(str(int(binary_pred(y.value())))) with open(OUTPUT_FILE, 'w') as f: f.write('\n'.join(pred_y))
args.model[:args.model.find('Parser') + 6]) config = Configurable(args.config_file, extra_args) dyparams = dy.DynetParams() # dyparams.from_args() # dyparams.set_autobatch(True) dyparams.set_random_seed(666) dyparams.set_mem(5120) dyparams.init() model = dy.ParameterCollection() model_path = config.load_model_path + \ args.model + "_dev={}".format(args.dev_fscore) # model_path = config.load_model_path + "GNNParser2_50epoch" [parser] = dy.load(model_path, model) print("Loaded model from {}".format(model_path)) if args.use_bert: test_bert_embeddings = parser.vocab.load_bert_embeddings( config.test_bert_file) print('Loaded bert embeddings!') testing_data = parser.vocab.gold_data_from_file(config.test_file) print("Loaded testing data from {}".format(config.test_file)) start_time = time.time() test_fscore = test(parser, testing_data, config.evalb_dir, args.unsupervised, test_bert_embeddings) print("test-fscore {} " "test-elapsed {}".format(