-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
325 lines (248 loc) · 15.4 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
"""Top-level model classes.
Author:
Chris Chute (chute@stanford.edu)
"""
import layers
import torch
import torch.nn as nn
import pdb
class BiDAF(nn.Module):
"""Baseline BiDAF model for SQuAD.
Based on the paper:
"Bidirectional Attention Flow for Machine Comprehension"
by Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, Hannaneh Hajishirzi
(https://arxiv.org/abs/1611.01603).
Follows a high-level structure commonly found in SQuAD models:
- Embedding layer: Embed word indices to get word vectors.
- Encoder layer: Encode the embedded sequence.
- Attention layer: Apply an attention mechanism to the encoded sequence.
- Model encoder layer: Encode the sequence again.
- Output layer: Simple layer (e.g., fc + softmax) to get final outputs.
Args:
word_vectors (torch.Tensor): Pre-trained word vectors.
hidden_size (int): Number of features in the hidden state at each layer.
drop_prob (float): Dropout probability.
"""
def __init__(self, word_vectors, hidden_size, drop_prob=0.):
super(BiDAF, self).__init__()
self.emb = layers.Embedding(word_vectors=word_vectors,
hidden_size=hidden_size,
drop_prob=drop_prob)
self.enc = layers.RNNEncoder(input_size=hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob)
self.att = layers.BiDAFAttention(hidden_size=2 * hidden_size,
drop_prob=drop_prob)
self.mod = layers.RNNEncoder(input_size=8 * hidden_size,
hidden_size=hidden_size,
num_layers=2,
drop_prob=drop_prob)
self.out = layers.BiDAFOutput(hidden_size=hidden_size,
drop_prob=drop_prob)
def forward(self, cw_idxs, qw_idxs):
c_mask = torch.zeros_like(cw_idxs) != cw_idxs
q_mask = torch.zeros_like(qw_idxs) != qw_idxs
c_len, q_len = c_mask.sum(-1), q_mask.sum(-1)
c_emb = self.emb(cw_idxs) # (batch_size, c_len, hidden_size)
q_emb = self.emb(qw_idxs) # (batch_size, q_len, hidden_size)
c_enc = self.enc(c_emb, c_len) # (batch_size, c_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_len) # (batch_size, q_len, 2 * hidden_size)
att = self.att(c_enc, q_enc,
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
mod = self.mod(att, c_len) # (batch_size, c_len, 2 * hidden_size)
out = self.out(att, mod, c_mask) # 2 tensors, each (batch_size, c_len)
return out
class BiDAF_char(nn.Module):
"""Baseline BiDAF model for SQuAD 2.0 with both the word-level and the character-level embedding.
Based on the paper:
"Bidirectional Attention Flow for Machine Comprehension"
by Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, Hannaneh Hajishirzi
(https://arxiv.org/abs/1611.01603).
Follows a high-level structure commonly found in SQuAD models:
- Embedding layer: Embed word indices and character indices to get word vectors.
- Encoder layer: Encode the embedded sequence.
- Attention layer: Apply an attention mechanism to the encoded sequence.
- Model encoder layer: Encode the sequence again.
- Output layer: Simple layer (e.g., fc + softmax) to get final outputs.
Args:
word_vectors (torch.Tensor): Pre-trained word vectors.
char_vectors (torch.Tensor): Random character vectors.
hidden_size (int): Number of features in the hidden state at each layer.
drop_prob (float): Dropout probability.
"""
def __init__(self, word_vectors, char_vectors, hidden_size, drop_prob=0.):
super(BiDAF_char, self).__init__()
self.emb = layers.Embedding_Char(word_vectors=word_vectors,
char_vectors=char_vectors,
hidden_size=hidden_size,
drop_prob=drop_prob)
self.enc = layers.RNNEncoder(input_size=hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob)
self.att = layers.BiDAFAttention(hidden_size=2 * hidden_size,
drop_prob=drop_prob)
self.mod = layers.RNNEncoder(input_size=8 * hidden_size,
hidden_size=hidden_size,
num_layers=2,
drop_prob=drop_prob)
self.out = layers.BiDAFOutput(hidden_size=hidden_size,
drop_prob=drop_prob)
def forward(self, cw_idxs, qw_idxs, cc_idxs, qc_idxs):
c_mask = torch.zeros_like(cw_idxs) != cw_idxs
q_mask = torch.zeros_like(qw_idxs) != qw_idxs
c_len, q_len = c_mask.sum(-1), q_mask.sum(-1)
c_emb = self.emb(cw_idxs, cc_idxs)# (batch_size, c_len, hidden_size)
q_emb = self.emb(qw_idxs, qc_idxs)# (batch_size, q_len, hidden_size)
c_enc = self.enc(c_emb, c_len) # (batch_size, c_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_len) # (batch_size, q_len, 2 * hidden_size)
att = self.att(c_enc, q_enc,
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
mod = self.mod(att, c_len) # (batch_size, c_len, 2 * hidden_size)
out = self.out(att, mod, c_mask) # 2 tensors, each (batch_size, c_len)
return out
class BiDAF_tag(nn.Module):
"""Baseline BiDAF model for SQuAD 2.0 with both the word-level and the character-level embedding, and the tag (POS, NER) features.
Based on the paper:
"Bidirectional Attention Flow for Machine Comprehension"
by Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, Hannaneh Hajishirzi
(https://arxiv.org/abs/1611.01603).
Follows a high-level structure commonly found in SQuAD models:
- Embedding layer: Embed word indices and character indices to get word vectors.
- Encoder layer: Encode the embedded sequence.
- Attention layer: Apply an attention mechanism to the encoded sequence.
- Model encoder layer: Encode the sequence again.
- Output layer: Simple layer (e.g., fc + softmax) to get final outputs.
Args:
word_vectors (torch.Tensor): Pre-trained word vectors. # (word_vocab_size, word_emb_size)
char_vectors (torch.Tensor): Random character vectors. # (char_vocab_size, char_emb_size)
pos_vectors (torch.Tensor): one-hot encoding POS vectors. # (n_pos_classes, n_pos_classes)
ner_vectors (torch.Tensor): one-hot encoding NER vectors. # (n_ner_classes, n_ner_classes)
hidden_size (int): Number of features in the hidden state at each layer.
drop_prob (float): Dropout probability.
freeze_tag(bool): Whether to freeze the tag features embeddings
"""
def __init__(self, word_vectors, char_vectors, pos_vectors, ner_vectors, hidden_size, drop_prob=0., freeze_tag=True):
super(BiDAF_tag, self).__init__()
self.emb = layers.Embedding_Tag(word_vectors=word_vectors,
char_vectors=char_vectors,
pos_vectors = pos_vectors,
ner_vectors = ner_vectors,
hidden_size=hidden_size,
drop_prob=drop_prob,
freeze_tag=freeze_tag)
self.enc = layers.RNNEncoder(input_size=hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob)
self.att = layers.BiDAFAttention(hidden_size=2 * hidden_size,
drop_prob=drop_prob)
self.mod = layers.RNNEncoder(input_size=8 * hidden_size,
hidden_size=hidden_size,
num_layers=2,
drop_prob=drop_prob)
self.out = layers.BiDAFOutput(hidden_size=hidden_size,
drop_prob=drop_prob)
def forward(self, cw_idxs, qw_idxs, cc_idxs, qc_idxs, cpos_idxs, qpos_idxs, cner_idxs, qner_idxs):
c_mask = torch.zeros_like(cw_idxs) != cw_idxs
q_mask = torch.zeros_like(qw_idxs) != qw_idxs
c_len, q_len = c_mask.sum(-1), q_mask.sum(-1)
c_emb = self.emb(cw_idxs, cc_idxs, cpos_idxs, cner_idxs)# (batch_size, c_len, hidden_size)
q_emb = self.emb(qw_idxs, qc_idxs, qpos_idxs, qner_idxs)# (batch_size, q_len, hidden_size)
c_enc = self.enc(c_emb, c_len) # (batch_size, c_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_len) # (batch_size, q_len, 2 * hidden_size)
att = self.att(c_enc, q_enc,
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
mod = self.mod(att, c_len) # (batch_size, c_len, 2 * hidden_size)
out = self.out(att, mod, c_mask) # 2 tensors, each (batch_size, c_len)
return out
class BiDAF_tag_ext(nn.Module):
"""Baseline BiDAF model for SQuAD 2.0 with both the word-level and the character-level embedding, and the tag (POS, NER, EM, TF) features.
Based on the paper:
"Bidirectional Attention Flow for Machine Comprehension"
by Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, Hannaneh Hajishirzi
(https://arxiv.org/abs/1611.01603).
Follows a high-level structure commonly found in SQuAD models:
- Embedding layer: Embed word indices and character indices to get word vectors.
- Encoder layer: Encode the embedded sequence.
- Attention layer: Apply an attention mechanism to the encoded sequence.
- Model encoder layer: Encode the sequence again.
- Output layer: Simple layer (e.g., fc + softmax) to get final outputs.
Args:
word_vectors (torch.Tensor): Pre-trained word vectors. # (word_vocab_size, word_emb_size)
char_vectors (torch.Tensor): Random character vectors. # (char_vocab_size, char_emb_size)
pos_vectors (torch.Tensor): one-hot encoding POS vectors. # (n_pos_classes, n_pos_classes)
ner_vectors (torch.Tensor): one-hot encoding NER vectors. # (n_ner_classes, n_ner_classes)
hidden_size (int): Number of features in the hidden state at each layer.
drop_prob (float): Dropout probability.
freeze_tag(bool): Whether to freeze the tag features embeddings
"""
def __init__(self, word_vectors, char_vectors, pos_vectors, ner_vectors, hidden_size, drop_prob=0., freeze_tag=True):
super(BiDAF_tag_ext, self).__init__()
self.emb = layers.Embedding_Tag_Ext(word_vectors=word_vectors,
char_vectors=char_vectors,
pos_vectors = pos_vectors,
ner_vectors = ner_vectors,
hidden_size=hidden_size,
drop_prob=drop_prob,
freeze_tag=freeze_tag)
self.enc = layers.RNNEncoder(input_size=hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob)
self.att = layers.BiDAFAttention(hidden_size=2 * hidden_size,
drop_prob=drop_prob)
self.mod = layers.RNNEncoder(input_size=8 * hidden_size,
hidden_size=hidden_size,
num_layers=2,
drop_prob=drop_prob)
self.out = layers.BiDAFOutput(hidden_size=hidden_size,
drop_prob=drop_prob)
def forward(self, cw_idxs, qw_idxs, cc_idxs, qc_idxs, cpos_idxs, qpos_idxs, cner_idxs, qner_idxs, cw_ems, qw_ems, cw_tfs, qw_tfs):
c_mask = torch.zeros_like(cw_idxs) != cw_idxs
q_mask = torch.zeros_like(qw_idxs) != qw_idxs
c_len, q_len = c_mask.sum(-1), q_mask.sum(-1)
c_emb = self.emb(cw_idxs, cc_idxs, cpos_idxs, cner_idxs, cw_ems, cw_tfs)# (batch_size, c_len, hidden_size)
q_emb = self.emb(qw_idxs, qc_idxs, qpos_idxs, qner_idxs, qw_ems, qw_tfs)# (batch_size, q_len, hidden_size)
c_enc = self.enc(c_emb, c_len) # (batch_size, c_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_len) # (batch_size, q_len, 2 * hidden_size)
att = self.att(c_enc, q_enc,
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
mod = self.mod(att, c_len) # (batch_size, c_len, 2 * hidden_size)
out = self.out(att, mod, c_mask) # 2 tensors, each (batch_size, c_len)
return out
class CoattentionModel(nn.Module):
def __init__(self, hidden_dim, embedding_matrix, train_word_embeddings, dropout, pooling_size, number_of_iters, number_of_layers):
super(CoattentionModel, self).__init__()
self.Encoder = layers.Encoder(hidden_dim, embedding_matrix, train_word_embeddings, dropout, number_of_layers)
self.Coattention_Encoder = layers.Coattention_Encoder(hidden_dim, dropout, number_of_layers)
self.Decoder = layers.Decoder(hidden_dim, pooling_size, number_of_iters, dropout)
def forward(self, max_p_length, max_q_length, batch_passages, batch_questions, batch_p_lengths, batch_q_lengths, number_of_examples, apply_batch_norm, istraining):
#max_p_length_batch = torch.max(batch_p_lengths).item()
#max_q_length_batch = torch.max(batch_q_lengths).item()
#if max_p_length_batch < max_p_length:
# batch_passages = batch_passages[:, :max_p_length_batch]
# max_p_length = max_p_length_batch
#if max_q_length_batch < max_q_length:
# batch_questions = batch_questions[:, :max_q_length_batch]
# max_q_length = max_q_length_batch
passage_representation = self.Encoder.forward(max_p_length, batch_passages, batch_p_lengths, number_of_examples, apply_batch_norm, istraining, isquestion=False)
question_representation = self.Encoder.forward(max_q_length, batch_questions, batch_q_lengths, number_of_examples, apply_batch_norm, istraining, isquestion=True)
'''
In passage_length_index and question_length_index-> corresponding elements denote same passage question pair
'''
u_matrix = self.Coattention_Encoder.forward(question_representation,
batch_q_lengths.clone(),
passage_representation,
batch_p_lengths.clone(),
number_of_examples,
apply_batch_norm,
istraining)
# size is batch*(2*hidden)*passage_lens
alphas, betas = self.Decoder.forward(u_matrix,
number_of_examples,
batch_p_lengths.clone(),
apply_batch_norm,
istraining)
return alphas, betas#start_outputs, end_outputs, entropies