This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathutils.py
571 lines (489 loc) · 19 KB
/
utils.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
"""Minibatching utilities."""
import operator
import numpy as np
import torch
from torch.autograd import Variable
from sklearn.utils import shuffle
import itertools
import os
import pickle
from itertools import izip
class DataIterator(object):
"""Data Iterator."""
def _trim_vocab(self, vocab, vocab_size):
# Discard start, end, pad and unk tokens if already present
if '<s>' in vocab:
del vocab['<s>']
if '<pad>' in vocab:
del vocab['<pad>']
if '</s>' in vocab:
del vocab['</s>']
if '<unk>' in vocab:
del vocab['<unk>']
word2id = {
'<s>': 0,
'<pad>': 1,
'</s>': 2,
'<unk>': 3,
}
id2word = {
0: '<s>',
1: '<pad>',
2: '</s>',
3: '<unk>',
}
sorted_word2id = sorted(
vocab.items(),
key=operator.itemgetter(1),
reverse=True
)
if vocab_size != -1:
sorted_words = [x[0] for x in sorted_word2id[:vocab_size]]
else:
sorted_words = [x[0] for x in sorted_word2id]
for ind, word in enumerate(sorted_words):
word2id[word] = ind + 4
for ind, word in enumerate(sorted_words):
id2word[ind + 4] = word
return word2id, id2word
def construct_vocab(
self, sentences, vocab_size,
lowercase=False, charlevel=False
):
"""Create vocabulary."""
vocab = {}
for sentence in sentences:
if isinstance(sentence, str):
if lowercase:
sentence = sentence.lower()
if not charlevel:
sentence = sentence.split()
for word in sentence:
if word not in vocab:
vocab[word] = 1
else:
vocab[word] += 1
print('Found %d words in dataset ' % (len(vocab)))
word2id, id2word = self._trim_vocab(vocab, vocab_size)
return word2id, id2word
class BufferedDataIterator(DataIterator):
"""Multi Parallel corpus data iterator."""
def __init__(
self, src, trg, src_vocab_size, trg_vocab_size, tasknames,
save_dir, buffer_size=1e6, lowercase=False
):
"""Initialize params."""
self.fname_src = src
self.fname_trg = trg
self.src_vocab_size = src_vocab_size
self.trg_vocab_size = trg_vocab_size
self.tasknames = tasknames
self.save_dir = save_dir
self.buffer_size = buffer_size
self.lowercase = lowercase
# Open a list of file pointers to all the files.
self.f_src = [open(fname, 'r') for fname in self.fname_src]
self.f_trg = [open(fname, 'r') for fname in self.fname_trg]
# Initialize dictionaries that contain sentences & word mapping dicts
self.src = [
{'data': [], 'word2id': None, 'id2word': None}
for i in range(len(self.fname_src))
]
self.trg = [
{'data': [], 'word2id': None, 'id2word': None}
for i in range(len(self.fname_trg))
]
print('Building vocabulary ...')
self.build_vocab()
'''Reset file pointers to the start after reading the file to
build vocabularies.'''
for idx in range(len(self.src)):
self._reset_filepointer(idx)
for idx in range(len(self.src)):
self.fetch_buffer(idx)
def _reset_filepointer(self, idx):
self.f_src[idx] = open(self.fname_src[idx], 'r')
self.f_trg[idx] = open(self.fname_trg[idx], 'r')
def fetch_buffer(self, idx, reset=True):
"""Fetch sentences from the file into the buffer."""
print('Fetching sentences ...')
print('Processing corpus : %d task %s ' % (
idx, self.tasknames[idx])
)
# Reset the contents of the current buffer.
if reset:
self.src[idx]['data'] = []
self.trg[idx]['data'] = []
# Populate buffer
for src, trg in izip(self.f_src[idx], self.f_trg[idx]):
if len(self.src[idx]['data']) == self.buffer_size:
break
if self.lowercase:
self.src[idx]['data'].append(src.lower().split())
self.trg[idx]['data'].append(trg.lower().split())
else:
self.src[idx]['data'].append(src.split())
self.trg[idx]['data'].append(trg.split())
# Sort sentences by decreasing length (hacky bucketing)
self.src[idx]['data'], self.trg[idx]['data'] = \
zip(*sorted(
zip(self.src[idx]['data'], self.trg[idx]['data']),
key=lambda x: len(x[0]),
reverse=True
))
'''If buffer isn't full after reading the contents of the file,
cycle around. '''
if len(self.src[idx]['data']) < self.buffer_size:
assert len(self.src[idx]['data']) == len(self.trg[idx]['data'])
print('Reached end of dataset, reseting file pointer ...')
# Cast things to list to avoid issue with calling .append above
self.src[idx]['data'] = list(self.src[idx]['data'])
self.trg[idx]['data'] = list(self.trg[idx]['data'])
self._reset_filepointer(idx)
self.fetch_buffer(idx, reset=False)
print('Fetched %d sentences' % (len(self.src[idx]['data'])))
def build_vocab(self):
"""Build a memory efficient vocab."""
# Construct common source vocab.
print('Building common source vocab ...')
# Check if save directory exists.
if not os.path.exists(self.save_dir):
raise ValueError("Could not find save dir : %s" % (
self.save_dir)
)
# Check if a cached vocab file exists.
if os.path.exists(os.path.join(self.save_dir, 'src_vocab.pkl')):
print('Found existing vocab file. Reloading ...')
vocab = pickle.load(open(
os.path.join(self.save_dir, 'src_vocab.pkl'),
'rb'
))
word2id, id2word = vocab['word2id'], vocab['id2word']
# If not, compute the vocab from scratch and store a cache.
else:
print('Could not find existing vocab. Building ...')
word2id, id2word = self.construct_vocab(
itertools.chain.from_iterable(self.f_src),
self.src_vocab_size, self.lowercase
)
pickle.dump(
{'word2id': word2id, 'id2word': id2word},
open(os.path.join(self.save_dir, 'src_vocab.pkl'), 'wb')
)
for corpus in self.src:
corpus['word2id'], corpus['id2word'] = word2id, id2word
# Do the same for the target vocabulary.
print('Building target vocabs ...')
if os.path.exists(os.path.join(self.save_dir, 'trg_vocab.pkl')):
print('Found existing vocab file. Reloading ...')
vocab = pickle.load(open(
os.path.join(self.save_dir, 'trg_vocab.pkl'),
'rb'
))
for idx, (corpus, fname) in enumerate(
zip(self.trg, self.f_trg)
):
print('Reloading vocab for %s ' % (self.tasknames[idx]))
word2id, id2word = vocab[self.tasknames[idx]]['word2id'], \
vocab[self.tasknames[idx]]['id2word']
corpus['word2id'], corpus['id2word'] = word2id, id2word
else:
print('Could not find existing vocab. Building ...')
trg_vocab_dump = {}
for idx, (corpus, fname) in enumerate(
zip(self.trg, self.f_trg)
):
print('Building vocab for %s ' % (self.tasknames[idx]))
word2id, id2word = self.construct_vocab(
fname, self.trg_vocab_size, self.lowercase
)
corpus['word2id'], corpus['id2word'] = word2id, id2word
trg_vocab_dump[self.tasknames[idx]] = {}
trg_vocab_dump[self.tasknames[idx]]['word2id'] = word2id
trg_vocab_dump[self.tasknames[idx]]['id2word'] = id2word
pickle.dump(
trg_vocab_dump,
open(os.path.join(self.save_dir, 'trg_vocab.pkl'), 'wb')
)
def shuffle_dataset(self, idx):
"""Shuffle current buffer."""
self.src[idx]['data'], self.trg[idx]['data'] = shuffle(
self.src[idx]['data'], self.trg[idx]['data']
)
def get_parallel_minibatch(
self, corpus_idx, index, batch_size, max_len_src, max_len_trg
):
"""Prepare minibatch."""
src_lines = [
['<s>'] + line[:max_len_src - 2] + ['</s>']
for line in self.src[corpus_idx]['data'][index: index + batch_size]
]
trg_lines = [
['<s>'] + line[:max_len_trg - 2] + ['</s>']
for line in self.trg[corpus_idx]['data'][index: index + batch_size]
]
'''Sort sentences by decreasing length within a minibatch for
`torch.nn.utils.packed_padded_sequence`'''
src_lens = [len(line) for line in src_lines]
sorted_indices = np.argsort(src_lens)[::-1]
sorted_src_lines = [src_lines[idx] for idx in sorted_indices]
sorted_trg_lines = [trg_lines[idx] for idx in sorted_indices]
sorted_src_lens = [len(line) for line in sorted_src_lines]
sorted_trg_lens = [len(line) for line in sorted_trg_lines]
max_src_len = max(sorted_src_lens)
max_trg_len = max(sorted_trg_lens)
# Map words to indices
input_lines_src = [
[self.src[corpus_idx]['word2id'][w] if w in self.src[corpus_idx]['word2id'] else self.src[corpus_idx]['word2id']['<unk>'] for w in line] +
[self.src[corpus_idx]['word2id']['<pad>']] * (max_src_len - len(line))
for line in sorted_src_lines
]
input_lines_trg = [
[self.trg[corpus_idx]['word2id'][w] if w in self.trg[corpus_idx]['word2id'] else self.trg[corpus_idx]['word2id']['<unk>'] for w in line[:-1]] +
[self.trg[corpus_idx]['word2id']['<pad>']] * (max_trg_len - len(line))
for line in sorted_trg_lines
]
output_lines_trg = [
[self.trg[corpus_idx]['word2id'][w] if w in self.trg[corpus_idx]['word2id'] else self.trg[corpus_idx]['word2id']['<unk>'] for w in line[1:]] +
[self.trg[corpus_idx]['word2id']['<pad>']] * (max_trg_len - len(line))
for line in sorted_trg_lines
]
# Cast lists to torch tensors
input_lines_src = Variable(torch.LongTensor(input_lines_src)).cuda()
input_lines_trg = Variable(torch.LongTensor(input_lines_trg)).cuda()
output_lines_trg = Variable(torch.LongTensor(output_lines_trg)).cuda()
sorted_src_lens = Variable(
torch.LongTensor(sorted_src_lens), volatile=True
).squeeze().cuda()
# Return minibatch of src-trg pairs
return {
'input_src': input_lines_src,
'input_trg': input_lines_trg,
'output_trg': output_lines_trg,
'src_lens': sorted_src_lens,
'type': 'seq2seq'
}
class NLIIterator(DataIterator):
"""Data iterator for tokenized NLI datasets."""
def __init__(
self, train, dev, test,
vocab_size, lowercase=True, vocab=None
):
r"""Initialize params.
Each of train/dev/test is a tab-separate file of the form
premise \t hypothesis \t label
"""
self.train = train
self.dev = dev
self.test = test
self.vocab_size = vocab_size
self.lowercase = lowercase
self.vocab = vocab
self.train_lines = [
line.strip().lower().split('\t')
for line in open(self.train)
]
self.dev_lines = [
line.strip().lower().split('\t')
for line in open(self.dev)
]
self.test_lines = [
line.strip().lower().split('\t')
for line in open(self.test)
]
if self.vocab is not None:
self.vocab = pickle.load(open(self.vocab, 'rb'))
self.word2id = self.vocab['word2id']
self.id2word = self.vocab['id2word']
self.vocab_size = len(self.word2id)
else:
self.word2id, self.id2word = self.construct_vocab(
[x[0] for x in self.train_lines] +
[x[1] for x in self.train_lines],
self.vocab_size, lowercase=self.lowercase
)
# Label text to class mapping.
self.text2label = {
'entailment': 0,
'neutral': 1,
'contradiction': 2
}
self.shuffle_dataset()
def shuffle_dataset(self):
"""Shuffle training data."""
self.train_lines = shuffle(self.train_lines)
def get_parallel_minibatch(
self, index, batch_size, sent_type='train'
):
"""Prepare minibatch."""
if sent_type == 'train':
lines = self.train_lines
elif sent_type == 'dev':
lines = self.dev_lines
else:
lines = self.test_lines
sent1 = [
['<s>'] + line[0].split() + ['</s>']
for line in lines[index: index + batch_size]
]
sent2 = [
['<s>'] + line[1].split() + ['</s>']
for line in lines[index: index + batch_size]
]
labels = [
self.text2label[line[2]]
for line in lines[index: index + batch_size]
]
sent1_lens = [len(line) for line in sent1]
sorted_sent1_indices = np.argsort(sent1_lens)[::-1]
sorted_sent1_lines = [sent1[idx] for idx in sorted_sent1_indices]
rev_sent1 = np.argsort(sorted_sent1_indices)
sent2_lens = [len(line) for line in sent2]
sorted_sent2_indices = np.argsort(sent2_lens)[::-1]
sorted_sent2_lines = [sent2[idx] for idx in sorted_sent2_indices]
rev_sent2 = np.argsort(sorted_sent2_indices)
sorted_sent1_lens = [len(line) for line in sorted_sent1_lines]
sorted_sent2_lens = [len(line) for line in sorted_sent2_lines]
max_sent1_len = max(sorted_sent1_lens)
max_sent2_len = max(sorted_sent2_lens)
sent1 = [
[
self.word2id[w] if w in self.word2id else self.word2id['<unk>']
for w in line
] +
[self.word2id['<pad>']] * (max_sent1_len - len(line))
for line in sorted_sent1_lines
]
sent2 = [
[
self.word2id[w] if w in self.word2id else self.word2id['<unk>']
for w in line
] +
[self.word2id['<pad>']] * (max_sent2_len - len(line))
for line in sorted_sent2_lines
]
sent1 = Variable(torch.LongTensor(sent1)).cuda()
sent2 = Variable(torch.LongTensor(sent2)).cuda()
labels = Variable(torch.LongTensor(labels)).cuda()
sent1_lens = Variable(
torch.LongTensor(sorted_sent1_lens),
requires_grad=False
).squeeze().cuda()
sent2_lens = Variable(
torch.LongTensor(sorted_sent2_lens),
requires_grad=False
).squeeze().cuda()
rev_sent1 = Variable(
torch.LongTensor(rev_sent1),
requires_grad=False
).squeeze().cuda()
rev_sent2 = Variable(
torch.LongTensor(rev_sent2),
requires_grad=False
).squeeze().cuda()
return {
'sent1': sent1,
'sent2': sent2,
'sent1_lens': sent1_lens,
'sent2_lens': sent2_lens,
'rev_sent1': rev_sent1,
'rev_sent2': rev_sent2,
'labels': labels,
'type': 'nli'
}
def get_validation_minibatch(
src, trg, index, batch_size,
src_word2id, trg_word2id
):
"""Prepare minibatch."""
src_lines = [
['<s>'] + line + ['</s>']
for line in src[index: index + batch_size]
]
trg_lines = [
['<s>'] + line + ['</s>']
for line in trg[index: index + batch_size]
]
src_lens = [len(line) for line in src_lines]
sorted_indices = np.argsort(src_lens)[::-1]
sorted_src_lines = [src_lines[idx] for idx in sorted_indices]
sorted_trg_lines = [trg_lines[idx] for idx in sorted_indices]
sorted_src_lens = [len(line) for line in sorted_src_lines]
sorted_trg_lens = [len(line) for line in sorted_trg_lines]
max_src_len = max(sorted_src_lens)
max_trg_len = max(sorted_trg_lens)
input_lines_src = [
[
src_word2id[w] if w in src else src_word2id['<unk>']
for w in line
] +
[src_word2id['<pad>']] * (max_src_len - len(line))
for line in sorted_src_lines
]
input_lines_trg = [
[
trg_word2id[w] if w in trg_word2id else trg_word2id['<unk>']
for w in line[:-1]
] +
[trg_word2id['<pad>']] * (max_trg_len - len(line))
for line in sorted_trg_lines
]
output_lines_trg = [
[
trg_word2id[w] if w in trg_word2id else trg_word2id['<unk>']
for w in line[1:]
] +
[trg_word2id['<pad>']] * (max_trg_len - len(line))
for line in sorted_trg_lines
]
input_lines_src = Variable(
torch.LongTensor(input_lines_src),
volatile=True
).cuda()
input_lines_trg = Variable(
torch.LongTensor(input_lines_trg),
volatile=True
).cuda()
output_lines_trg = Variable(
torch.LongTensor(output_lines_trg),
volatile=True
).cuda()
sorted_src_lens = Variable(
torch.LongTensor(sorted_src_lens),
volatile=True
).squeeze().cuda()
return {
'input_src': input_lines_src,
'input_trg': input_lines_trg,
'output_trg': output_lines_trg,
'src_lens': sorted_src_lens,
'type': 'seq2seq'
}
def compute_validation_loss(
config, model, train_iterator,
criterion, task_idx, lowercase=False
):
"""Compute validation loss for a task."""
val_src = config['data']['paths'][task_idx]['val_src']
val_trg = config['data']['paths'][task_idx]['val_trg']
if lowercase:
val_src = [line.strip().lower().split() for line in open(val_src, 'r')]
val_trg = [line.strip().lower().split() for line in open(val_trg, 'r')]
else:
val_src = [line.strip().split() for line in open(val_src, 'r')]
val_trg = [line.strip().split() for line in open(val_trg, 'r')]
batch_size = config['training']['batch_size']
losses = []
for j in range(0, len(val_src), batch_size):
minibatch = get_validation_minibatch(
val_src, val_trg, j, batch_size,
train_iterator.src[task_idx]['word2id'],
train_iterator.trg[task_idx]['word2id'],
)
decoder_logit = model(minibatch, task_idx)
loss = criterion(
decoder_logit.contiguous().view(-1, decoder_logit.size(2)),
minibatch['output_trg'].contiguous().view(-1)
)
losses.append(loss.data[0])
return np.mean(losses)