def build_model(fields, args, checkpoint=None): """Build model.""" model = LMModel(fields, args) if checkpoint is not None: logger.info("Set model using saved checkpoint") model.load_state_dict(checkpoint["model"]) return model.to(args.device)
def build_test_model(fields, params): """Build test model.""" model = LMModel(fields, params["args"]) model.load_state_dict(params["model"]) model = model.to(params["args"].device) model.eval() return model
def main(_): if not FLAGS.data_path: raise ValueError("Must set --data_path to PTB data directory") raw_data = reader.ptb_raw_data(FLAGS.data_path) test_data= raw_data eval_config = get_config() eval_config.batch_size = 1 eval_config.num_steps = 1 config_gpu = tf.ConfigProto(allow_soft_placement=True) with tf.Graph().as_default(),tf.Session(config=config_gpu) as sess: initializer = tf.random_uniform_initializer(-eval_config.init_scale,eval_config.init_scale) with tf.name_scope("Test"): with tf.variable_scope("model", reuse=None,initializer=initializer): mtest = LMModel(is_training=False, config=eval_config) tf.global_variables_initializer().run() saver = tf.train.Saver() checkpoint=tf.train.latest_checkpoint(FLAGS.save_path) saver.restore(sess,checkpoint) test_perplexity = do_eval(sess, mtest, test_data) print("Test Perplexity: %.3f" % test_perplexity)
# load data train_batch_size = args.train_batch_size eval_batch_size = args.eval_batch_size batch_size = {'train': train_batch_size, 'valid': eval_batch_size} data_loader = data.Corpus("../data/ptb", batch_size, args.max_sql) # WRITE CODE HERE within two '#' bar ######################################## # Build LMModel model (bulid your language model here) nvoc = len(data_loader.vocabulary) print("nvoc: " + str(nvoc)) ninput = 150 nhid = 150 nlayer = 4 model = LMModel(nvoc, ninput, nhid, nlayer, device, args.attention).to(device) ######################################## criterion = nn.CrossEntropyLoss().to(device) # WRITE CODE HERE within two '#' bar ######################################## # Evaluation Function # Calculate the average cross-entropy loss between the prediction and the ground truth word. # And then exp(average cross-entropy loss) is perplexity. def bleu_metric(candidates, references, max_n): def get_max_n_grams(s): ans = []
else: device = torch.device("cpu") # load data train_batch_size = args.train_batch_size eval_batch_size = args.eval_batch_size batch_size = {'train': train_batch_size, 'valid': eval_batch_size} data_loader = data.Corpus("../data/ptb", batch_size, args.max_sql) # WRITE CODE HERE within two '#' bar ######################################## # Build LMModel model (build your language model here) n_input = 256 n_hid = 512 n_layers = 2 model = LMModel(data_loader.nvoc, n_input, n_hid, n_layers) if args.cuda: model.cuda() opt = optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=0.00001) criterion = nn.CrossEntropyLoss() ######################################## # WRITE CODE HERE within two '#' bar ######################################## # Evaluation Function # Calculate the average cross-entropy loss between the prediction and the ground truth word.