forked from facebookresearch/InferSent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
executable file
·780 lines (585 loc) · 30.2 KB
/
models.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
"""
This file contains the definition of encoders used in https://arxiv.org/pdf/1705.02364.pdf
"""
import numpy as np
import time
import torch
from torch.autograd import Variable
import torch.nn as nn
"""
BLSTM (max/mean) encoder
"""
class BLSTMEncoder(nn.Module):
def __init__(self, config):
super(BLSTMEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.dpout_model = config['dpout_model']
self.use_cuda = config['use_cuda']
self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True, dropout=self.dpout_model)
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
idx_unsort = np.argsort(idx_sort)
idx_sort = torch.from_numpy(idx_sort).cuda() if self.use_cuda else torch.from_numpy(idx_sort)
sent = sent.index_select(1, Variable(idx_sort))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed)[0] #seqlen x batch x 2*nhid
sent_output = nn.utils.rnn.pad_packed_sequence(sent_output)[0]
# Un-sort by length
idx_unsort = torch.from_numpy(idx_unsort).cuda() if self.use_cuda else torch.from_numpy(idx_unsort)
sent_output = sent_output.index_select(1, Variable(idx_unsort))
# Pooling
if self.pool_type == "mean":
sent_len = Variable(torch.FloatTensor(sent_len)).unsqueeze(1).cuda()
emb = torch.sum(sent_output, 0).squeeze(0)
emb = emb / sent_len.expand_as(emb)
elif self.pool_type == "max":
emb = torch.max(sent_output, 0)[0].squeeze(0)
return emb
def set_glove_path(self, glove_path):
self.glove_path = glove_path
def get_word_dict(self, sentences, tokenize=True):
# create vocab of words
word_dict = {}
if tokenize: from nltk.tokenize import word_tokenize
sentences = [s.split() if not tokenize else word_tokenize(s) for s in sentences]
for sent in sentences:
for word in sent:
if word not in word_dict:
word_dict[word] = ''
word_dict['<s>'] = ''
word_dict['</s>'] = ''
return word_dict
def get_glove(self, word_dict):
assert hasattr(self, 'glove_path'), 'warning : you need to set_glove_path(glove_path)'
# create word_vec with glove vectors
word_vec = {}
with open(self.glove_path) as f:
for line in f:
word, vec = line.split(' ', 1)
if word in word_dict:
word_vec[word] = np.fromstring(vec, sep=' ')
print('Found {0}(/{1}) words with glove vectors'.format(len(word_vec), len(word_dict)))
return word_vec
def get_glove_k(self, K):
assert hasattr(self, 'glove_path'), 'warning : you need to set_glove_path(glove_path)'
# create word_vec with k first glove vectors
k = 0
word_vec = {}
with open(self.glove_path) as f:
for line in f:
word, vec = line.split(' ', 1)
if k<=K:
word_vec[word] = np.fromstring(vec, sep=' ')
k += 1
if k>K:
if word in ['<s>', '</s>']:
word_vec[word] = np.fromstring(vec, sep=' ')
if k>K and all([w in word_vec for w in ['<s>', '</s>']]):
break
return word_vec
def build_vocab(self, sentences, tokenize=True):
assert hasattr(self, 'glove_path'), 'warning : you need to set_glove_path(glove_path)'
word_dict = self.get_word_dict(sentences, tokenize)
self.word_vec = self.get_glove(word_dict)
print('Vocab size : {0}'.format(len(self.word_vec)))
# build GloVe vocab with k most frequent words
def build_vocab_k_words(self, K):
assert hasattr(self, 'glove_path'), 'warning : you need to set_glove_path(glove_path)'
self.word_vec = self.get_glove_k(K)
print('Vocab size : {0}'.format(K))
def update_vocab(self, sentences, tokenize=True):
assert hasattr(self, 'glove_path'), 'warning : you need to set_glove_path(glove_path)'
assert hasattr(self, 'word_vec'), 'build_vocab before updating it'
word_dict = self.get_word_dict(sentences, tokenize)
# keep only new words
for word in self.word_vec:
if word in word_dict:
del word_dict[word]
# udpate vocabulary
if word_dict:
new_word_vec = self.get_glove(word_dict)
self.word_vec.update(new_word_vec)
print('New vocab size : {0} (added {1} words)'.format(len(self.word_vec), len(new_word_vec)))
def get_batch(self, batch):
# sent in batch in decreasing order of lengths (bsize, max_len, word_dim)
embed = np.zeros((len(batch[0]), len(batch), self.word_emb_dim))
for i in range(len(batch)):
for j in range(len(batch[i])):
embed[j, i, :] = self.word_vec[batch[i][j]]
return torch.FloatTensor(embed)
def encode(self, sentences, bsize=64, tokenize=True, verbose=False):
tic = time.time()
if tokenize: from nltk.tokenize import word_tokenize
sentences = [['<s>']+s.split()+['</s>'] if not tokenize else ['<s>']+word_tokenize(s)+['</s>'] for s in sentences]
n_w = np.sum([len(x) for x in sentences])
# filters words without glove vectors
for i in range(len(sentences)):
s_f = [word for word in sentences[i] if word in self.word_vec]
if not s_f:
import warnings
warnings.warn('No words in "{0}" (idx={1}) have glove vectors. Replacing by "</s>"..'.format(sentences[i], i))
s_f = ['</s>']
sentences[i] = s_f
lengths = np.array([len(s) for s in sentences])
n_wk = np.sum(lengths)
if verbose:
print('Nb words kept : {0}/{1} ({2} %)'.format(n_wk, n_w, round((100.0 * n_wk) / n_w, 2)))
# sort by decreasing length
lengths, idx_sort = np.sort(lengths)[::-1], np.argsort(-lengths)
sentences = np.array(sentences)[idx_sort]
embeddings = []
for stidx in range(0, len(sentences), bsize):
batch = Variable(self.get_batch(sentences[stidx:stidx + bsize]), volatile=True)
if self.use_cuda:
batch = batch.cuda()
batch = self.forward((batch, lengths[stidx:stidx + bsize])).data.cpu().numpy()
embeddings.append(batch)
embeddings = np.vstack(embeddings)
# unsort
idx_unsort = np.argsort(idx_sort)
embeddings = embeddings[idx_unsort]
if verbose:
print('Speed : {0} sentences/s ({1} mode, bsize={2})'.format(round(len(embeddings)/(time.time()-tic), 2),\
'gpu' if self.use_cuda else 'cpu', bsize))
return embeddings
def visualize(self, sent, tokenize=True):
if tokenize: from nltk.tokenize import word_tokenize
sent = sent.split() if not tokenize else word_tokenize(sent)
sent = [['<s>'] + [word for word in sent if word in self.word_vec] + ['</s>']]
if ' '.join(sent[0]) == '<s> </s>':
import warnings
warnings.warn('No words in "{0}" have glove vectors. Replacing by "<s> </s>"..'.format(sent))
batch = Variable(self.get_batch(sent), volatile=True)
if self.use_cuda:
batch = batch.cuda()
output = self.enc_lstm(batch)[0]
output, idxs = torch.max(output, 0)
#output, idxs = output.squeeze(), idxs.squeeze()
idxs = idxs.data.cpu().numpy()
argmaxs = [np.sum((idxs==k)) for k in range(len(sent[0]))]
# visualize model
import matplotlib.pyplot as plt
x = range(len(sent[0]))
y = [100.0*n/np.sum(argmaxs) for n in argmaxs]
fig = plt.figure()
plt.xticks(x, sent[0], rotation=45)
plt.bar(x, y)
plt.ylabel('%')
plt.title('Visualisation of words importance')
plt.show()
return output, idxs
"""
BiGRU encoder (first/last hidden states)
"""
class BGRUlastEncoder(nn.Module):
def __init__(self, config):
super(BGRUlastEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.dpout_model = config['dpout_model']
self.enc_lstm = nn.GRU(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True, dropout=self.dpout_model)
self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda()
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(2, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
_, hn = self.enc_lstm(sent_packed, self.init_lstm)
emb = torch.cat((hn[0],hn[1]), 1) # batch x 2*nhid
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
emb = emb.index_select(0, Variable(torch.cuda.LongTensor(idx_unsort)))
return emb
"""
BLSTM encoder with projection after BiLSTM
"""
class BLSTMprojEncoder(nn.Module):
def __init__(self, config):
super(BLSTMprojEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.dpout_model = config['dpout_model']
self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True, dropout=self.dpout_model)
self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda()
self.proj_enc = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False)
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(2, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed, (self.init_lstm, self.init_lstm))[0] #seqlen x batch x 2*nhid
sent_output = nn.utils.rnn.pad_packed_sequence(sent_output)[0]
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
sent_output = sent_output.index_select(1, Variable(torch.cuda.LongTensor(idx_unsort)))
sent_output = self.proj_enc(sent_output.view(-1, 2*self.enc_lstm_dim)).view(-1, bsize, 2*self.enc_lstm_dim)
# Pooling
if self.pool_type == "mean":
sent_len = Variable(torch.FloatTensor(sent_len)).unsqueeze(1).cuda()
emb = torch.sum(sent_output, 0).squeeze(0)
emb = emb / sent_len.expand_as(emb)
elif self.pool_type == "max":
emb = torch.max(sent_output, 0)[0].squeeze(0)
return emb
"""
LSTM encoder
"""
class LSTMEncoder(nn.Module):
def __init__(self, config):
super(LSTMEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.dpout_model = config['dpout_model']
self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=False, dropout=self.dpout_model)
self.init_lstm = Variable(torch.FloatTensor(1, self.bsize, self.enc_lstm_dim).zero_()).cuda()
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(1, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed, (self.init_lstm, self.init_lstm))[1][0].squeeze(0) # batch x 2*nhid
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
emb = sent_output.index_select(0, Variable(torch.cuda.LongTensor(idx_unsort)))
return emb
"""
GRU encoder
"""
class GRUEncoder(nn.Module):
def __init__(self, config):
super(GRUEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.dpout_model = config['dpout_model']
self.enc_lstm = nn.GRU(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=False, dropout=self.dpout_model)
self.init_lstm = Variable(torch.FloatTensor(1, self.bsize, self.enc_lstm_dim).zero_()).cuda()
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(1, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed, self.init_lstm)[1].squeeze(0) # batch x 2*nhid
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
emb = sent_output.index_select(0, Variable(torch.cuda.LongTensor(idx_unsort)))
return emb
"""
Inner attention from "hierarchical attention for document classification"
"""
class InnerAttentionNAACLEncoder(nn.Module):
def __init__(self, config):
super(InnerAttentionNAACLEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True)
self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda()
self.proj_key = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False)
self.proj_lstm = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False)
self.query_embedding = nn.Embedding(1, 2*self.enc_lstm_dim)
self.softmax = nn.Softmax()
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(2, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed, (self.init_lstm, self.init_lstm))[0] #seqlen x batch x 2*nhid
sent_output = nn.utils.rnn.pad_packed_sequence(sent_output)[0]
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
sent_output = sent_output.index_select(1, Variable(torch.cuda.LongTensor(idx_unsort)))
sent_output = sent_output.transpose(0,1).contiguous()
sent_output_proj = self.proj_lstm(sent_output.view(-1, 2*self.enc_lstm_dim)).view(bsize, -1, 2*self.enc_lstm_dim)
sent_key_proj = self.proj_key(sent_output.view(-1, 2*self.enc_lstm_dim)).view(bsize, -1, 2*self.enc_lstm_dim)
sent_key_proj = torch.tanh(sent_key_proj) # NAACL : u_it=tanh(W_w.h_it + b_w) like in NAACL paper (bsize, seqlen, 2nhid)
sent_w = self.query_embedding(Variable(torch.LongTensor(bsize*[0]).cuda())).unsqueeze(2) #(bsize, 2*nhid, 1)
Temp = 2
keys = sent_key_proj.bmm(sent_w).squeeze(2) / Temp
# Set probas of padding to zero in softmax
keys = keys + ((keys == 0).float()*-10000)
alphas = self.softmax(keys/Temp).unsqueeze(2).expand_as(sent_output)
if int(time.time())%100==0:
print('w', torch.max(sent_w), torch.min(sent_w))
print('alphas', alphas[0,:,0])
emb = torch.sum(alphas * sent_output_proj, 1).squeeze(1)
return emb
"""
Inner attention inspired from "Self-attentive ..."
"""
class InnerAttentionMILAEncoder(nn.Module):
def __init__(self, config):
super(InnerAttentionMILAEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True)
self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda()
self.proj_key = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False)
self.proj_lstm = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False)
self.query_embedding = nn.Embedding(2, 2*self.enc_lstm_dim)
self.softmax = nn.Softmax()
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(2, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed, (self.init_lstm, self.init_lstm))[0] #seqlen x batch x 2*nhid
sent_output = nn.utils.rnn.pad_packed_sequence(sent_output)[0]
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
sent_output = sent_output.index_select(1, Variable(torch.cuda.LongTensor(idx_unsort)))
sent_output = sent_output.transpose(0,1).contiguous()
sent_output_proj = self.proj_lstm(sent_output.view(-1, 2*self.enc_lstm_dim)).view(bsize, -1, 2*self.enc_lstm_dim)
sent_key_proj = self.proj_key(sent_output.view(-1, 2*self.enc_lstm_dim)).view(bsize, -1, 2*self.enc_lstm_dim)
sent_key_proj = torch.tanh(sent_key_proj) # NAACL : u_it=tanh(W_w.h_it + b_w) like in NAACL paper
# Temperature
Temp = 3
sent_w1 = self.query_embedding(Variable(torch.LongTensor(bsize*[0]).cuda())).unsqueeze(2) #(bsize, nhid, 1)
keys1 = sent_key_proj.bmm(sent_w1).squeeze(2) / Temp
keys1 = keys1 + ((keys1 == 0).float()*-1000)
alphas1 = self.softmax(keys1).unsqueeze(2).expand_as(sent_key_proj)
emb1 = torch.sum(alphas1 * sent_output_proj, 1).squeeze(1)
sent_w2 = self.query_embedding(Variable(torch.LongTensor(bsize*[1]).cuda())).unsqueeze(2) #(bsize, nhid, 1)
keys2 = sent_key_proj.bmm(sent_w2).squeeze(2) / Temp
keys2 = keys2 + ((keys2 == 0).float()*-1000)
alphas2 = self.softmax(keys2).unsqueeze(2).expand_as(sent_key_proj)
emb2 = torch.sum(alphas2 * sent_output_proj, 1).squeeze(1)
sent_w3 = self.query_embedding(Variable(torch.LongTensor(bsize*[1]).cuda())).unsqueeze(2) #(bsize, nhid, 1)
keys3 = sent_key_proj.bmm(sent_w3).squeeze(2) / Temp
keys3 = keys3 + ((keys3 == 0).float()*-1000)
alphas3 = self.softmax(keys3).unsqueeze(2).expand_as(sent_key_proj)
emb3 = torch.sum(alphas3 * sent_output_proj, 1).squeeze(1)
sent_w4 = self.query_embedding(Variable(torch.LongTensor(bsize*[1]).cuda())).unsqueeze(2) #(bsize, nhid, 1)
keys4 = sent_key_proj.bmm(sent_w4).squeeze(2) / Temp
keys4 = keys4 + ((keys4 == 0).float()*-1000)
alphas4 = self.softmax(keys4).unsqueeze(2).expand_as(sent_key_proj)
emb4 = torch.sum(alphas4 * sent_output_proj, 1).squeeze(1)
if int(time.time())%100==0:
print('alphas', torch.cat((alphas1.data[0,:,0], alphas2.data[0,:,0], torch.abs(alphas1.data[0,:,0] - alphas2.data[0,:,0])), 1))
emb = torch.cat((emb1, emb2, emb3, emb4), 1)
return emb
"""
Inner attention from Yang et al.
"""
class InnerAttentionYANGEncoder(nn.Module):
def __init__(self, config):
super(InnerAttentionYANGEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True)
self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda()
self.proj_lstm = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=True)
self.proj_query = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=True)
self.proj_enc = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=True)
self.query_embedding = nn.Embedding(1, 2*self.enc_lstm_dim)
self.softmax = nn.Softmax()
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
self.init_lstm = self.init_lstm if bsize == self.init_lstm.size(1) else \
Variable(torch.FloatTensor(2, bsize, self.enc_lstm_dim).zero_()).cuda()
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
sent = sent.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len)
sent_output = self.enc_lstm(sent_packed, (self.init_lstm, self.init_lstm))[0] #seqlen x batch x 2*nhid
sent_output = nn.utils.rnn.pad_packed_sequence(sent_output)[0]
# Un-sort by length
idx_unsort = np.argsort(idx_sort)
sent_output = sent_output.index_select(1, Variable(torch.cuda.LongTensor(idx_unsort)))
sent_output = sent_output.transpose(0,1).contiguous()
sent_output_proj = self.proj_lstm(sent_output.view(-1, 2*self.enc_lstm_dim)) \
.view(bsize, -1, 2*self.enc_lstm_dim)
sent_keys = self.proj_enc(sent_output.view(-1, 2*self.enc_lstm_dim)) \
.view(bsize, -1, 2*self.enc_lstm_dim)
sent_max = torch.max(sent_output, 1)[0].squeeze(1) # (bsize, 2*nhid)
sent_summary = self.proj_query(sent_max).unsqueeze(1).expand_as(sent_keys) # (bsize, seqlen, 2*nhid)
sent_M = torch.tanh(sent_keys + sent_summary) # (bsize, seqlen, 2*nhid) YANG : M = tanh(Wh_i + Wh_avg
sent_w = self.query_embedding( Variable( torch.LongTensor( bsize*[0] ).cuda() )).unsqueeze(2) # (bsize, 2*nhid, 1)
sent_alphas = self.softmax(sent_M.bmm(sent_w).squeeze(2)).unsqueeze(1) # (bsize, 1, seqlen)
if int(time.time())%200==0:
print('w', torch.max(sent_w[0]), torch.min(sent_w[0]))
print('alphas', sent_alphas[0][0][0:sent_len[0]])
# Get attention vector
emb = sent_alphas.bmm(sent_output_proj).squeeze(1)
return emb
"""
Hierarchical ConvNet
"""
class ConvNetEncoder(nn.Module):
def __init__(self, config):
super(ConvNetEncoder, self).__init__()
self.bsize = config['bsize']
self.word_emb_dim = config['word_emb_dim']
self.enc_lstm_dim = config['enc_lstm_dim']
self.pool_type = config['pool_type']
self.convnet1 = nn.Sequential(
nn.Conv1d(self.word_emb_dim, 2*self.enc_lstm_dim, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
)
self.convnet2 = nn.Sequential(
nn.Conv1d(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
)
self.convnet3 = nn.Sequential(
nn.Conv1d(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
)
self.convnet4 = nn.Sequential(
nn.Conv1d(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
)
def forward(self, sent_tuple):
# sent_len [max_len, ..., min_len] (batch) | sent Variable(seqlen x batch x worddim)
sent, sent_len = sent_tuple
bsize = sent.size(1)
sent = sent.transpose(0,1).transpose(1,2).contiguous() # batch, nhid, seqlen)
sent = self.convnet1(sent)
u1 = torch.max(sent, 2)[0].squeeze(2)
sent = self.convnet2(sent)
u2 = torch.max(sent, 2)[0].squeeze(2)
sent = self.convnet3(sent)
u3 = torch.max(sent, 2)[0].squeeze(2)
sent = self.convnet4(sent)
u4 = torch.max(sent, 2)[0].squeeze(2)
emb = torch.cat((u1, u2, u3, u4), 1)
return emb
"""
Main module for Natural Language Inference
"""
class NLINet(nn.Module):
def __init__(self, config):
super(NLINet, self).__init__()
# classifier
self.nonlinear_fc = config['nonlinear_fc']
self.fc_dim = config['fc_dim']
self.n_classes = 3
self.enc_lstm_dim = config['enc_lstm_dim']
self.encoder_type = config['encoder_type']
self.dpout_fc = config['dpout_fc']
self.encoder = eval(self.encoder_type)(config)
self.inputdim = 4*2*self.enc_lstm_dim
self.inputdim = 4*self.inputdim if self.encoder_type in \
["ConvNetEncoder", "InnerAttentionMILAEncoder"] else self.inputdim
self.inputdim = self.inputdim/2 if self.encoder_type=="LSTMEncoder" else self.inputdim
if self.nonlinear_fc:
self.classifier = nn.Sequential(
nn.Dropout(p=self.dpout_fc),
nn.Linear(self.inputdim, self.fc_dim),
nn.Tanh(),
nn.Dropout(p=self.dpout_fc),
nn.Linear(self.fc_dim, self.fc_dim),
nn.Tanh(),
nn.Dropout(p=self.dpout_fc),
nn.Linear(self.fc_dim, self.n_classes),
)
else:
self.classifier = nn.Sequential(
nn.Linear(self.inputdim, self.fc_dim),
nn.Linear(self.fc_dim, self.fc_dim),
nn.Linear(self.fc_dim, self.n_classes)
)
def forward(self, s1, s2):
# s1 : (s1, s1_len)
u = self.encoder(s1)
v = self.encoder(s2)
features = torch.cat((u, v, torch.abs(u-v), u*v), 1)
output = self.classifier(features)
return output
def encode(self, s1):
emb = self.encoder(s1)
return emb
"""
Main module for Classification
"""
class ClassificationNet(nn.Module):
def __init__(self, config):
super(ClassificationNet, self).__init__()
# classifier
self.nonlinear_fc = config['nonlinear_fc']
self.fc_dim = config['fc_dim']
self.n_classes = config['n_classes']
self.enc_lstm_dim = config['enc_lstm_dim']
self.encoder_type = config['encoder_type']
self.dpout_fc = config['dpout_fc']
self.encoder = eval(self.encoder_type)(config)
self.inputdim = 2*self.enc_lstm_dim
self.inputdim = 4*self.inputdim if self.encoder_type == "ConvNetEncoder" else self.inputdim
self.inputdim = self.enc_lstm_dim if self.encoder_type =="LSTMEncoder" else self.inputdim
self.classifier = nn.Sequential(
nn.Linear(self.inputdim, 512),
nn.Linear(512, self.n_classes),
)
def forward(self, s1):
# s1 : (s1, s1_len)
u = self.encoder(s1)
import pdb
#pdb.set_trace()
output = self.classifier(u)
return output
def encode(self, s1):
emb = self.encoder(s1)
return emb