-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitask.py
466 lines (423 loc) · 19.7 KB
/
multitask.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import argparse
import time
import math
import torch
import torch.nn as nn
from torch.autograd import Variable
from progress.bar import Bar
import data
import model
import sys
import numpy as np
import random
#import Decimal #TODO: need to install Decimal
sys.stderr.write('Libraries loaded\n')
## Parallelization notes:
## Does not currently operate across multiple nodes
## Single GPU is better for default: tied,emsize:200,nhid:200,nlayers:2,dropout:0.2
##
## Multiple GPUs are better for tied,emsize:1500,nhid:1500,nlayers:2,dropout:0.65
## 4 GPUs train on wikitext-2 in 1/2 - 2/3 the time of 1 GPU
parser = argparse.ArgumentParser(description='PyTorch RNN/LSTM language modeling and CCG tagging multitask model')
parser.add_argument('--lm_data', type=str, default='./data/penn',
help='location of the language modeling corpus')
parser.add_argument('--ccg_data', type=str, default='./data/ccg.02-21.common',
help='location of the CCG corpus')
parser.add_argument('--model', type=str, default='LSTM',
help='type of recurrent net (RNN_TANH, RNN_RELU, LSTM, GRU)')
parser.add_argument('--emsize', type=int, default=200,
help='size of word embeddings')
parser.add_argument('--nhid', type=int, default=200,
help='number of hidden units per layer')
parser.add_argument('--nlayers', type=int, default=2,
help='number of layers')
parser.add_argument('--lr', type=float, default=20,
help='initial learning rate')
parser.add_argument('--clip', type=float, default=0.25,
help='gradient clipping')
parser.add_argument('--epochs', type=int, default=40,
help='upper epoch limit')
parser.add_argument('--batch_size', type=int, default=20, metavar='N',
help='batch size')
parser.add_argument('--bptt', type=int, default=35,
help='sequence length')
parser.add_argument('--dropout', type=float, default=0.2,
help='dropout applied to layers (0 = no dropout)')
parser.add_argument('--tied', action='store_true',
help='tie the word embedding and softmax weights')
parser.add_argument('--seed', type=int, default=1111,
help='random seed')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--log-interval', type=int, default=200, metavar='N',
help='report interval')
parser.add_argument('--save', type=str, default='model.pt',
help='path to save the final model')
parser.add_argument('--single', action='store_true',
help='use only a single GPU (even if more are available)')
parser.add_argument('--save_lm_data', type=str, default='lm_data.bin',
help='path to save the LM data')
parser.add_argument('--test', action='store_true',
help='test a trained LM')
parser.add_argument('--guess', action='store_true',
help='display best guesses at each time step')
parser.add_argument('--guessscores', action='store_true',
help='display guess scores along with guesses')
parser.add_argument('--guessratios', action='store_true',
help='display guess ratios normalized by best guess')
parser.add_argument('--guessprobs', action='store_true',
help='display guess probs along with guesses')
parser.add_argument('--guessn', type=int, default=1,
help='output top n guesses')
parser.add_argument('--words', action='store_true',
help='evaluate word-level complexities (instead of sentence-level loss)')
parser.add_argument('--trainfname', type=str, default='train.txt',
help='name of the training file')
parser.add_argument('--validfname', type=str, default='valid.txt',
help='name of the validation file')
parser.add_argument('--testfname', type=str, default='test.txt',
help='name of the test file')
args = parser.parse_args()
# Set the random seed manually for reproducibility.
torch.manual_seed(args.seed)
if torch.cuda.is_available():
if not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
else:
torch.cuda.manual_seed(args.seed)
###############################################################################
# Load data
###############################################################################
# Starting from sequential data, batchify arranges the dataset into columns.
# For instance, with the alphabet as the sequence and batch size 4, we'd get
# a g m s
# b h n t
# c i o u
# d j p v
# e k q w
# f l r x
# These columns are treated as independent by the model, which means that the
# dependence of e. g. 'g' on 'f' can not be learned, but allows more efficient
# batch processing.
def batchify(data, bsz):
# Work out how cleanly we can divide the dataset into bsz parts.
if isinstance(data, tuple):
nbatch = data[0].size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
tag_data = data[1].narrow(0, 0, nbatch * bsz)
data = data[0].narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
tag_data = tag_data.view(bsz, -1).t().contiguous()
else:
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
# Turning the data over to CUDA at this point may lead to more OOM errors
#if args.cuda:
# data = data.cuda()
if isinstance(data,tuple):
return data, tag_data
return data
eval_batch_size = 10
corpus = data.SentenceCorpus(args.lm_data, args.ccg_data, args.save_lm_data, args.test,
trainfname=args.trainfname,
validfname=args.validfname,
testfname=args.testfname)
if args.test:
test_lm_sentences, test_lm_data = corpus.test_lm
test_ccg_sentences, test_ccg_data = corpus.test_ccg
else:
train_lm_data = batchify(corpus.train_lm, args.batch_size)
train_ccg_data = batchify(corpus.train_ccg, args.batch_size)
val_lm_data = batchify(corpus.valid_lm, eval_batch_size)
val_ccg_data = batchify(corpus.valid_ccg, eval_batch_size)
###############################################################################
# Build/load the model
###############################################################################
if not args.test:
ntokens = len(corpus.dictionary)
model = model.RNNModel(args.model, ntokens, args.emsize, args.nhid, args.nlayers, args.dropout, args.tied)
if args.cuda:
if (not args.single) and (torch.cuda.device_count() > 1):
# Scatters minibatches (in dim=1) across available GPUs
model = nn.DataParallel(model,dim=1)
model.cuda()
criterion = nn.CrossEntropyLoss()
###############################################################################
# Complexity measures
###############################################################################
def get_entropy(o):
## o should be a vector scoring possible classes
probs = nn.functional.softmax(o,dim=0)
logprobs = nn.functional.log_softmax(o,dim=0) #numerically more stable than two separate operations
return -1 * torch.sum(probs * logprobs)
def get_surps(o):
## o should be a vector scoring possible classes
logprobs = nn.functional.log_softmax(o,dim=0)
return -1 * logprobs
def get_guesses(o,scores=False):
## o should be a vector scoring possible classes
guessvals, guessixes = torch.topk(o,args.guessn,0)
# guessvals are the scores of each input cell
# guessixes are the indices of the max cells
if scores:
return guessvals
else:
return guessixes
def get_guessscores(o):
return get_guesses(o,True)
def get_complexity_iter(o,t):
for corpuspos,targ in enumerate(t):
word = corpus.dictionary.idx2word[targ]
surp = get_surps(o[corpuspos])
H = get_entropy(o[corpuspos])
print(str(word)+' '+str(surp)+' '+str(H))
def get_complexity_apply(o,t,sentid,tags=False):
## Use apply() method
Hs = torch.squeeze(apply(get_entropy,o))
surps = apply(get_surps,o)
if args.guess:
guesses = apply(get_guesses, o)
guessscores = apply(get_guessscores, o)
## Use dimensional indexing method
## NOTE: For some reason, this doesn't work.
## May marginally speed things if we can determine why
## Currently 'probs' ends up equivalent to o after the softmax
#probs = nn.functional.softmax(o,dim=0)
#logprobs = nn.functional.log_softmax(o,dim=0)
#Hs = -1 * torch.sum(probs * logprobs),dim=1)
#surps = -1 * logprobs
## Move along
for corpuspos,targ in enumerate(t):
if tags:
word = corpus.dictionary.idx2tag[int(targ)]
else:
word = corpus.dictionary.idx2word[int(targ)]
if word == '<eos>' or word == '<EOS>':
#don't output the complexity of EOS
continue
surp = surps[corpuspos][int(targ)]
if args.guess:
outputguesses = []
for g in range(args.guessn):
if tags:
outputguesses.append(corpus.dictionary.idx2tag[int(guesses[corpuspos][g])])
else:
outputguesses.append(corpus.dictionary.idx2word[int(guesses[corpuspos][g])])
if args.guessscores:
##output raw scores
outputguesses.append("{:.3f}".format(float(guessscores[corpuspos][g])))
elif args.guessratios:
##output scores (ratio of score(x)/score(best guess)
outputguesses.append("{:.3f}".format(float(guessscores[corpuspos][g])/float(guessscores[corpuspos][0])))
elif args.guessprobs:
##output probabilities ## Currently normalizes probs over N-best list; ideally it'd normalize to probs before getting the N-best
outputguesses.append("{:.3f}".format(math.exp(float(nn.functional.log_softmax(guessscores[corpuspos],dim=0)[g]))))
outputguesses = ' '.join(outputguesses)
print(str(word)+' '+str(sentid)+' '+str(corpuspos)+' '+str(len(word))+' '+str(float(surp))+' '+str(float(Hs[corpuspos]))+' '+str(outputguesses))
else:
print(str(word)+' '+str(sentid)+' '+str(corpuspos)+' '+str(len(word))+' '+str(float(surp))+' '+str(float(Hs[corpuspos])))
def apply(func, M):
## applies a function along a given dimension
tList = [func(m) for m in torch.unbind(M, dim=0) ]
res = torch.stack(tList, dim=0)
return res
###############################################################################
# Training code
###############################################################################
def repackage_hidden(h):
"""Wraps hidden states in new Variables, to detach them from their history."""
if type(h) == Variable:
return Variable(h.data)
else:
return tuple(repackage_hidden(v) for v in h)
# get_batch subdivides the source data into chunks of length args.bptt.
# If source is equal to the example output of the batchify function, with
# a bptt-limit of 2, we'd get the following two Variables for i = 0:
# a g m s b h n t
# b h n t c i o u
# Note that despite the name of the function, the subdivison of data is not
# done along the batch dimension (i.e. dimension 1), since that was handled
# by the batchify function. The chunks are along dimension 0, corresponding
# to the seq_len dimension in the LSTM.
def test_get_batch(source, evaluation=False):
if isinstance(source, tuple):
seq_len = len(source[0]) - 1
data = Variable(source[0][:seq_len], volatile=evaluation)
target = Variable(source[1][:seq_len], volatile=evaluation)
else:
seq_len = len(source) - 1
data = Variable(source[:seq_len], volatile=evaluation)
target = Variable(source[1:1+seq_len].view(-1))
# This is where data should be CUDA-fied to lessen OOM errors
if args.cuda:
return data.cuda(), target.cuda()
else:
return data, target
def get_batch(source, i, evaluation=False):
if isinstance(source, tuple):
seq_len = min(args.bptt, len(source[0]) - 1 - i)
data = Variable(source[0][i:i+seq_len], volatile=evaluation)
target = Variable(source[1][i:i+seq_len].view(-1))
else:
seq_len = min(args.bptt, len(source) - 1 - i)
data = Variable(source[i:i+seq_len], volatile=evaluation)
target = Variable(source[i+1:i+1+seq_len].view(-1))
#This is where data should be CUDA-fied to lessen OOM errors
if args.cuda:
return data.cuda(), target.cuda()
else:
return data, target
def test_evaluate(test_lm_sentences, test_ccg_sentences, lm_data_source, ccg_data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0
ntokens = len(corpus.dictionary)
if args.words:
print('word sentid sentpos wlen surp entropy')#,end='')
if args.guess:
for i in range(args.guessn):
print(' guess'+str(i))#,end='')
if args.guessscores:
print(' gscore'+str(i))#,end='')
sys.stdout.write('\n')
bar = Bar('Processing', max=len(lm_data_source)+len(ccg_data_source))
for i in range(len(lm_data_source)+len(ccg_data_source)):
if i >= len(lm_data_source):
sent_ids = ccg_data_source[i-len(lm_data_source)]
sent = test_ccg_sentences[i-len(lm_data_source)]
else:
sent_ids = lm_data_source[i]
sent = test_lm_sentences[i]
if args.cuda:
sent_ids = sent_ids.cuda()
if (not args.single) and (torch.cuda.device_count() > 1):
# "module" is necessary when using DataParallel
hidden = model.module.init_hidden(1) # number of parallel sentences being processed
else:
hidden = model.init_hidden(1) # number of parallel sentences being processed
data, targets = test_get_batch(sent_ids, evaluation=True)
data=data.unsqueeze(1) # only needed if there is just a single sentence being processed
print data
output, hidden = model(data, hidden)
output_flat = output.view(-1, ntokens)
curr_loss = criterion(output_flat, targets).data
#curr_loss = len(data) * criterion(output_flat, targets).data # needed if there is more than a single sentence being processed
total_loss += curr_loss
if args.words:
# output word-level complexity metrics
if i >= len(lm_data_source):
get_complexity_apply(output_flat,targets,i-len(lm_data_source),tags=True)
else:
get_complexity_apply(output_flat,targets,i)
else:
# output sentence-level loss
print(str(sent)+":"+str(curr_loss[0]))
hidden = repackage_hidden(hidden)
bar.next()
bar.finish()
return total_loss[0] / (len(lm_data_source)+len(ccg_data_source))
def evaluate(lm_data_source, ccg_data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0
ntokens = len(corpus.dictionary)
if (not args.single) and (torch.cuda.device_count() > 1):
#"module" is necessary when using DataParallel
hidden = model.module.init_hidden(eval_batch_size)
else:
hidden = model.init_hidden(eval_batch_size)
for i in range(0, lm_data_source.size(0) + ccg_data_source.size(0) - 1, args.bptt):
# TAG
if i > lm_data_source.size(0):
data, targets = get_batch(ccg_data_source, i - lm_data_source.size(0), evaluation=True)
# LM
else:
data, targets = get_batch(lm_data_source, i, evaluation=True)
output, hidden = model(data, hidden)
output_flat = output.view(-1, ntokens)
curr_loss = len(data) * criterion(output_flat, targets).data
total_loss += curr_loss
hidden = repackage_hidden(hidden)
return total_loss[0] / (len(lm_data_source)+len(ccg_data_source))
def train():
# Turn on training mode which enables dropout.
model.train()
total_loss = 0
start_time = time.time()
ntokens = len(corpus.dictionary)
if (not args.single) and (torch.cuda.device_count() > 1):
# "module" is necessary when using DataParallel
hidden = model.module.init_hidden(args.batch_size)
else:
hidden = model.init_hidden(args.batch_size)
# UNCOMMENT FOR DEBUGGING
#random.seed(10)
order = list(enumerate(range(0, train_lm_data.size(0) + train_ccg_data.size(0) - 1, args.bptt)))
random.shuffle(order)
for batch, i in order:#enumerate(range(0, train_lm_data.size(0) + train_ccg_data.size(0) - 1, args.bptt)):
# TAG
if i > train_lm_data.size(0):
data, targets = get_batch(train_ccg_data, i - train_lm_data.size(0))
# LM
else:
data, targets = get_batch(train_lm_data, i)
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
hidden = repackage_hidden(hidden)
model.zero_grad()
output, hidden = model(data, hidden)
loss = criterion(output.view(-1, ntokens), targets)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
for p in model.parameters():
p.data.add_(-lr, p.grad.data)
total_loss += loss.data
if batch % args.log_interval == 0 and batch > 0:
cur_loss = total_loss[0] / args.log_interval
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, batch, len(train_lm_data)+len(train_ccg_data) // args.bptt, lr,
elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
total_loss = 0
start_time = time.time()
# Loop over epochs.
lr = args.lr
best_val_loss = None
# At any point you can hit Ctrl + C to break out of training early.
if not args.test:
try:
for epoch in range(1, args.epochs+1):
epoch_start_time = time.time()
train()
val_loss = evaluate(val_lm_data, val_ccg_data)
print('-' * 89)
print('| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | '
'valid ppl {:8.2f}'.format(epoch, (time.time() - epoch_start_time),
val_loss, math.exp(val_loss)))
print('-' * 89)
# Save the model if the validation loss is the best we've seen so far.
if not best_val_loss or val_loss < best_val_loss:
with open(args.save, 'wb') as f:
torch.save(model, f)
best_val_loss = val_loss
else:
# Anneal the learning rate if no improvement has been seen in the validation dataset.
lr /= 4.0
except KeyboardInterrupt:
print('-' * 89)
print('Exiting from training early')
else:
# Load the best saved model.
with open(args.save, 'rb') as f:
model = torch.load(f)
# Run on test data.
test_loss = test_evaluate(test_lm_data, test_ccg_data)
print('=' * 89)
print('| End of training | test loss {:5.2f} | test ppl {:8.2f}'.format(
test_loss, math.exp(test_loss)))
print('=' * 89)