forked from hashbangCoder/Text-Summarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
177 lines (148 loc) · 8.95 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import torch
from torch.autograd import Variable
import cPickle as pickle
import argparse
import pdb, os
import numpy as np
import models
from torch.nn.utils import clip_grad_norm
from tqdm import tqdm
import dataloader
from visdom import Visdom
parser = argparse.ArgumentParser()
parser.add_argument("--train-file", dest="train_file", help="Path to train datafile", default='finished_files/train.bin', type=str)
parser.add_argument("--test-file", dest="test_file", help="Path to test/eval datafile", default='finished_files/test.bin', type=str)
parser.add_argument("--vocab-file", dest="vocab_file", help="Path to vocabulary datafile", default='finished_files/vocabulary.bin', type=str)
parser.add_argument("--max-abstract-size", dest="max_abstract_size", help="Maximum size of abstract for decoder input", default=110, type=int)
parser.add_argument("--max-article-size", dest="max_article_size", help="Maximum size of article for encoder input", default=300, type=int)
parser.add_argument("--num-epochs", dest="epochs", help="Number of epochs", default=10, type=int)
parser.add_argument("--batch-size", dest="batchSize", help="Mini-batch size", default=32, type=int)
parser.add_argument("--embed-size", dest="embedSize", help="Size of word embedding", default=300, type=int)
parser.add_argument("--hidden-size", dest="hiddenSize", help="Size of hidden to model", default=128, type=int)
parser.add_argument("--learning-rate", dest="lr", help="Learning Rate", default=0.001, type=float)
parser.add_argument("--lambda", dest="lmbda", help="Hyperparameter for auxillary cost", default=1, type=float)
parser.add_argument("--beam-size", dest="beam_size", help="beam size for beam search decoding", default=4, type=int)
parser.add_argument("--max-decode", dest="max_decode", help="Maximum length of decoded output", default=40, type=int)
parser.add_argument("--grad-clip", dest="grad_clip", help="Clip gradients of RNN model", default=2, type=float)
parser.add_argument("--truncate-vocab", dest="trunc_vocab", help="size of truncated Vocabulary <= 50000 [to save memory]", default=50000, type=int)
parser.add_argument("--bootstrap", dest="bootstrap", help="Bootstrap word embeds with GloVe?", default=0, type=int)
parser.add_argument("--print-ground-truth", dest="print_ground_truth", help="Print the article and abstract", default=1, type=int)
parser.add_argument("--eval-freq", dest="eval_freq", help="How frequently (every mini-batch) to evaluate model", default=20000, type=int)
parser.add_argument("--save-dir", dest="save_dir", help="Directory to save trained models", default='Saved-Models/', type=str)
parser.add_argument("--load-model", dest="load_model", help="Directory from which to load trained models", default=None, type=str)
opt = parser.parse_args()
vis = Visdom()
### evaluation code
def evalModel(model):
# set model to eval mode
model.eval()
print '\n\n'
print '*'*30, ' MODEL EVALUATION ', '*'*30
_article, _revArticle, _extArticle, max_article_oov, article_oov, article_string, abs_string = dl.getEvalBatch()
_article = Variable(_article.cuda(), volatile=True)
_extArticle = Variable(_extArticle.cuda(), volatile=True)
_revArticle = Variable(_revArticle.cuda(), volatile=True)
all_summaries = model((_article, _revArticle, _extArticle), max_article_oov, decode_flag=True)
model.train()
return all_summaries, article_string, abs_string, article_oov
### utility code for displaying generated abstract
def displayOutput(all_summaries, article, abstract, article_oov, show_ground_truth=False):
print '*' * 80
print '\n'
if show_ground_truth:
print 'ARTICLE TEXT : \n', article
print 'ACTUAL ABSTRACT : \n', abstract
for i, summary in enumerate(all_summaries):
generated_summary = ' '.join([dl.id2word[ind] if ind<=dl.vocabSize else article_oov[ind % dl.vocabSize] for ind in summary])
print 'GENERATED ABSTRACT #%d : \n' %(i+1), generated_summary
print '*' * 80
return
# Utility code to save model to disk
def save_model(net, optimizer,all_summaries, article_string, abs_string):
save_dict = dict({'model': net.state_dict(), 'optim': optimizer.state_dict(), 'epoch': dl.epoch, 'iter':dl.iterInd, 'summaries':all_summaries, 'article':article_string, 'abstract_gold':abs_string})
print '\n','-' * 60
print 'Saving Model to : ', opt.save_dir
save_name = opt.save_dir + 'savedModel_E%d_%d.pth' % (dl.epoch, dl.iterInd)
torch.save(save_dict, save_name)
print '-' * 60
return
assert opt.trunc_vocab <= 50000, 'Invalid value for --truncate-vocab'
assert os.path.isfile(opt.vocab_file), 'Invalid Path to vocabulary file'
with open(opt.vocab_file) as f:
vocab = pickle.load(f) #list of tuples of word,count. Convert to list of words
vocab = [item[0] for item in vocab[:-(5+ 50000 - opt.trunc_vocab)]] # Truncate vocabulary to conserve memory
vocab += ['<unk>', '<go>', '<end>', '<s>', '</s>'] # add special token to vocab to bring total count to 50k
dl = dataloader.dataloader(opt.batchSize, opt.epochs, vocab, opt.train_file, opt.test_file,
opt.max_article_size, opt.max_abstract_size)
if opt.bootstrap:
# bootstrap with pretrained embeddings
wordEmbed = torch.nn.Embedding(len(vocab) + 1, 300, 0)
print 'Bootstrapping with pretrained GloVe word vectors...'
assert os.path.isfile('embeds.pkl'), 'Cannot find pretrained Word embeddings to bootstrap'
with open('embeds.pkl', 'rb') as f:
embeds = pickle.load(f)
assert wordEmbed.weight.size() == embeds.size()
wordEmbed.weight.data[1:,:] = embeds
else:
# learn embeddings from scratch (default)
wordEmbed = torch.nn.Embedding(len(vocab) + 1, opt.embedSize, 0)
print 'Building and initializing SummaryNet...'
net = models.SummaryNet(opt.embedSize, opt.hiddenSize, dl.vocabSize, wordEmbed,
start_id=dl.word2id['<go>'], stop_id=dl.word2id['<end>'], unk_id=dl.word2id['<unk>'],
max_decode=opt.max_decode, beam_size=opt.beam_size, lmbda=opt.lmbda)
net = net.cuda()
optimizer = torch.optim.Adam(net.parameters(), lr=opt.lr)
if opt.load_model is not None and os.path.isfile(opt.load_model):
saved_file = torch.load(opt.load_model)
net.load_state_dict(saved_file['model'])
optimizer.load_state_dict(saved_file['optim'])
dl.epoch = saved_file['epoch']
dl.iterInd = saved_file['iter']
dl.pbar.update(dl.iterInd)
print '\n','*'*30, 'RESUME FROM CHECKPOINT : %s' %opt.load_model,'*'*30
else:
print '\n','*'*30, 'START TRAINING','*'*30
#dl.iterInd = 287226
#dl.pbar.update(dl.iterInd)
all_loss = []
win = None
### Training loop'
while dl.epoch <= opt.epochs:
data_batch = dl.getBatch(opt.batchSize)
batchArticles, batchExtArticles, batchRevArticles, batchAbstracts, batchTargets, _, _, max_article_oov, article_oov = data_batch
# end of training/max epoch reached
if data_batch is None:
print '-'*50, 'END OF TRAINING', '-'*50
break
batchArticles = Variable(batchArticles.cuda())
batchExtArticles = Variable(batchExtArticles.cuda())
batchRevArticles = Variable(batchRevArticles.cuda())
batchTargets = Variable(batchTargets.cuda())
batchAbstracts = Variable(batchAbstracts.cuda())
losses = net((batchArticles, batchExtArticles, batchRevArticles, batchAbstracts, batchTargets), max_article_oov)
batch_loss = losses.mean()
batch_loss.backward()
# gradient clipping by norm
clip_grad_norm(net.parameters(), opt.grad_clip)
optimizer.step()
optimizer.zero_grad()
# update loss ticker
dl.pbar.set_postfix(loss=batch_loss.cpu().data[0])
dl.pbar.update(opt.batchSize)
# save losses periodically
if dl.iterInd % 50:
all_loss.append(batch_loss.cpu().data.tolist()[0])
title = 'Pointer Model with Coverage'
if win is None:
win = vis.line(Y=np.array(all_loss), X=np.arange(1, len(all_loss)+1), opts=dict(title=title, xlabel='#Mini-Batches (x%d)' %(opt.batchSize),
ylabel='Train-Loss'))
vis.line(Y=np.array(all_loss), X=np.arange(1, len(all_loss)+1), win=win, update='replace', opts=dict(title=title, xlabel='#Mini-Batches (x%d)' %(opt.batchSize),
ylabel='Train-Loss'))
# evaluate model periodically
if dl.iterInd % opt.eval_freq < opt.batchSize and dl.iterInd > opt.batchSize:
all_summaries, article_string, abs_string, article_oov = evalModel(net)
displayOutput(all_summaries, article_string, abs_string, article_oov, show_ground_truth=opt.print_ground_truth)
#if dl.epoch > 1 and dl.iterInd == 0:
if dl.iterInd % (6*opt.eval_freq) < opt.batchSize and dl.iterInd > opt.batchSize:
save_model(net, optimizer, all_summaries, article_string, abs_string)
del batch_loss, batchArticles, batchExtArticles, batchRevArticles, batchAbstracts, batchTargets