forked from THUDM/CogQA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
373 lines (336 loc) · 15.7 KB
/
model.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
from pytorch_pretrained_bert.modeling import (
BertPreTrainedModel as PreTrainedBertModel, # The name was changed in the new versions of pytorch_pretrained_bert
BertModel,
BertLayerNorm,
gelu,
BertEncoder,
BertPooler,
)
import torch
from torch import nn
from utils import (
fuzzy_find,
find_start_end_after_tokenized,
find_start_end_before_tokenized,
bundle_part_to_batch,
)
from pytorch_pretrained_bert.tokenization import (
whitespace_tokenize,
BasicTokenizer,
BertTokenizer,
)
import re
import pdb
class MLP(nn.Module):
def __init__(self, input_sizes, dropout_prob=0.2, bias=False):
super(MLP, self).__init__()
self.layers = nn.ModuleList()
for i in range(1, len(input_sizes)):
self.layers.append(nn.Linear(input_sizes[i - 1], input_sizes[i], bias=bias))
self.norm_layers = nn.ModuleList()
if len(input_sizes) > 2:
for i in range(1, len(input_sizes) - 1):
self.norm_layers.append(nn.LayerNorm(input_sizes[i]))
self.drop_out = nn.Dropout(p=dropout_prob)
def forward(self, x):
for i, layer in enumerate(self.layers):
x = layer(self.drop_out(x))
if i < len(self.layers) - 1:
x = gelu(x)
if len(self.norm_layers):
x = self.norm_layers[i](x)
return x
class GCN(nn.Module):
def init_weights(self, module):
""" Initialize the weights.
"""
if isinstance(module, (nn.Linear, nn.Embedding)):
module.weight.data.normal_(mean=0.0, std=0.05)
def __init__(self, input_size):
super(GCN, self).__init__()
self.diffusion = nn.Linear(input_size, input_size, bias=False)
self.retained = nn.Linear(input_size, input_size, bias=False)
self.predict = MLP(input_sizes=(input_size, input_size, 1))
self.apply(self.init_weights)
def forward(self, A, x):
layer1_diffusion = A.t().mm(gelu(self.diffusion(x)))
x = gelu(self.retained(x) + layer1_diffusion)
layer2_diffusion = A.t().mm(gelu(self.diffusion(x)))
x = gelu(self.retained(x) + layer2_diffusion)
return self.predict(x).squeeze(-1)
class BertEmbeddingsPlus(nn.Module):
"""Construct the embeddings from word, position and token_type embeddings.
"""
def __init__(self, config, max_sentence_type=30):
super(BertEmbeddingsPlus, self).__init__()
self.word_embeddings = nn.Embedding(config.vocab_size, config.hidden_size)
self.position_embeddings = nn.Embedding(
config.max_position_embeddings, config.hidden_size
)
self.token_type_embeddings = nn.Embedding(
config.type_vocab_size, config.hidden_size
)
self.sentence_type_embeddings = nn.Embedding(
max_sentence_type, config.hidden_size
)
# self.LayerNorm is not snake-cased to stick with TensorFlow model variable name and be able to load
# any TensorFlow checkpoint file
self.LayerNorm = BertLayerNorm(config.hidden_size, eps=1e-12)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
def forward(self, input_ids, token_type_ids=None):
seq_length = input_ids.size(1)
position_ids = torch.arange(
seq_length, dtype=torch.long, device=input_ids.device
)
position_ids = position_ids.unsqueeze(0).expand_as(input_ids)
if token_type_ids is None:
token_type_ids = torch.zeros_like(input_ids)
words_embeddings = self.word_embeddings(input_ids)
position_embeddings = self.position_embeddings(position_ids)
token_type_embeddings = self.token_type_embeddings((token_type_ids > 0).long())
sentence_type_embeddings = self.sentence_type_embeddings(token_type_ids)
embeddings = (
words_embeddings
+ position_embeddings
+ token_type_embeddings
+ sentence_type_embeddings
)
embeddings = self.LayerNorm(embeddings)
embeddings = self.dropout(embeddings)
return embeddings
class BertModelPlus(BertModel):
def __init__(self, config):
super(BertModel, self).__init__(config)
self.embeddings = BertEmbeddingsPlus(config)
self.encoder = BertEncoder(config)
self.pooler = BertPooler(config)
self.apply(self.init_bert_weights)
def forward(
self, input_ids, token_type_ids=None, attention_mask=None, output_hidden=-4
):
if attention_mask is None:
attention_mask = torch.ones_like(input_ids)
if token_type_ids is None:
token_type_ids = torch.zeros_like(input_ids)
# We create a 3D attention mask from a 2D tensor mask.
# Sizes are [batch_size, 1, 1, to_seq_length]
# So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
# this attention mask is more simple than the triangular masking of causal attention
# used in OpenAI GPT, we just need to prepare the broadcast dimension here.
extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
# Since attention_mask is 1.0 for positions we want to attend and 0.0 for
# masked positions, this operation will create a tensor which is 0.0 for
# positions we want to attend and -10000.0 for masked positions.
# Since we are adding it to the raw scores before the softmax, this is
# effectively the same as removing these entirely.
extended_attention_mask = extended_attention_mask.to(
dtype=next(self.parameters()).dtype
) # fp16 compatibility
extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
embedding_output = self.embeddings(input_ids, token_type_ids)
encoded_layers = self.encoder(
embedding_output, extended_attention_mask, output_all_encoded_layers=True
)
sequence_output = encoded_layers[-1]
# pooled_output = self.pooler(sequence_output)
encoded_layers, hidden_layers = (
encoded_layers[-1],
encoded_layers[output_hidden],
)
return encoded_layers, hidden_layers
class BertForMultiHopQuestionAnswering(PreTrainedBertModel):
def __init__(self, config):
super(BertForMultiHopQuestionAnswering, self).__init__(config)
self.bert = BertModelPlus(config)
self.qa_outputs = nn.Linear(config.hidden_size, 4)
self.apply(self.init_bert_weights)
def forward(
self,
input_ids,
token_type_ids=None,
attention_mask=None,
sep_positions=None,
hop_start_weights=None,
hop_end_weights=None,
ans_start_weights=None,
ans_end_weights=None,
B_starts=None,
allow_limit=(0, 0),
):
""" Extract spans by System 1.
Args:
input_ids (LongTensor): Token ids of word-pieces. (batch_size * max_length)
token_type_ids (LongTensor): The A/B Segmentation in BERTs. (batch_size * max_length)
attention_mask (LongTensor): Indicating whether the position is a token or padding. (batch_size * max_length)
sep_positions (LongTensor): Positions of [SEP] tokens, mainly used in finding the num_sen of supporing facts. (batch_size * max_seps)
hop_start_weights (Tensor): The ground truth of the probability of hop start positions. The weight of sample has been added on the ground truth.
(You can verify it by examining the gradient of binary cross entropy.)
hop_end_weights ([Tensor]): The ground truth of the probability of hop end positions.
ans_start_weights ([Tensor]): The ground truth of the probability of ans start positions.
ans_end_weights ([Tensor]): The ground truth of the probability of ans end positions.
B_starts (LongTensor): Start positions of sentence B.
allow_limit (tuple, optional): An Offset for negative threshold. Defaults to (0, 0).
Returns:
[type]: [description]
"""
batch_size = input_ids.size()[0]
device = input_ids.get_device() if input_ids.is_cuda else torch.device("cpu")
sequence_output, hidden_output = self.bert(
input_ids, token_type_ids, attention_mask
)
semantics = hidden_output[:, 0]
# Some shapes: sequence_output [batch_size, max_length, hidden_size], pooled_output [batch_size, hidden_size]
if sep_positions is None:
return semantics # Only semantics, used in bundle forward
else:
max_sep = sep_positions.size()[-1]
if max_sep == 0:
empty = torch.zeros(batch_size, 0, dtype=torch.long, device=device)
return (
empty,
empty,
semantics,
empty,
) # Only semantics, used in eval, the same ``empty'' variable is a mistake in general cases but simple
# Predict spans
logits = self.qa_outputs(sequence_output)
hop_start_logits, hop_end_logits, ans_start_logits, ans_end_logits = logits.split(
1, dim=-1
)
hop_start_logits = hop_start_logits.squeeze(-1)
hop_end_logits = hop_end_logits.squeeze(-1)
ans_start_logits = ans_start_logits.squeeze(-1)
ans_end_logits = ans_end_logits.squeeze(-1) # Shape: [batch_size, max_length]
if hop_start_weights is not None: # Train mode
lgsf = torch.nn.LogSoftmax(
dim=1
) # If there is no targeted span in the sentence, start_weights = end_weights = 0(vec)
hop_start_loss = -torch.sum(
hop_start_weights * lgsf(hop_start_logits), dim=-1
)
hop_end_loss = -torch.sum(hop_end_weights * lgsf(hop_end_logits), dim=-1)
ans_start_loss = -torch.sum(
ans_start_weights * lgsf(ans_start_logits), dim=-1
)
ans_end_loss = -torch.sum(ans_end_weights * lgsf(ans_end_logits), dim=-1)
hop_loss = torch.mean((hop_start_loss + hop_end_loss)) / 2
ans_loss = torch.mean((ans_start_loss + ans_end_loss)) / 2
else:
# In eval mode, find the exact top K spans.
K_hop, K_ans = 3, 1
hop_preds = torch.zeros(
batch_size, K_hop, 3, dtype=torch.long, device=device
) # (start, end, sen_num)
ans_preds = torch.zeros(
batch_size, K_ans, 3, dtype=torch.long, device=device
)
ans_start_gap = torch.zeros(batch_size, device=device)
for u, (start_logits, end_logits, preds, K, allow) in enumerate(
(
(
hop_start_logits,
hop_end_logits,
hop_preds,
K_hop,
allow_limit[0],
),
(
ans_start_logits,
ans_end_logits,
ans_preds,
K_ans,
allow_limit[1],
),
)
):
for i in range(batch_size):
if sep_positions[i, 0] > 0:
values, indices = start_logits[i, B_starts[i] :].topk(K)
for k, index in enumerate(indices):
if values[k] <= start_logits[i, 0] - allow: # not golden
if u == 1: # For ans spans
ans_start_gap[i] = start_logits[i, 0] - values[k]
break
start = index + B_starts[i]
# find ending
for j, ending in enumerate(sep_positions[i]):
if ending > start or ending <= 0:
break
if ending <= start:
break
ending = min(ending, start + 10)
end = torch.argmax(end_logits[i, start:ending]) + start
preds[i, k, 0] = start
preds[i, k, 1] = end
preds[i, k, 2] = j
return (
(hop_loss, ans_loss, semantics)
if hop_start_weights is not None
else (hop_preds, ans_preds, semantics, ans_start_gap)
)
class CognitiveGNN(nn.Module):
def __init__(self, hidden_size):
super(CognitiveGNN, self).__init__()
self.gcn = GCN(hidden_size)
self.both_net = MLP((hidden_size, hidden_size, 1))
self.select_net = MLP((hidden_size, hidden_size, 1))
def forward(self, bundle, model, device):
batch = bundle_part_to_batch(bundle)
batch = tuple(t.to(device) for t in batch)
hop_loss, ans_loss, semantics = model(
*batch
) # Shape of semantics: [num_para, hidden_size]
num_additional_nodes = len(bundle.additional_nodes)
if num_additional_nodes > 0:
max_length_additional = max([len(x) for x in bundle.additional_nodes])
ids = torch.zeros(
(num_additional_nodes, max_length_additional),
dtype=torch.long,
device=device,
)
segment_ids = torch.zeros(
(num_additional_nodes, max_length_additional),
dtype=torch.long,
device=device,
)
input_mask = torch.zeros(
(num_additional_nodes, max_length_additional),
dtype=torch.long,
device=device,
)
for i in range(num_additional_nodes):
length = len(bundle.additional_nodes[i])
ids[i, :length] = torch.tensor(
bundle.additional_nodes[i], dtype=torch.long
)
input_mask[i, :length] = 1
additional_semantics = model(ids, segment_ids, input_mask)
semantics = torch.cat((semantics, additional_semantics), dim=0)
assert semantics.size()[0] == bundle.adj.size()[0]
if bundle.question_type == 0: # Wh-
pred = self.gcn(bundle.adj.to(device), semantics)
ce = torch.nn.CrossEntropyLoss()
final_loss = ce(
pred.unsqueeze(0),
torch.tensor([bundle.answer_id], dtype=torch.long, device=device),
)
else:
x, y, ans = bundle.answer_id
ans = torch.tensor(ans, dtype=torch.float, device=device)
diff_sem = semantics[x] - semantics[y]
classifier = self.both_net if bundle.question_type == 1 else self.select_net
final_loss = 0.2 * torch.nn.functional.binary_cross_entropy_with_logits(
classifier(diff_sem).squeeze(-1), ans.to(device)
)
return hop_loss, ans_loss, final_loss
if __name__ == "__main__":
BERT_MODEL = "bert-base-uncased"
tokenizer = BertTokenizer.from_pretrained(BERT_MODEL, do_lower_case=True)
orig_text = "".join(
[
"Theatre Centre is a UK-based theatre company touring new plays for young audiences aged 4 to 18, founded in 1953 by Brian Way, the company has developed plays by writers including which British writer, dub poet and Rastafarian?",
" It is the largest urban not-for-profit theatre company in the country and the largest in Western Canada, with productions taking place at the 650-seat Stanley Industrial Alliance Stage, the 440-seat Granville Island Stage, the 250-seat Goldcorp Stage at the BMO Theatre Centre, and on tour around the province.",
]
)
tokenized_text = tokenizer.tokenize(orig_text)
print(len(tokenized_text))