def __init__(self, verbose=False, random_seed=None, memory=512, autobatch=False, use_gpu=False): """ Create an empty instance for Cube Before it can be used, you must call @method load with @param language_code set to your target language """ self._loaded = False self._verbose = verbose import dynet_config if random_seed != None: if not isinstance(random_seed, int): raise Exception ("Random seed must be an integer!") if random_seed == 0: print("[Warning] While Python and Numpy's seeds are now set to 0, DyNet uses 0 to reset the seed generator (fully random). Use any non-zero int value to set DyNet to a fixed random seed.") # set python random seed import random random.seed(random_seed) #set numpy random seed import numpy as np np.random.seed(random_seed) else: random_seed = 0 # this is the default value for DyNet (meaning full random) dynet_config.set(mem=memory, random_seed=random_seed, autobatch=autobatch) if use_gpu: dynet_config.set_gpu()
def get_dynet(): if bool(os.environ['DYNET_USE_GPU']): dynet_config.set_gpu() dynet_config.set(mem=os.environ['DYNET_MEM'], random_seed=int(os.environ['DYNET_SEED']), autobatch=int(os.environ['DYNET_AUTOBATCH']), requested_gpus=int(os.environ['DYNET_GPUS'])) log.info(f"DyNet config: {dynet_config.get()}") import dynet as dy return dy
def set_dynet_arguments(self): self.random.seed(self.args.seed) kwargs = dict(random_seed=self.args.seed) if self.args.dynet_mem: kwargs.update(mem=self.args.dynet_mem) if self.args.dynet_weight_decay: kwargs.update(weight_decay=self.args.dynet_weight_decay) if self.args.dynet_gpus and self.args.dynet_gpus != 1: kwargs.update(requested_gpus=self.args.dynet_gpus) if self.args.dynet_autobatch: kwargs.update(autobatch=True) dynet_config.set(**kwargs) if self.args.dynet_gpu: dynet_config.set_gpu()
def __init__(self): self.use_gpu = True if self.use_gpu: dynet_config.set_gpu() from networks import RRTNetwork self.name = 'rlt_model' self.param_path = 'rlt_model' self.mode = 'train' self.train_set_path = None self.dev_set_path = None self.test_set_path = None self.test_set_path2 = None self.test_conllu_gold_path = None self.dev_ud = None self.gold_ud = None self.conll_format = const.CONLL06 self.wembpath = None self.tune_las = False self.use_bilstm_input = False self.in_lstm_size = 256 self.in_lstm_count = 2 self.hid_mlp_size = 256 self.hid_mlp_count = 1 self.encoding_type = 'forward' self.hid_lstm_size = 512 self.hid_lstm_count = 2 self.batch_size = 10 self.stack_feats = 4 self.buffer_feats = 4 self.min_iter_for_test = 0 self.iter_per_test = 500 self.max_epochs = 30 self.nn = RRTNetwork() self.exp_data = None self._session = None self._load_model_path = None
dest='model_base', help='Location where to store the model') parser.add_option("--patience", action='store', dest='patience', default='20', type='int', help='Early stopping condition') (params, _) = parser.parse_args(sys.argv) memory = int(params.memory) if params.autobatch: autobatch = True else: autobatch = False dynet_config.set(mem=memory, random_seed=9, autobatch=autobatch) if params.gpu: dynet_config.set_gpu() import dynet as dy if params.train: train(params) elif params.test: test(params) elif params.convert: convert(params) else: print("Invalid parameters. Use '--help' for help")
def main(): # Configuration file processing argparser = argparse.ArgumentParser() argparser.add_argument('--config_file', default='../configs/debug.cfg') argparser.add_argument('--continue_training', action='store_true', help='Load model Continue Training') argparser.add_argument('--name', default='experiment', help='The name of the experiment.') argparser.add_argument('--model', default='s2s', help='s2s: seq2seq-head-selection-model' 's2tDFS: seq2tree-DFS-decoder-model') argparser.add_argument('--gpu', default='0', help='GPU ID (-1 to cpu)') args, extra_args = argparser.parse_known_args() cfg = IniConfigurator(args.config_file, extra_args) # Logger setting logger = dual_channel_logger( __name__, file_path=cfg.LOG_FILE, file_model='w', formatter='%(asctime)s - %(levelname)s - %(message)s', time_formatter='%m-%d %H:%M') from eval.script_evaluator import ScriptEvaluator # DyNet setting os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu import dynet_config dynet_config.set(mem=cfg.DYNET_MEM, random_seed=cfg.DYNET_SEED) dynet_config.set_gpu() import dynet as dy from models.token_representation import TokenRepresentation from antu.nn.dynet.seq2seq_encoders import DeepBiRNNBuilder, orthonormal_VanillaLSTMBuilder from models.graph_nn_decoder import GraphNNDecoder from models.jackknife_decoder import JackKnifeGraphNNDecoder # Build the dataset of the training process # Build data reader data_reader = PTBReader( field_list=['word', 'tag', 'head', 'rel'], root='0\t**root**\t_\t**rcpos**\t**rpos**\t_\t0\t**rrel**\t_\t_', spacer=r'[\t]',) # Build vocabulary with pretrained glove vocabulary = Vocabulary() g_word, _ = glove_reader(cfg.GLOVE) pretrained_vocabs = {'glove': g_word} vocabulary.extend_from_pretrained_vocab(pretrained_vocabs) # Setup datasets datasets_settings = {'train': DatasetSetting(cfg.TRAIN, True), 'dev': DatasetSetting(cfg.DEV, False), 'test': DatasetSetting(cfg.TEST, False), } datasets = PTBDataset(vocabulary, datasets_settings, data_reader) counters = {'word': Counter(), 'tag': Counter(), 'rel': Counter()} datasets.build_dataset(counters, no_pad_namespace={'rel'}, no_unk_namespace={'rel'}) # Build model # Parameter pc = dy.ParameterCollection() LR = 0.0005 trainer = dy.AdamTrainer(pc, LR, cfg.ADAM_BETA1, cfg.ADAM_BETA2, cfg.EPS) # Token Representation Layer token_repre = TokenRepresentation(pc, cfg, datasets.vocabulary, include_pos=True) # BiLSTM Encoder Layer #encoder = BiaffineAttention() #encoder = MultiHeadedAttention(pc, 10, token_repre.token_dim) #encoder = MultiLayerMultiHeadAttention(pc, 10, token_repre.token_dim, num_layers=1) #encoder = MyMultiHeadAttention(None, 6, token_repre.token_dim, 32, 32, model=pc) #encoder = LabelAttention(None, token_repre.token_dim, 128, 128, 112, 128, use_resdrop=True, q_as_matrix=False, residual_dropout=0.1, attention_dropout=0.1, d_positional=None, model=pc) # encoder = Encoder(None, token_repre.token_dim, # num_layers=1, num_heads=2, d_kv = 32, d_ff=1024, d_l=112, # d_positional=None, # num_layers_position_only=0, # relu_dropout=0.1, residual_dropout=0.1, attention_dropout=0.1, # use_lal=True, # lal_d_kv=128, # lal_d_proj=128, # lal_resdrop=True, # lal_pwff=True, # lal_q_as_matrix=False, # lal_partitioned=True, # model=pc) #encoder = ScaledDotProductAttention(pc, 10) encoder = DeepBiRNNBuilder(pc, cfg.ENC_LAYERS, token_repre.token_dim, cfg.ENC_H_DIM, orthonormal_VanillaLSTMBuilder) # GNN Decoder Layer decoder = GraphNNDecoder(pc, cfg, datasets.vocabulary) #decoder = JackKnifeGraphNNDecoder(pc, cfg, datasets.vocabulary) # PTB Evaluator my_eval = ScriptEvaluator(['Valid', 'Test'], datasets.vocabulary) #dy.save(cfg.LAST_FILE, [token_repre, encoder, decoder]) #exit(0) # Build Training Batch def cmp(ins): return len(ins['word']) train_batch = datasets.get_batches('train', cfg.TRAIN_BATCH_SIZE, True, cmp, True) valid_batch = list(datasets.get_batches('dev', cfg.TEST_BATCH_SIZE, False, cmp, False)) test_batch = list(datasets.get_batches('test', cfg.TEST_BATCH_SIZE, False, cmp, False)) #print('-----------------------') # print('TRAIN BATCH IS: ') # # print(train_batch) # indexes, masks, truth = train_batch.__next__() # print(indexes) # print('------------------',end='\n\n\n\n\n\n\n') # print(len(indexes)) # exit(0) # exit(0) # for k in indexes: # print(k) #print(indexes) #print(masks) # Train model BEST_DEV_LAS = BEST_DEV_UAS = BEST_ITER = 0 cnt_iter = -cfg.WARM * cfg.GRAPH_LAYERS valid_loss = [[] for i in range(cfg.GRAPH_LAYERS+3)] logger.info("Experiment name: %s" % args.name) SHA = os.popen('git log -1 | head -n 1 | cut -c 8-13').readline().rstrip() logger.info('Git SHA: %s' % SHA) while cnt_iter < cfg.MAX_ITER: print(cnt_iter, cfg.MAX_ITER) #dy.renew_cg() dy.renew_cg(immediate_compute = True, check_validity = True) cnt_iter += 1 indexes, masks, truth = train_batch.__next__() vectors = token_repre(indexes, True) #vectors = encoder(vectors, np.array(masks['1D']).T) #print(vectors.npvalue) #vectors= encoder(vectors, vectors, vectors, np.array(masks['1D']).T) #vectors= encoder(vectors, vectors, vectors, np.array(masks['1D']).T, cfg.RNN_DROP) vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False, True) loss, part_loss = decoder(vectors, masks, truth, cnt_iter, True, True) for i, l in enumerate([loss]+part_loss): valid_loss[i].append(l.value()) loss.backward() trainer.learning_rate = LR*cfg.LR_DECAY**(max(cnt_iter, 0)/cfg.LR_ANNEAL) #trainer.learning_rate = cfg.LR*cfg.LR_DECAY**(max(cnt_iter, 0)/cfg.LR_ANNEAL) trainer.update() if cnt_iter % cfg.VALID_ITER: continue # Validation for i in range(len(valid_loss)): valid_loss[i] = str(round(np.mean(valid_loss[i]), 2)) avg_loss = ', '.join(valid_loss) logger.info("") logger.info("Iter: %d-%d, Avg_loss: %s, LR (%f), Best (%d)" % (cnt_iter/cfg.VALID_ITER, cnt_iter, avg_loss, trainer.learning_rate, BEST_ITER)) valid_loss = [[] for i in range(cfg.GRAPH_LAYERS+3)] my_eval.clear('Valid') for indexes, masks, truth in valid_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, np.array(masks['1D']).T) #vectors= encoder(vectors, vectors, vectors, np.array(masks['1D']).T) #vectors = encoder(vectors, vectors, vectors, np.array(masks['1D']).T, cfg.RNN_DROP) #vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False, False) pred = decoder(vectors, masks, None, cnt_iter, False, True) my_eval.add_truth('Valid', truth) my_eval.add_pred('Valid', pred) dy.save(cfg.LAST_FILE, [token_repre, encoder, decoder]) if my_eval.evaluation('Valid', cfg.PRED_DEV, cfg.DEV): BEST_ITER = cnt_iter/cfg.VALID_ITER os.system('cp %s.data %s.data' % (cfg.LAST_FILE, cfg.BEST_FILE)) os.system('cp %s.meta %s.meta' % (cfg.LAST_FILE, cfg.BEST_FILE)) # Just record test result my_eval.clear('Test') for indexes, masks, truth in test_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, np.array(masks['1D']).T) #vectors= encoder(vectors, vectors, vectors, np.array(masks['1D']).T) #vectors = encoder(vectors, vectors, vectors, np.array(masks['1D']).T, cfg.RNN_DROP) #vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False, False) pred = decoder(vectors, masks, None, cnt_iter, False, True) my_eval.add_truth('Test', truth) my_eval.add_pred('Test', pred) my_eval.evaluation('Test', cfg.PRED_TEST, cfg.TEST) my_eval.print_best_result('Valid') # Final Test test_pc = dy.ParameterCollection() token_repre, encoder, decoder = dy.load(cfg.BEST_FILE, test_pc) my_eval.clear('Test') for indexes, masks, truth in test_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, np.array(masks['1D']).T) #vectors= encoder(vectors, vectors, vectors, np.array(masks['1D']).T) #vectors = encoder(vectors, vectors, vectors, np.array(masks['1D']).T, cfg.RNN_DROP) #vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False, False) pred = decoder(vectors, masks, None, 0, False, True) my_eval.add_truth('Test', truth) my_eval.add_pred('Test', pred) my_eval.evaluation('Test', cfg.PRED_TEST, cfg.TEST)
def main(): # Configuration file processing argparser = argparse.ArgumentParser() argparser.add_argument('--config_file', default='../configs/debug.cfg') argparser.add_argument('--continue_training', action='store_true', help='Load model Continue Training') argparser.add_argument('--name', default='experiment', help='The name of the experiment.') argparser.add_argument('--model', default='s2s', help='s2s: seq2seq-head-selection-model' 's2tBFS: seq2tree-BFS-decoder-model' 's2tDFS: seq2tree-DFS-decoder-model') argparser.add_argument('--gpu', default='0', help='GPU ID (-1 to cpu)') args, extra_args = argparser.parse_known_args() cfg = IniConfigurator(args.config_file, extra_args) # Logger setting logger = dual_channel_logger( __name__, file_path=cfg.LOG_FILE, file_model='w', formatter='%(asctime)s - %(levelname)s - %(message)s', time_formatter='%m-%d %H:%M') from eval.script_evaluator import ScriptEvaluator # DyNet setting os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu import dynet_config dynet_config.set(mem=cfg.DYNET_MEM, random_seed=cfg.DYNET_SEED) dynet_config.set_gpu() import dynet as dy from models.token_representation import TokenRepresentation from antu.nn.dynet.seq2seq_encoders import DeepBiRNNBuilder, orthonormal_VanillaLSTMBuilder from models.graph_nn_decoder import GraphNNDecoder # Build the dataset of the training process # Build data reader data_reader = PTBReader( field_list=['word', 'tag', 'head', 'rel'], root='0\t**root**\t_\t**rcpos**\t**rpos**\t_\t0\t**rrel**\t_\t_', spacer=r'[\t]', ) # Build vocabulary with pretrained glove vocabulary = Vocabulary() g_word, _ = glove_reader(cfg.GLOVE) pretrained_vocabs = {'glove': g_word} vocabulary.extend_from_pretrained_vocab(pretrained_vocabs) # Setup datasets datasets_settings = { 'train': DatasetSetting(cfg.TRAIN, True), 'dev': DatasetSetting(cfg.DEV, False), 'test': DatasetSetting(cfg.TEST, False), } datasets = PTBDataset(vocabulary, datasets_settings, data_reader) counters = {'word': Counter(), 'tag': Counter(), 'rel': Counter()} datasets.build_dataset(counters, no_pad_namespace={'rel'}, no_unk_namespace={'rel'}) # Build model # Parameter pc = dy.ParameterCollection() trainer = dy.AdamTrainer(pc, alpha=cfg.LR, beta_1=cfg.ADAM_BETA1, beta_2=cfg.ADAM_BETA2, eps=cfg.EPS) # Token Representation Layer token_repre = TokenRepresentation(pc, cfg, datasets.vocabulary) # BiLSTM Encoder Layer encoder = DeepBiRNNBuilder(pc, cfg.ENC_LAYERS, token_repre.token_dim, cfg.ENC_H_DIM, orthonormal_VanillaLSTMBuilder) # GNN Decoder Layer decoder = GraphNNDecoder(pc, cfg, datasets.vocabulary) # PTB Evaluator my_eval = ScriptEvaluator(['Valid', 'Test'], datasets.vocabulary) # Build Training Batch def cmp(ins): return len(ins['word']) train_batch = datasets.get_batches('train', cfg.TRAIN_BATCH_SIZE, True, cmp, True) valid_batch = list( datasets.get_batches('dev', cfg.TEST_BATCH_SIZE, False, cmp, False)) test_batch = list( datasets.get_batches('test', cfg.TEST_BATCH_SIZE, False, cmp, False)) # Train model BEST_DEV_LAS = BEST_DEV_UAS = BEST_ITER = cnt_iter = 0 valid_loss = [[] for i in range(cfg.GRAPH_LAYERS + 3)] logger.info("Experiment name: %s" % args.name) SHA = os.popen('git log -1 | head -n 1 | cut -c 8-13').readline().rstrip() logger.info('Git SHA: %s' % SHA) while cnt_iter < cfg.MAX_ITER: dy.renew_cg() cnt_iter += 1 indexes, masks, truth = train_batch.__next__() vectors = token_repre(indexes, True) vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, True) loss, part_loss = decoder(vectors, masks, truth, True, True) for i, l in enumerate([loss] + part_loss): valid_loss[i].append(l.value()) loss.backward() trainer.learning_rate = cfg.LR * cfg.LR_DECAY**(cnt_iter / cfg.LR_ANNEAL) trainer.update() if cnt_iter % cfg.VALID_ITER: continue # Validation for i in range(len(valid_loss)): valid_loss[i] = str(round(np.mean(valid_loss[i]), 2)) avg_loss = ', '.join(valid_loss) logger.info("") logger.info("Iter: %d-%d, Avg_loss: %s, LR (%f), Best (%d)" % (cnt_iter / cfg.VALID_ITER, cnt_iter, avg_loss, trainer.learning_rate, BEST_ITER)) valid_loss = [[] for i in range(cfg.GRAPH_LAYERS + 3)] my_eval.clear('Valid') for indexes, masks, truth in valid_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False) pred = decoder(vectors, masks, None, False, True) my_eval.add_truth('Valid', truth) my_eval.add_pred('Valid', pred) dy.save(cfg.LAST_FILE, [token_repre, encoder, decoder]) if my_eval.evaluation('Valid', cfg.PRED_DEV, cfg.DEV): BEST_ITER = cnt_iter / cfg.VALID_ITER os.system('cp %s.data %s.data' % (cfg.LAST_FILE, cfg.BEST_FILE)) os.system('cp %s.meta %s.meta' % (cfg.LAST_FILE, cfg.BEST_FILE)) # Just record test result my_eval.clear('Test') for indexes, masks, truth in test_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False) pred = decoder(vectors, masks, None, False, True) my_eval.add_truth('Test', truth) my_eval.add_pred('Test', pred) my_eval.evaluation('Test', cfg.PRED_TEST, cfg.TEST) my_eval.print_best_result('Valid') test_pc = dy.ParameterCollection() token_repre, encoder, decoder = dy.load(cfg.BEST_FILE, test_pc) my_eval.clear('Test') test_batch = datasets.get_batches('test', cfg.TEST_BATCH_SIZE, False, cmp, False) for indexes, masks, truth in test_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, np.array(masks['1D']).T, False) pred = decoder(vectors, masks, None, False, True) my_eval.add_truth('Test', truth) my_eval.add_pred('Test', pred) my_eval.evaluation('Test', cfg.PRED_TEST, cfg.TEST)
"""Simplest possible neural language model: use word w_i to predict word w_(i + 1) """ import os import pickle from time import clock from math import exp from matplotlib import pyplot as plt from matplotlib import patches import dynet_config dynet_config.set(random_seed=42, autobatch=1) dynet_config.set_gpu(True) import dynet as dy N_GRAM_SIZE = 1 MAX_EPOCHS = 20 BATCH_SIZE = 32 HIDDEN_DIM = 32 USE_UNLABELED = False VOCAB_SIZE = 4748 def make_batches(data, batch_size): batches = [] batch = [] for pair in data: if len(batch) == batch_size: batches.append(batch) batch = []
import os import sys import time from optparse import OptionParser from dynet import * from evaluation import * from raw_data import make_data_instance from semafor_evaluation import convert_conll_to_frame_elements import dynet_config from datetime import datetime import numpy as np import traceback from pprint import pprint import nltk dynet_config.set_gpu("GPU:0") ########################################## # functions def combine_examples(corpus_ex): """ Target ID needs to be trained for all targets in the sentence jointly, as opposed to frame and arg ID. Returns all target annotations for a given sentence. """ combined_ex = [corpus_ex[0]] for ex in corpus_ex[1:]: if ex.sent_num == combined_ex[-1].sent_num: current_sent = combined_ex.pop() target_frame_dict = current_sent.targetframedict.copy() target_frame_dict.update(ex.targetframedict) current_sent.targetframedict = target_frame_dict
import argparse import dynet_config dynet_config.set_gpu(True) # actually, this statement does not work. dy_seed = 1314159 #dy_seed = 78996 dynet_config.set(mem='4096', random_seed=dy_seed) import dynet as dy import random from utils import * from evals import * import os import pickle from model import Model def run(args, flag2embedding_path, test_ids): """ :param args: user-specific arguments :param flag2embedding_path: flag to word embedding path :param test_ids: list of sample id for testing, only used for cross-validation :return: """ win_size = args.win ds_name = args.ds_name train_set, test_test, vocab, _, tag_vocab, tag_inv_vocab = build_dataset( ds_name=ds_name, win=win_size, mode=args.running_mode, test_ids=test_ids)
def main(): # Configuration file processing argparser = argparse.ArgumentParser() argparser.add_argument('--config_file', default='../configs/debug.cfg') argparser.add_argument('--continue_training', action='store_true', help='Load model Continue Training') argparser.add_argument('--name', default='experiment', help='The name of the experiment.') argparser.add_argument('--model', default='s2s', help='s2s: seq2seq-head-selection-model' 's2tBFS: seq2tree-BFS-decoder-model' 's2tDFS: seq2tree-DFS-decoder-model') argparser.add_argument('--gpu', default='0', help='GPU ID (-1 to cpu)') args, extra_args = argparser.parse_known_args() cfg = IniConfigurator(args.config_file, extra_args) # Logger setting logger = dual_channel_logger( __name__, file_path=cfg.LOG_FILE, file_model='w', formatter='%(asctime)s - %(levelname)s - %(message)s', time_formatter='%m-%d %H:%M') from eval.script_evaluator import ScriptEvaluator # DyNet setting os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu import dynet_config dynet_config.set(mem=cfg.DYNET_MEM, random_seed=cfg.DYNET_SEED) dynet_config.set_gpu() import dynet as dy # Build the dataset of the training process # Build data reader data_reader = PTBReader( field_list=['word', 'tag', 'head', 'rel'], root='0\t**root**\t_\t**rcpos**\t**rpos**\t_\t0\t**rrel**\t_\t_', spacer=r'[\t]',) # Build vocabulary with pretrained glove vocabulary = Vocabulary() g_word, _ = glove_reader(cfg.GLOVE) pretrained_vocabs = {'glove': g_word} vocabulary.extend_from_pretrained_vocab(pretrained_vocabs) # Setup datasets datasets_settings = {'train': DatasetSetting(cfg.TRAIN, True), 'dev': DatasetSetting(cfg.DEV, False), 'test': DatasetSetting(cfg.TEST, False), } datasets = PTBDataset(vocabulary, datasets_settings, data_reader) counters = {'word': Counter(), 'tag': Counter(), 'rel': Counter()} datasets.build_dataset(counters, no_pad_namespace={'rel'}, no_unk_namespace={'rel'}) logger.info("Experiment name: %s" % args.name) SHA = os.popen('git log -1 | head -n 1 | cut -c 8-13').readline().rstrip() logger.info('Git SHA: %s' % SHA) # Build Test model test_pc = dy.ParameterCollection() token_repre, encoder, decoder = dy.load(cfg.BEST_FILE, test_pc) # PTB Evaluator my_eval = ScriptEvaluator(['Valid', 'Test'], datasets.vocabulary) my_eval.clear('Test') def cmp(ins): return len(ins['word']) test_batch = datasets.get_batches('test', cfg.TEST_BATCH_SIZE, False, cmp, False) for indexes, masks, truth in test_batch: dy.renew_cg() vectors = token_repre(indexes, False) vectors = encoder(vectors, None, cfg.RNN_DROP, cfg.RNN_DROP, False) pred = decoder(vectors, masks, None, False, True) my_eval.add_truth('Test', truth) my_eval.add_pred('Test', pred) my_eval.evaluation('Test', cfg.PRED_TEST, cfg.TEST)