-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
324 lines (232 loc) · 12.9 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
"""Top-level model classes.
Author:
Chris Chute (chute@stanford.edu)
"""
import layers
import torch
import torch.nn as nn
import util
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, char_vectors, hidden_size, num_heads=8, drop_prob=0.):
super(BiDAF, self).__init__()
self.emb = layers.Embedding(word_vectors=word_vectors,
char_vectors=char_vectors,
hidden_size=hidden_size,
drop_prob=drop_prob)
hidden_size *= 2 # update hidden size for other layers due to char embeddings
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)
# (batch_size, c_len, hidden_size)
c_emb = self.emb(cw_idxs, cc_idxs)
# (batch_size, q_len, hidden_size)
q_emb = self.emb(qw_idxs, qc_idxs)
# (batch_size, c_len, 2 * hidden_size)
c_enc = self.enc(c_emb, c_len)
# (batch_size, q_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_len)
att = self.att(c_enc, q_enc,
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
# (batch_size, c_len, 2 * hidden_size)
mod = self.mod(att, c_len)
out = self.out(att, mod, c_mask) # 2 tensors, each (batch_size, c_len)
return out
class SketchyReader(nn.Module):
def __init__(self, word_vectors, char_vectors, hidden_size, num_heads, char_embed_drop_prob, drop_prob=0.1):
super(SketchyReader, self).__init__()
'''class QANet(nn.Module):
def __init__(self, word_vectors, char_vectors, hidden_size, device, drop_prob=0.):
super(QANet, self).__init__()
self.device = device'''
self.emb = layers.Embedding(word_vectors=word_vectors,
char_vectors=char_vectors,
hidden_size=hidden_size,
char_embed_drop_prob=char_embed_drop_prob,
word_embed_drop_prob=drop_prob)
hidden_size *= 2 # update hidden size for other layers due to char embeddings
self.c_resizer = layers.Initialized_Conv1d(hidden_size, 128)
self.q_resizer = layers.Initialized_Conv1d(hidden_size, 128)
self.model_resizer = layers.Initialized_Conv1d(512, 128)
self.enc = layers.StackedEncoder(num_conv_blocks=4,
kernel_size=7,
num_heads=num_heads,
dropout=drop_prob) # embedding encoder layer
self.att = layers.BiDAFAttention(hidden_size=128,
drop_prob=drop_prob) # context-query attention layer
# self.mod1 = layers.StackedEncoder(num_conv_blocks=2,
# kernel_size=7,
# dropout=drop_prob) # model layer
# self.mod2 = layers.StackedEncoder(num_conv_blocks=2,
# kernel_size=7,
# dropout=drop_prob) # model layer
# self.mod3 = layers.StackedEncoder(num_conv_blocks=2,
# kernel_size=7,
# dropout=drop_prob) # model layer
self.model_encoder_layers = nn.ModuleList([layers.StackedEncoder(num_conv_blocks=2,
kernel_size=7,
dropout=drop_prob) for _ in range(7)])
self.out = layers.SketchyOutput(hidden_size=128) # output layer
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_mask_3d = torch.eq(cw_idxs, 1).float()
# q_mask_3d = torch.eq(qw_idxs, 1).float()
# (batch_size, c_len, hidden_size)
c_emb = self.emb(cw_idxs, cc_idxs)
# (batch_size, q_len, hidden_size)
q_emb = self.emb(qw_idxs, qc_idxs)
c_emb = self.c_resizer(c_emb.transpose(1, 2))
q_emb = self.q_resizer(q_emb.transpose(1, 2))
# (batch_size, c_len, 2 * hidden_size)
c_enc = self.enc(c_emb, c_mask, 1, 1)
# (batch_size, q_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_mask, 1, 1)
att = self.att(c_enc.transpose(1, 2), q_enc.transpose(1, 2),
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
att = att.transpose(1, 2)
att = self.model_resizer(att)
mod1 = att
for i, layer in enumerate(self.model_encoder_layers):
mod1 = layer(mod1, c_mask, i*(2+2)+1, 7)
mod2 = mod1
for i, layer in enumerate(self.model_encoder_layers):
mod2 = layer(mod2, c_mask, i*(2+2)+1, 7)
mod3 = mod2
for i, layer in enumerate(self.model_encoder_layers):
mod3 = layer(mod3, c_mask, i*(2+2)+1, 7)
# mod1 = self.mod1(att) # (batch_size, c_len, 2 * hidden_size)
# mod2 = self.mod2(mod1) # (batch_size, c_len, 2 * hidden_size)
# mod3 = self.mod3(mod2) # (batch_size, c_len, 2 * hidden_size)
out = self.out(mod1, mod2, mod3, c_mask)
return out
class IntensiveReader(nn.Module):
def __init__(self, word_vectors, char_vectors, hidden_size, num_heads, char_embed_drop_prob, drop_prob=0.):
super(IntensiveReader, self).__init__()
'''class QANet(nn.Module):
def __init__(self, word_vectors, char_vectors, hidden_size, device, drop_prob=0.):
super(QANet, self).__init__()
self.device = device'''
self.emb = layers.Embedding(word_vectors=word_vectors,
char_vectors=char_vectors,
hidden_size=hidden_size,
char_embed_drop_prob=char_embed_drop_prob,
word_embed_drop_prob=drop_prob)
hidden_size *= 2 # update hidden size for other layers due to char embeddings
self.c_resizer = layers.Initialized_Conv1d(hidden_size, 128)
self.q_resizer = layers.Initialized_Conv1d(hidden_size, 128)
self.model_resizer = layers.Initialized_Conv1d(512, 128)
self.enc = layers.StackedEncoder(num_conv_blocks=4,
kernel_size=7,
num_heads=num_heads,
dropout=drop_prob) # embedding encoder layer
self.att = layers.BiDAFAttention(hidden_size=128,
drop_prob=drop_prob) # context-query attention layer
# self.mod1 = layers.StackedEncoder(num_conv_blocks=2,
# kernel_size=7,
# dropout=drop_prob) # model layer
# self.mod2 = layers.StackedEncoder(num_conv_blocks=2,
# kernel_size=7,
# dropout=drop_prob) # model layer
# self.mod3 = layers.StackedEncoder(num_conv_blocks=2,
# kernel_size=7,
# dropout=drop_prob) # model layer
self.model_encoder_layers = nn.ModuleList([layers.StackedEncoder(num_conv_blocks=2,
kernel_size=7,
dropout=drop_prob) for _ in range(7)])
self.out = layers.IntensiveOutput(hidden_size=128) # output layer
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_mask_3d = torch.eq(cw_idxs, 1).float()
# q_mask_3d = torch.eq(qw_idxs, 1).float()
# (batch_size, c_len, hidden_size)
c_emb = self.emb(cw_idxs, cc_idxs)
# (batch_size, q_len, hidden_size)
q_emb = self.emb(qw_idxs, qc_idxs)
c_emb = self.c_resizer(c_emb.transpose(1, 2))
q_emb = self.q_resizer(q_emb.transpose(1, 2))
# (batch_size, c_len, 2 * hidden_size)
c_enc = self.enc(c_emb, c_mask, 1, 1)
# (batch_size, q_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_mask, 1, 1)
att = self.att(c_enc.transpose(1, 2), q_enc.transpose(1, 2),
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
att = att.transpose(1, 2)
att = self.model_resizer(att)
mod1 = att
for i, layer in enumerate(self.model_encoder_layers):
mod1 = layer(mod1, c_mask, i*(2+2)+1, 7)
mod2 = mod1
for i, layer in enumerate(self.model_encoder_layers):
mod2 = layer(mod2, c_mask, i*(2+2)+1, 7)
mod3 = mod2
for i, layer in enumerate(self.model_encoder_layers):
mod3 = layer(mod3, c_mask, i*(2+2)+1, 7)
# mod1 = self.mod1(att) # (batch_size, c_len, 2 * hidden_size)
# mod2 = self.mod2(mod1) # (batch_size, c_len, 2 * hidden_size)
# mod3 = self.mod3(mod2) # (batch_size, c_len, 2 * hidden_size)
out = self.out(mod1, mod2, mod3, c_mask)
return out
class RetroQANet(nn.Module):
"""Retro-Reader over QANet
"""
def __init__(self, word_vectors, char_vectors, hidden_size, intensive_path, num_heads, sketchy_path, gpu_ids, char_embed_drop_prob, drop_prob=0.):
super(RetroQANet, self).__init__()
self.sketchy = SketchyReader(word_vectors=word_vectors,
char_vectors=char_vectors,
hidden_size=hidden_size,
num_heads=num_heads,
char_embed_drop_prob=char_embed_drop_prob,
drop_prob=drop_prob)
self.sketchy = nn.DataParallel(self.sketchy, gpu_ids)
self.sketchy, _ = util.load_model(self.sketchy, sketchy_path, gpu_ids)
self.intensive = IntensiveReader(word_vectors=word_vectors,
char_vectors=char_vectors,
num_heads=num_heads,
char_embed_drop_prob=char_embed_drop_prob,
hidden_size=hidden_size,
drop_prob=drop_prob)
self.intensive = nn.DataParallel(self.intensive, gpu_ids)
self.intensive, _ = util.load_model(
self.intensive, intensive_path, gpu_ids)
self.RV_TAV = layers.RV_TAV()
def forward(self, cw_idxs, qw_idxs, cc_idxs, qc_idxs):
self.sketchy.eval()
self.intensive.eval()
yi_s = self.sketchy(cw_idxs, qw_idxs, cc_idxs, qc_idxs)
yi_i, log_p1, log_p2 = self.intensive(
cw_idxs, qw_idxs, cc_idxs, qc_idxs)
out = self.RV_TAV(yi_s.to(device='cuda:0'), yi_i.to(
device='cuda:0'), log_p1.to(device='cuda:0'), log_p2.to(device='cuda:0'))
return out