-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
378 lines (274 loc) · 15.9 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
373
374
375
376
377
378
import torch
from torch import nn
from transformers.modeling_electra import ElectraPreTrainedModel, ElectraModel
from torch.nn import CrossEntropyLoss, BCEWithLogitsLoss, MSELoss, L1Loss
import torch.nn.functional as F
def masked_softmax(vector, mask, dim=-1, memory_efficient=False, mask_fill_value=-1e32):
if mask is None:
result = torch.nn.functional.softmax(vector, dim=dim)
else:
mask = mask.float()
while mask.dim() < vector.dim():
mask = mask.unsqueeze(1)
if not memory_efficient:
result = torch.nn.functional.softmax(vector * mask, dim=dim)
result = result * mask
result = result / (result.sum(dim=dim, keepdim=True) + 1e-13)
else:
masked_vector = vector.masked_fill((1 - mask).to(dtype=torch.bool), mask_fill_value)
result = torch.nn.functional.softmax(masked_vector, dim=dim)
return result
def weighted_sum(matrix, attention):
if attention.dim() == 2 and matrix.dim() == 3:
return attention.unsqueeze(1).bmm(matrix).squeeze(1)
if attention.dim() == 3 and matrix.dim() == 3:
return attention.bmm(matrix)
if matrix.dim() - 1 < attention.dim():
expanded_size = list(matrix.size())
for i in range(attention.dim() - matrix.dim() + 1):
matrix = matrix.unsqueeze(1)
expanded_size.insert(i + 1, attention.size(i + 1))
matrix = matrix.expand(*expanded_size)
intermediate = attention.unsqueeze(-1).expand_as(matrix) * matrix
return intermediate.sum(dim=-2)
class ElectraDependencyModel(ElectraPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.electra = ElectraModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.dep_label_classifier = nn.Linear(2 * config.hidden_size, 2)
self.init_weights()
def forward(self, input_ids, attention, child, head, mask_entail, mask_cont,
sent_label, num_dependency, device='cuda', weights=None, **kwargs):
batch_size = input_ids.size(0)
transformer_outputs = self.electra(input_ids, attention_mask=attention)
outputs = transformer_outputs[0]
outputs = self.dropout(outputs)
padding_value = torch.zeros(batch_size, 1, outputs.size(-1)).to(device)
outputs = torch.cat((outputs, padding_value), 1)
child = child.squeeze(2)
head = head.squeeze(2)
add = torch.arange(batch_size) * outputs.size(1)
add = add.unsqueeze(1).to(device)
child_temp = child + add
head_temp = head + add
outputs = outputs.view((-1, outputs.size(-1)))
child_embeddings = outputs[child_temp]
head_embeddings = outputs[head_temp]
child_embeddings = child_embeddings.view(batch_size, -1, child_embeddings.size(-1))
head_embeddings = head_embeddings.view(batch_size, -1, head_embeddings.size(-1))
final_embeddings = torch.cat([child_embeddings, head_embeddings], dim=2)
logits_all = self.dep_label_classifier(final_embeddings)
mask = torch.arange(mask_entail.size(1)).to(device)[None, :] >= num_dependency[:, None]
mask = mask.type(torch.long) * -1
mask_entail = mask_entail + mask
mask_entail = mask_entail.detach()
loss_fct_dep = CrossEntropyLoss(ignore_index=-1, weight=weights)
loss = loss_fct_dep(logits_all.view(-1, 2), mask_entail.view(-1).type(torch.long))
outputs_return = (logits_all,)
outputs_return = (loss,) + outputs_return
return outputs_return
class SumPhraseModel(ElectraPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.electra = ElectraModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.span_attention_logits_layer = nn.Linear(config.hidden_size, 1)
self.phrase_rel_linear = nn.Linear(2 * config.hidden_size, config.hidden_size)
self.phrase_linear = nn.Linear(config.hidden_size, config.hidden_size)
self.gelu = nn.GELU()
self.phrase_label_classifier = nn.Linear(config.hidden_size, 2)
self.init_weights()
def forward(self, input_ids, attention, child, head, mask_entail, mask_cont,
sent_label, num_dependency, device='cuda', weights=None, **kwargs):
batch_size = input_ids.size(0)
transformer_outputs = self.electra(input_ids, attention_mask=attention)
outputs = transformer_outputs[0]
outputs = self.dropout(outputs)
padding_value = torch.zeros(batch_size, 1, outputs.size(-1)).to(device)
outputs = torch.cat((outputs, padding_value), 1)
# span
padding_id = outputs.size(1) - 1
head_mask = head != padding_id
child_mask = child != padding_id
add = torch.arange(batch_size) * outputs.size(1)
add = add.unsqueeze(1).to(device)
outputs = outputs.view((-1, outputs.size(-1)))
#span
token_attention_logits = self.span_attention_logits_layer(outputs)
max_phrase = child.size(1)
child_temp = child.view(batch_size, -1) + add
head_temp = head.view(batch_size, -1) + add
child_embeddings = outputs[child_temp]
head_embeddings = outputs[head_temp]
child_embeddings = child_embeddings.view(batch_size, max_phrase, -1, child_embeddings.size(-1))
head_embeddings = head_embeddings.view(batch_size, max_phrase, -1, head_embeddings.size(-1))
# span
head_logits = token_attention_logits[head_temp]
child_logits = token_attention_logits[child_temp]
head_logits = head_logits.view(batch_size, max_phrase, -1)
child_logits = child_logits.view(batch_size, max_phrase, -1)
head_weight = masked_softmax(head_logits, head_mask)
head_embeddings = weighted_sum(head_embeddings, head_weight)
child_weight = masked_softmax(child_logits, child_mask)
child_embeddings = weighted_sum(child_embeddings, child_weight)
same_head_child = torch.all(child == head, dim=2)
same_head_child_embeddings = child_embeddings[same_head_child]
same_head_child_embeddings = self.gelu(self.phrase_linear(same_head_child_embeddings))
not_same_head_child_embeddings = torch.cat([child_embeddings, head_embeddings], dim=2)
final_embeddings = self.gelu(self.phrase_rel_linear(not_same_head_child_embeddings))
final_embeddings[same_head_child] = same_head_child_embeddings
logits_all = self.phrase_label_classifier(final_embeddings)
mask = torch.arange(mask_entail.size(1)).to(device)[None, :] >= num_dependency[:, None]
mask = mask.type(torch.long) * -1
mask_entail = mask_entail + mask
mask_entail = mask_entail.detach()
loss_fct_dep = CrossEntropyLoss(ignore_index=-1, weight=weights)
loss = loss_fct_dep(logits_all.view(-1, 2), mask_entail.view(-1).type(torch.long))
outputs_return = (logits_all,)
outputs_return = (loss,) + outputs_return
return outputs_return
class SumPhraseMultiTaskModel(ElectraPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.electra = ElectraModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.span_attention_logits_layer = nn.Linear(config.hidden_size, 1)
self.phrase_rel_linear = nn.Linear(2 * config.hidden_size, config.hidden_size)
self.phrase_linear = nn.Linear(config.hidden_size, config.hidden_size)
self.gelu = nn.GELU()
self.phrase_label_classifier = nn.Linear(config.hidden_size, 2)
self.sent_basis_linear = nn.Linear(config.hidden_size*2, config.hidden_size)
self.sent_basis_label_classifier = nn.Linear(config.hidden_size, 2)
self.init_weights()
def forward(self, input_ids, attention, child, head, mask_entail, mask_cont,
sent_label, num_dependency, hypo_cls_idx, sent_indices, sent_basis_label,
device='cuda', weights=None, multitask_loss_weight=[1,1], **kwargs):
batch_size = input_ids.size(0)
transformer_outputs = self.electra(input_ids, attention_mask=attention)
outputs = transformer_outputs[0]
outputs = self.dropout(outputs)
padding_value = torch.zeros(batch_size, 1, outputs.size(-1)).to(device)
outputs = torch.cat((outputs, padding_value), 1)
# span
padding_id = outputs.size(1) - 1
head_mask = head != padding_id
child_mask = child != padding_id
sent_mask = sent_indices != padding_id
add = torch.arange(batch_size) * outputs.size(1)
add = add.unsqueeze(1).to(device)
outputs = outputs.view((-1, outputs.size(-1)))
#span
token_attention_logits = self.span_attention_logits_layer(outputs)
max_phrase = child.size(1)
max_num_sent = sent_indices.size(1)
child_temp = child.view(batch_size, -1) + add
head_temp = head.view(batch_size, -1) + add
hypo_cls_idx += add.squeeze(1)
sent_indices_tmp = sent_indices.view(batch_size, -1) + add
child_embeddings = outputs[child_temp]
head_embeddings = outputs[head_temp]
hypo_cls_embeddings = outputs[hypo_cls_idx]
sent_embeddings = outputs[sent_indices_tmp]
child_embeddings = child_embeddings.view(batch_size, max_phrase, -1, child_embeddings.size(-1))
head_embeddings = head_embeddings.view(batch_size, max_phrase, -1, head_embeddings.size(-1))
sent_embeddings = sent_embeddings.view(batch_size, max_num_sent, -1, sent_embeddings.size(-1))
# span
head_logits = token_attention_logits[head_temp]
child_logits = token_attention_logits[child_temp]
sent_logits = token_attention_logits[sent_indices_tmp]
head_logits = head_logits.view(batch_size, max_phrase, -1)
child_logits = child_logits.view(batch_size, max_phrase, -1)
sent_logits = sent_logits.view(batch_size, max_num_sent, -1)
head_weight = masked_softmax(head_logits, head_mask)
head_embeddings = weighted_sum(head_embeddings, head_weight)
child_weight = masked_softmax(child_logits, child_mask)
child_embeddings = weighted_sum(child_embeddings, child_weight)
sent_weight = masked_softmax(sent_logits, sent_mask)
sent_embeddings = weighted_sum(sent_embeddings, sent_weight)
same_head_child = torch.all(child == head, dim=2)
same_head_child_embeddings = child_embeddings[same_head_child]
same_head_child_embeddings = self.gelu(self.phrase_linear(same_head_child_embeddings))
not_same_head_child_embeddings = torch.cat([child_embeddings, head_embeddings], dim=2)
final_embeddings = self.gelu(self.phrase_rel_linear(not_same_head_child_embeddings))
final_embeddings[same_head_child] = same_head_child_embeddings
logits_all = self.phrase_label_classifier(final_embeddings)
# hypo
hypo_cls_embeddings = hypo_cls_embeddings.unsqueeze(1).repeat(1, max_num_sent, 1)
sent_embeddings = torch.cat([sent_embeddings, hypo_cls_embeddings], dim=2)
sent_embeddings = self.gelu(self.sent_basis_linear(sent_embeddings))
sent_basis_logits = self.sent_basis_label_classifier(sent_embeddings)
mask = torch.arange(mask_entail.size(1)).to(device)[None, :] >= num_dependency[:, None]
mask = mask.type(torch.long) * -1
mask_entail = mask_entail + mask
mask_entail = mask_entail.detach()
loss_fct_dep = CrossEntropyLoss(ignore_index=-1, weight=weights)
loss_fct_sent = CrossEntropyLoss(ignore_index=-1)
loss_dep = loss_fct_dep(logits_all.view(-1, 2), mask_entail.view(-1).type(torch.long))
sent_basis_label = sent_basis_label[:, :max_num_sent].contiguous()
loss_sent = loss_fct_sent(sent_basis_logits.view(-1, 2), sent_basis_label.view(-1))
loss = multitask_loss_weight[0]*loss_dep + multitask_loss_weight[1]*loss_sent
outputs_return = (loss, logits_all, sent_basis_logits, loss_dep, loss_sent)
return outputs_return
class ElectraDependencyMultiTaskModel(ElectraPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.electra = ElectraModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.dep_label_classifier = nn.Linear(2 * config.hidden_size, 2)
self.span_attention_logits_layer = nn.Linear(config.hidden_size, 1)
self.sent_basis_linear = nn.Linear(config.hidden_size*2, config.hidden_size)
self.sent_basis_label_classifier = nn.Linear(config.hidden_size, 2)
self.init_weights()
def forward(self, input_ids, attention, child, head, mask_entail, mask_cont,
sent_label, num_dependency, hypo_cls_idx, sent_indices, sent_basis_label,
device='cuda', weights=None, multitask_loss_weight=[1,1], **kwargs):
batch_size = input_ids.size(0)
transformer_outputs = self.electra(input_ids, attention_mask=attention)
outputs = transformer_outputs[0]
outputs = self.dropout(outputs)
padding_value = torch.zeros(batch_size, 1, outputs.size(-1)).to(device)
outputs = torch.cat((outputs, padding_value), 1)
child = child.squeeze(2)
head = head.squeeze(2)
max_num_sent = sent_indices.size(1)
padding_id = outputs.size(1) - 1
sent_mask = sent_indices != padding_id
add = torch.arange(batch_size) * outputs.size(1)
add = add.unsqueeze(1).to(device)
child_temp = child + add
head_temp = head + add
hypo_cls_idx += add.squeeze(1)
sent_indices_tmp = sent_indices.view(batch_size, -1) + add
outputs = outputs.view((-1, outputs.size(-1)))
#span
token_attention_logits = self.span_attention_logits_layer(outputs)
child_embeddings = outputs[child_temp]
head_embeddings = outputs[head_temp]
hypo_cls_embeddings = outputs[hypo_cls_idx]
sent_embeddings = outputs[sent_indices_tmp]
child_embeddings = child_embeddings.view(batch_size, -1, child_embeddings.size(-1))
head_embeddings = head_embeddings.view(batch_size, -1, head_embeddings.size(-1))
sent_embeddings = sent_embeddings.view(batch_size, max_num_sent, -1, sent_embeddings.size(-1))
final_embeddings = torch.cat([child_embeddings, head_embeddings], dim=2)
logits_all = self.dep_label_classifier(final_embeddings)
sent_logits = token_attention_logits[sent_indices_tmp]
sent_logits = sent_logits.view(batch_size, max_num_sent, -1)
sent_weight = masked_softmax(sent_logits, sent_mask)
sent_embeddings = weighted_sum(sent_embeddings, sent_weight)
hypo_cls_embeddings = hypo_cls_embeddings.unsqueeze(1).repeat(1, max_num_sent, 1)
sent_embeddings = torch.cat([sent_embeddings, hypo_cls_embeddings], dim=2)
sent_embeddings = F.gelu(self.sent_basis_linear(sent_embeddings))
sent_basis_logits = self.sent_basis_label_classifier(sent_embeddings)
mask = torch.arange(mask_entail.size(1)).to(device)[None, :] >= num_dependency[:, None]
mask = mask.type(torch.long) * -1
mask_entail = mask_entail + mask
mask_entail = mask_entail.detach()
loss_fct_dep = CrossEntropyLoss(ignore_index=-1, weight=weights)
loss_fct_sent = CrossEntropyLoss(ignore_index=-1)
loss_dep = loss_fct_dep(logits_all.view(-1, 2), mask_entail.view(-1).type(torch.long))
sent_basis_label = sent_basis_label[:, :max_num_sent].contiguous()
loss_sent = loss_fct_sent(sent_basis_logits.view(-1, 2), sent_basis_label.view(-1))
loss = multitask_loss_weight[0]*loss_dep + multitask_loss_weight[1]*loss_sent
outputs_return = (loss, logits_all, sent_basis_logits, loss_dep, loss_sent)
return outputs_return