-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
552 lines (514 loc) · 28.6 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
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
import torch.nn.functional as F
import math
import os
from pytorch_pretrained_bert.file_utils import PYTORCH_PRETRAINED_BERT_CACHE
from pytorch_pretrained_bert.modeling import BertConfig, WEIGHTS_NAME, CONFIG_NAME, BertPreTrainedModel, BertModel
def create_model(args, device, config_file='', weights_file=''):
''' create squad model from args '''
ModelClass = None
if args.squad_model == 'bert_base':
print('creating bert base model')
ModelClass = SquadModel
if args.squad_model == 'bert_linear':
print('creating bert linear model')
ModelClass = SquadLinearModel
if args.squad_model == 'bert_deep':
print('creating bert deep model')
ModelClass = SquadDeepModel
if args.squad_model == 'bert_qanet':
print('creating bert qanet model')
ModelClass = SquadModelQANet
if config_file == '' and weights_file == '':
print('creating an untrained model')
return ModelClass.from_pretrained(args.bert_model,
cache_dir=os.path.join(str(PYTORCH_PRETRAINED_BERT_CACHE), 'distributed_{}'.format(args.local_rank)))
else:
print('loading a trained model')
config = BertConfig(config_file)
model = ModelClass(config)
model.load_state_dict(torch.load(weights_file, map_location=device))
return model
class SquadModel(BertPreTrainedModel):
"""BERT model for Question Answering (span extraction).
This module is composed of the BERT model with a linear layer on top of
the sequence output that computes start_logits and end_logits
Params:
`config`: a BertConfig class instance with the configuration to build a new model.
Inputs:
`input_ids`: a torch.LongTensor of shape [batch_size, sequence_length]
with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts
`extract_features.py`, `run_classifier.py` and `run_squad.py`)
`token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token
types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to
a `sentence B` token (see BERT paper for more details).
`attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices
selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max
input sequence length in the current batch. It's the mask that we typically use for attention when
a batch has varying length sentences.
`start_positions`: position of the first token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
`end_positions`: position of the last token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
Outputs:
if `start_positions` and `end_positions` are not `None`:
Outputs the total_loss which is the sum of the CrossEntropy loss for the start and end token positions.
if `start_positions` or `end_positions` is `None`:
Outputs a tuple of start_logits, end_logits which are the logits respectively for the start and end
position tokens of shape [batch_size, sequence_length].
Example usage:
```python
# Already been converted into WordPiece token ids
input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]])
input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])
token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]])
config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
model = BertForQuestionAnswering(config)
start_logits, end_logits = model(input_ids, token_type_ids, input_mask)
```
"""
def __init__(self, config):
super(SquadModel, self).__init__(config)
self.bert = BertModel(config)
# TODO check with Google if it's normal there is no dropout on the token classifier of SQuAD in the TF version
# self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.qa_outputs = nn.Linear(config.hidden_size, 2)
self.apply(self.init_bert_weights)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, start_positions=None, end_positions=None):
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
logits = self.qa_outputs(sequence_output)
start_logits, end_logits = logits.split(1, dim=-1)
start_logits = start_logits.squeeze(-1)
end_logits = end_logits.squeeze(-1)
if start_positions is not None and end_positions is not None:
# If we are on multi-GPU, split add a dimension
if len(start_positions.size()) > 1:
start_positions = start_positions.squeeze(-1)
if len(end_positions.size()) > 1:
end_positions = end_positions.squeeze(-1)
# sometimes the start/end positions are outside our model inputs, we ignore these terms
ignored_index = start_logits.size(1)
start_positions.clamp_(0, ignored_index)
end_positions.clamp_(0, ignored_index)
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
start_loss = loss_fct(start_logits, start_positions)
end_loss = loss_fct(end_logits, end_positions)
total_loss = (start_loss + end_loss) / 2
return total_loss
else:
return start_logits, end_logits
class SquadLinearModel(BertPreTrainedModel):
"""BERT model for Question Answering (span extraction).
This module is composed of the BERT model with a linear layer on top of
the sequence output that computes start_logits and end_logits
Params:
`config`: a BertConfig class instance with the configuration to build a new model.
Inputs:
`input_ids`: a torch.LongTensor of shape [batch_size, sequence_length]
with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts
`extract_features.py`, `run_classifier.py` and `run_squad.py`)
`token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token
types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to
a `sentence B` token (see BERT paper for more details).
`attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices
selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max
input sequence length in the current batch. It's the mask that we typically use for attention when
a batch has varying length sentences.
`start_positions`: position of the first token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
`end_positions`: position of the last token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
Outputs:
if `start_positions` and `end_positions` are not `None`:
Outputs the total_loss which is the sum of the CrossEntropy loss for the start and end token positions.
if `start_positions` or `end_positions` is `None`:
Outputs a tuple of start_logits, end_logits which are the logits respectively for the start and end
position tokens of shape [batch_size, sequence_length].
Example usage:
```python
# Already been converted into WordPiece token ids
input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]])
input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])
token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]])
config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
model = BertForQuestionAnswering(config)
start_logits, end_logits = model(input_ids, token_type_ids, input_mask)
```
"""
def __init__(self, config):
super(SquadLinearModel, self).__init__(config)
self.bert = BertModel(config)
# TODO check with Google if it's normal there is no dropout on the token classifier of SQuAD in the TF version
# self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.hidden1 = nn.Linear(config.hidden_size, config.hidden_size)
self.hidden2 = nn.Linear(config.hidden_size, config.hidden_size)
self.qa_outputs = nn.Linear(config.hidden_size, 2)
self.apply(self.init_bert_weights)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, start_positions=None, end_positions=None):
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
#logits = self.qa_outputs(sequence_output)
logits = self.qa_outputs(self.hidden2(self.hidden1(sequence_output)))
start_logits, end_logits = logits.split(1, dim=-1)
start_logits = start_logits.squeeze(-1)
end_logits = end_logits.squeeze(-1)
if start_positions is not None and end_positions is not None:
# If we are on multi-GPU, split add a dimension
if len(start_positions.size()) > 1:
start_positions = start_positions.squeeze(-1)
if len(end_positions.size()) > 1:
end_positions = end_positions.squeeze(-1)
# sometimes the start/end positions are outside our model inputs, we ignore these terms
ignored_index = start_logits.size(1)
start_positions.clamp_(0, ignored_index)
end_positions.clamp_(0, ignored_index)
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
start_loss = loss_fct(start_logits, start_positions)
end_loss = loss_fct(end_logits, end_positions)
total_loss = (start_loss + end_loss) / 2
return total_loss
else:
return start_logits, end_logits
class SquadDeepModel(BertPreTrainedModel):
"""BERT model for Question Answering (span extraction).
This module is composed of the BERT model with a linear layer on top of
the sequence output that computes start_logits and end_logits
Params:
`config`: a BertConfig class instance with the configuration to build a new model.
Inputs:
`input_ids`: a torch.LongTensor of shape [batch_size, sequence_length]
with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts
`extract_features.py`, `run_classifier.py` and `run_squad.py`)
`token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token
types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to
a `sentence B` token (see BERT paper for more details).
`attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices
selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max
input sequence length in the current batch. It's the mask that we typically use for attention when
a batch has varying length sentences.
`start_positions`: position of the first token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
`end_positions`: position of the last token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
Outputs:
if `start_positions` and `end_positions` are not `None`:
Outputs the total_loss which is the sum of the CrossEntropy loss for the start and end token positions.
if `start_positions` or `end_positions` is `None`:
Outputs a tuple of start_logits, end_logits which are the logits respectively for the start and end
position tokens of shape [batch_size, sequence_length].
Example usage:
```python
# Already been converted into WordPiece token ids
input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]])
input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])
token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]])
config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
model = BertForQuestionAnswering(config)
start_logits, end_logits = model(input_ids, token_type_ids, input_mask)
```
"""
def __init__(self, config):
super(SquadDeepModel, self).__init__(config)
self.bert = BertModel(config)
# TODO check with Google if it's normal there is no dropout on the token classifier of SQuAD in the TF version
self.dropout = nn.Dropout(config.hidden_dropout_prob)
d_word = 384
self.hidden1 = nn.Linear(config.hidden_size, config.hidden_size)
self.hidden2 = nn.Linear(config.hidden_size, config.hidden_size)
self.hidden3 = nn.Linear(config.hidden_size, config.hidden_size)
self.batchnorm = nn.BatchNorm1d(d_word)
self.qa_outputs = nn.Linear(config.hidden_size, 2)
self.apply(self.init_bert_weights)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, start_positions=None, end_positions=None):
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
#logits = self.qa_outputs(sequence_output)
i1 = self.hidden2(self.batchnorm(self.hidden1(sequence_output)))
#i2 = self.batchnorm(self.hidden3(sequence_output))
logits = self.qa_outputs(i1)
start_logits, end_logits = logits.split(1, dim=-1)
start_logits = start_logits.squeeze(-1)
end_logits = end_logits.squeeze(-1)
if start_positions is not None and end_positions is not None:
# If we are on multi-GPU, split add a dimension
if len(start_positions.size()) > 1:
start_positions = start_positions.squeeze(-1)
if len(end_positions.size()) > 1:
end_positions = end_positions.squeeze(-1)
# sometimes the start/end positions are outside our model inputs, we ignore these terms
ignored_index = start_logits.size(1)
start_positions.clamp_(0, ignored_index)
end_positions.clamp_(0, ignored_index)
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
start_loss = loss_fct(start_logits, start_positions)
end_loss = loss_fct(end_logits, end_positions)
total_loss = (start_loss + end_loss) / 2
return total_loss
else:
return start_logits, end_logits
##############
# constants
d_model = 96
d_word = 768
n_head = 8
dropout = 0.1 #Dropout prob across the layers
d_k = d_model // n_head
len_c = 384
len_q = 384
def mask_logits(target, mask):
return target * (1-mask) + mask * (-1e30)
class PosEncoder(nn.Module):
def __init__(self, length):
super().__init__()
freqs = torch.Tensor(
[10000 ** (-i / d_model) if i % 2 == 0 else -10000 ** ((1 - i) / d_model) for i in range(d_model)]).unsqueeze(dim=1)
phases = torch.Tensor([0 if i % 2 == 0 else math.pi / 2 for i in range(d_model)]).unsqueeze(dim=1)
pos = torch.arange(length).repeat(d_model, 1).to(torch.float)
self.pos_encoding = nn.Parameter(torch.sin(torch.add(torch.mul(pos, freqs), phases)), requires_grad=False)
def forward(self, x):
x = x + self.pos_encoding
return x
class MultiHeadAttention(nn.Module):
def __init__(self):
super().__init__()
self.q_linear = nn.Linear(d_model, d_model)
self.v_linear = nn.Linear(d_model, d_model)
self.k_linear = nn.Linear(d_model, d_model)
self.dropout = nn.Dropout(dropout)
self.fc = nn.Linear(d_model, d_model)
self.a = 1 / math.sqrt(d_k)
def forward(self, x, mask):
bs, _, l_x = x.size()
x = x.transpose(1,2)
k = self.k_linear(x).view(bs, l_x, n_head, d_k)
q = self.q_linear(x).view(bs, l_x, n_head, d_k)
v = self.v_linear(x).view(bs, l_x, n_head, d_k)
q = q.permute(2, 0, 1, 3).contiguous().view(bs*n_head, l_x, d_k)
k = k.permute(2, 0, 1, 3).contiguous().view(bs*n_head, l_x, d_k)
v = v.permute(2, 0, 1, 3).contiguous().view(bs*n_head, l_x, d_k)
mask = mask.unsqueeze(1).expand(-1, l_x, -1).repeat(n_head, 1, 1)
attn = torch.bmm(q, k.transpose(1, 2)) * self.a
attn = mask_logits(attn, mask)
attn = F.softmax(attn, dim=2)
attn = self.dropout(attn)
out = torch.bmm(attn, v)
out = out.view(n_head, bs, l_x, d_k).permute(1,2,0,3).contiguous().view(bs, l_x, d_model)
out = self.fc(out)
out = self.dropout(out)
return out.transpose(1,2)
class DepthwiseSeparableConv(nn.Module):
def __init__(self, in_ch, out_ch, k, dim=1, bias=True):
super().__init__()
if dim == 1:
self.depthwise_conv = nn.Conv1d(in_channels=in_ch, out_channels=in_ch, kernel_size=k, groups=in_ch,
padding=k // 2, bias=bias)
self.pointwise_conv = nn.Conv1d(in_channels=in_ch, out_channels=out_ch, kernel_size=1, padding=0, bias=bias)
elif dim == 2:
self.depthwise_conv = nn.Conv2d(in_channels=in_ch, out_channels=in_ch, kernel_size=k, groups=in_ch,
padding=k // 2, bias=bias)
self.pointwise_conv = nn.Conv2d(in_channels=in_ch, out_channels=out_ch, kernel_size=1, padding=0, bias=bias)
else:
raise Exception("Wrong dimension for Depthwise Separable Convolution!")
nn.init.kaiming_normal_(self.depthwise_conv.weight)
nn.init.constant_(self.depthwise_conv.bias, 0.0)
nn.init.kaiming_normal_(self.depthwise_conv.weight)
nn.init.constant_(self.pointwise_conv.bias, 0.0)
def forward(self, x):
return self.pointwise_conv(self.depthwise_conv(x))
class EncoderBlock(nn.Module):
def __init__(self, conv_num: int, ch_num: int, k: int, length: int):
super().__init__()
self.convs = nn.ModuleList([DepthwiseSeparableConv(ch_num, ch_num, k) for _ in range(conv_num)])
self.self_att = MultiHeadAttention()
self.fc = nn.Linear(ch_num, ch_num, bias=True)
self.pos = PosEncoder(length)
# self.norm = nn.LayerNorm([d_model, length])
self.normb = nn.LayerNorm([d_model, length])
self.norms = nn.ModuleList([nn.LayerNorm([d_model, length]) for _ in range(conv_num)])
self.norme = nn.LayerNorm([d_model, length])
self.L = conv_num
def forward(self, x, mask):
out = self.pos(x)
res = out
out = self.normb(out)
for i, conv in enumerate(self.convs):
out = conv(out)
out = F.relu(out)
out = out + res
if (i + 1) % 2 == 0:
p_drop = dropout * (i + 1) / self.L
out = F.dropout(out, p=p_drop, training=self.training)
res = out
out = self.norms[i](out)
# print("Before attention: {}".format(out.size()))
out = self.self_att(out, mask)
# print("After attention: {}".format(out.size()))
out = out + res
out = F.dropout(out, p=dropout, training=self.training)
res = out
out = self.norme(out)
out = self.fc(out.transpose(1, 2)).transpose(1, 2)
out = F.relu(out)
out = out + res
out = F.dropout(out, p=dropout, training=self.training)
return out
class CQAttention(nn.Module):
def __init__(self):
super().__init__()
w = torch.empty(d_model * 3)
lim = 1 / d_model
nn.init.uniform_(w, -math.sqrt(lim), math.sqrt(lim))
self.w = nn.Parameter(w)
def forward(self, C, Q, cmask, qmask):
ss = []
C = C.transpose(1, 2)
Q = Q.transpose(1, 2)
cmask = cmask.unsqueeze(2)
qmask = qmask.unsqueeze(1)
shape = (C.size(0), C.size(1), Q.size(1), C.size(2))
Ct = C.unsqueeze(2).expand(shape)
Qt = Q.unsqueeze(1).expand(shape)
CQ = torch.mul(Ct, Qt)
S = torch.cat([Ct, Qt, CQ], dim=3)
S = torch.matmul(S, self.w)
S1 = F.softmax(mask_logits(S, qmask), dim=2)
S2 = F.softmax(mask_logits(S, cmask), dim=1)
A = torch.bmm(S1, Q)
B = torch.bmm(torch.bmm(S1, S2.transpose(1, 2)), C)
out = torch.cat([C, A, torch.mul(C, A), torch.mul(C, B)], dim=2)
out = F.dropout(out, p=dropout, training=self.training)
return out.transpose(1, 2)
class Pointer(nn.Module):
def __init__(self):
super().__init__()
w1 = torch.empty(d_model * 2)
w2 = torch.empty(d_model * 2)
lim = 3 / (2 * d_model)
nn.init.uniform_(w1, -math.sqrt(lim), math.sqrt(lim))
nn.init.uniform_(w2, -math.sqrt(lim), math.sqrt(lim))
self.w1 = nn.Parameter(w1)
self.w2 = nn.Parameter(w2)
self.Linear1 = nn.Linear(len_c, len_c)
self.Linear2 = nn.Linear(len_c, len_c)
def forward(self, M1, M2, M3, mask):
X1 = torch.cat([M1, M2], dim=1)
X2 = torch.cat([M1, M3], dim=1)
Y1 = torch.matmul(self.w1, X1)
Y2 = torch.matmul(self.w2, X2)
p1 = self.Linear1(Y1)
p2 = self.Linear2(Y2)
Y1 = mask_logits(Y1, mask)
Y2 = mask_logits(Y2, mask)
#p1 = F.log_softmax(Y1, dim=1)
#p2 = F.log_softmax(Y2, dim=1)
return p1, p2
##############
class SquadModelQANet(BertPreTrainedModel):
"""BERT model for Question Answering (span extraction).
This module is composed of the BERT model with a linear layer on top of
the sequence output that computes start_logits and end_logits
Params:
`config`: a BertConfig class instance with the configuration to build a new model.
Inputs:
`input_ids`: a torch.LongTensor of shape [batch_size, sequence_length]
with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts
`extract_features.py`, `run_classifier.py` and `run_squad.py`)
`token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token
types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to
a `sentence B` token (see BERT paper for more details).
`attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices
selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max
input sequence length in the current batch. It's the mask that we typically use for attention when
a batch has varying length sentences.
`start_positions`: position of the first token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
`end_positions`: position of the last token for the labeled span: torch.LongTensor of shape [batch_size].
Positions are clamped to the length of the sequence and position outside of the sequence are not taken
into account for computing the loss.
Outputs:
if `start_positions` and `end_positions` are not `None`:
Outputs the total_loss which is the sum of the CrossEntropy loss for the start and end token positions.
if `start_positions` or `end_positions` is `None`:
Outputs a tuple of start_logits, end_logits which are the logits respectively for the start and end
position tokens of shape [batch_size, sequence_length].
Example usage:
```python
# Already been converted into WordPiece token ids
input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]])
input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])
token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]])
config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
model = BertForQuestionAnswering(config)
start_logits, end_logits = model(input_ids, token_type_ids, input_mask)
```
"""
def __init__(self, config):
super(SquadModelQANet, self).__init__(config)
self.bert = BertModel(config)
# TODO check with Google if it's normal there is no dropout on the token classifier of SQuAD in the TF version
# self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.qa_outputs = nn.Linear(config.hidden_size, 2)
self.apply(self.init_bert_weights)
# Models of squad2
self.context_conv = DepthwiseSeparableConv(d_word,d_model, 5)
self.question_conv = DepthwiseSeparableConv(d_word,d_model, 5)
self.c_emb_enc = EncoderBlock(conv_num=4, ch_num=d_model, k=7, length=len_c)
self.q_emb_enc = EncoderBlock(conv_num=4, ch_num=d_model, k=7, length=len_q)
self.cq_att = CQAttention()
self.cq_resizer = DepthwiseSeparableConv(d_model * 4, d_model, 5)
enc_blk = EncoderBlock(conv_num=2, ch_num=d_model, k=5, length=len_c)
self.model_enc_blks = nn.ModuleList([enc_blk] * 7)
self.out = Pointer()
def forward(self, input_ids, token_type_ids=None, attention_mask=None, start_positions=None, end_positions=None):
#sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
# create question and context tensors
context_mask = token_type_ids * attention_mask
question_mask = ((1-context_mask) * attention_mask)
question_output, _ = self.bert(input_ids, token_type_ids, question_mask, output_all_encoded_layers=False)
context_output, _ = self.bert(input_ids, token_type_ids, context_mask, output_all_encoded_layers=False)
question_output = question_output.permute(0, 2, 1).float()
context_output = context_output.permute(0, 2, 1).float()
context_mask = context_mask.float()
question_mask = question_mask.float()
C = self.context_conv(context_output)
Q = self.question_conv(question_output)
Ce = self.c_emb_enc(C, context_mask)
Qe = self.q_emb_enc(Q, question_mask)
X = self.cq_att(Ce, Qe, context_mask, question_mask)
M1 = self.cq_resizer(X)
for enc in self.model_enc_blks: M1 = enc(M1, context_mask)
M2 = M1
for enc in self.model_enc_blks: M2 = enc(M2, context_mask)
M3 = M2
for enc in self.model_enc_blks: M3 = enc(M3, context_mask)
start_logits, end_logits = self.out(M1, M2, M3, context_mask)
start_logits = start_logits.squeeze(-1)
end_logits = end_logits.squeeze(-1)
if start_positions is not None and end_positions is not None:
# If we are on multi-GPU, split add a dimension
if len(start_positions.size()) > 1:
start_positions = start_positions.squeeze(-1)
if len(end_positions.size()) > 1:
end_positions = end_positions.squeeze(-1)
# sometimes the start/end positions are outside our model inputs, we ignore these terms
ignored_index = start_logits.size(1)
start_positions.clamp_(0, ignored_index)
end_positions.clamp_(0, ignored_index)
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
start_loss = loss_fct(start_logits, start_positions)
end_loss = loss_fct(end_logits, end_positions)
total_loss = (start_loss + end_loss) / 2
return total_loss
else:
return start_logits, end_logits