-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpr_like_model.py
More file actions
242 lines (193 loc) · 8.54 KB
/
Copy pathdpr_like_model.py
File metadata and controls
242 lines (193 loc) · 8.54 KB
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
import torch
from torch import nn
from noise import remove_subjects, crop_words
class collator_qc():
def __init__(self, tokenizer, corruption_rate = 0.):
self.corruption_rate = corruption_rate
self.tokenizer = tokenizer
def __call__(self, batch):
batch = corrupt(batch, corruption_rate=self.corruption_rate)
src_q = self.tokenizer(['<question> ' + sample['question'] for sample in batch], return_tensors="pt", padding='longest', truncation=True, max_length=512)
src_c = self.tokenizer(['<context> ' + sample['context'] for sample in batch], return_tensors="pt", padding='longest', truncation=True, max_length=512)
return {
'question': {**src_q},
'context': {**src_c},
"labels": torch.as_tensor([sample['target'] for sample in batch])
}
def corrupt(batch, corruption_rate=0.2):
new_data = []
ready = False
for i, data in enumerate(batch):
# Pass if we swithed two context so it's already in new_data
if ready:
ready = False
pass
# Convert and add the unanswerable question
elif not data['answerable']:
new_data.append({'question': data['question'], 'context': data['text'], 'target': 1})
# Randomly apply one of the four corruption function
else:
if random.random() > 1 - corruption_rate:
p = random.random()
if p < 0.33 and i+1<len(batch):
new_data.append({'question': data['question'], 'context': data['text'], 'target': 1})
new_data.append({'question': batch[i+1]['question'], 'context': batch[i+1]['text'], 'target': 1})
ready = True
elif p < 0.66:
new_data.append({'question': crop_words(data['question']), 'context': data['text'], 'target': 1})
else:
new_data.append({'question': remove_subjects(data['question']), 'context': data['text'], 'target': 1})
# else:
# context = get_important_noun_phrases(data['text'], nlp=nlp) + ' </s> ' + data['text']
# new_data.append({'input': context, 'target': 1})
# If no corruption, just add the data
else:
new_data.append({'question': data['question'], 'context': data['text'], 'target': 0})
return new_data
'''
The two function with the special head I wanted to try with Thomas,
they are for the first experiences only on french squadv2.
The results were not really good.
'''
class head_cls(nn.Module):
def __init__(self, model, loss_type='NLLL') -> None:
super(head_cls, self).__init__()
self.transformer = model
self.loss_type = loss_type
# dense layer 1
self.fc1 = nn.Linear(1536, 768)
# dropout layer
self.dropout = nn.Dropout(0.1)
# dense layer 2 (Output layer)
self.fc2 = nn.Linear(768, 2)
if self.loss_type=='NLLL':
# softmax activation function
self.softmax = nn.LogSoftmax(dim=1)
self.loss_fct = nn.NLLLoss()
elif self.loss_type=='cross':
self.loss_fct = nn.CrossEntropyLoss()
else:
print("loss error")
def forward(self, batch):
#In batch: 'question', 'context', and 'labels'
q = self.transformer(**batch['question'])[0]
c = self.transformer(**batch['context'])[0]
# x = x[:, 0, :]
x = torch.cat((q[:, 0, :], c[:, 0, :]), dim=1)
# Classification head to fine tune
x = self.fc1(x)
#x = self.relu(x) # Relu or tanh not both
x = torch.tanh(x)
x = self.dropout(x)
x = self.fc2(x)
# Loss fct
if self.loss_type=='NLLL':
x = self.softmax(x)
loss = self.loss_fct(x, batch['labels'])
elif self.loss_type=='cross':
loss = self.loss_fct(x, batch['labels'])
return loss, x
class head_colbert_like(nn.Module):
def __init__(self, model, loss_type='NLLL') -> None:
super(head_colbert_like, self).__init__()
self.transformer = model
self.loss_type = loss_type
# dense layer 1
self.fc1 = nn.Linear(1536, 1536)
# dropout layer
self.dropout = nn.Dropout(0.1)
# dense layer 2 (Output layer)
self.fc2 = nn.Linear(1536, 2)
if self.loss_type=='NLLL':
# softmax activation function
self.softmax = nn.LogSoftmax(dim=1)
self.loss_fct = nn.NLLLoss()
elif self.loss_type=='cross':
self.loss_fct = nn.CrossEntropyLoss()
else:
print("loss error")
def forward(self, batch):
#In batch: 'question', 'context', and 'labels'
q = self.transformer(**batch['question'])[0]
c = self.transformer(**batch['context'])[0]
mask_q = batch['question']['attention_mask']
mask_c = batch['context']['attention_mask']
# matrice to create the importance weights
x = torch.einsum('bik, bjk -> bij', q, c)
# retirer les mask puis rendre à -inf pr le softmax
x = torch.einsum('bij, bi -> bij', x, mask_q)
x = torch.einsum('bij, bj -> bij', x, mask_c)
x[x==0.] = -float('inf')
# Take the waigth matrix without the 'nan' created by 0. lines
p = torch.softmax(x, dim=2).nan_to_num()
pp = torch.softmax(x, dim=1).nan_to_num()
a = torch.einsum('bjk, bij -> bk', c, p)
b = torch.einsum('bik, bij -> bk', q, pp)
x = torch.cat((a, b), dim=1)
# Classification head to fine tune
x = self.fc1(x)
#x = self.relu(x) # Relu or tanh not both
x = torch.tanh(x)
x = self.dropout(x)
x = self.fc2(x)
# Loss fct
if self.loss_type=='NLLL':
x = self.softmax(x)
loss = self.loss_fct(x, batch['labels'])
elif self.loss_type=='cross':
loss = self.loss_fct(x, batch['labels'])
return loss, x
# Pytorch class to launch it:
class classification_test(pl.LightningModule):
def __init__(
self,
model_name = "camembert-base",
load_pretraned_model = False,
validation_callback = None,
log_dir = None,
num_labels = 2,
head = 'head_cls'
):
super().__init__()
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.tokenizer.add_tokens(['<question>', '<context>'], special_tokens=True)
if load_pretraned_model != False:
self.model = torch.load(load_pretraned_model)
else:
self.model = CamembertForSequenceClassification.from_pretrained(model_name, num_labels=num_labels).roberta
self.model.resize_token_embeddings(len(self.tokenizer))
if head == 'head_cls':
self.model = head_cls(self.model)
else:
self.model = head_colbert_like(self.model)
self.validation_callback = validation_callback
self.log_dir = log_dir
def training_step(self, batch, batch_idx):
loss, _ = self.model(batch)
self.log("train_loss", loss, sync_dist=True)
return loss
def configure_optimizers(self):
optimizer = AdamW(self.model.parameters(), lr=1e-5)
# scheduler1 = LinearLR(optimizer, total_iters = 1000, start_factor= 1.0 / 100.)
# # scheduler2 = ReduceLROnPlateau(optimizer, 'min', patience=3)
# scheduler2 = StepLR(optimizer, step_size=1000, gamma=0.5)
scheduler = {
"scheduler": LinearLR(optimizer, total_iters = 1000, start_factor= 1.0 / 1000.),
"interval": "step",
'name': 'lr_scheduler',
"frequency": 1
}
return [optimizer], [scheduler]
# return optimizer
def validation_step(self, batch, batch_idx):
loss, pred = self.model(batch)
self.log("val_loss", loss, sync_dist=True)
return {"predictions": pred.tolist(), "references": batch['labels'].tolist()}
def validation_epoch_end(self, batch, *kargs, **kwargs):
predictions = sum([b["predictions"] for b in batch], [])
predictions = [(a[0] < a[1]) * 1 for a in predictions]
references = sum([b["references"] for b in batch], [])
if self.validation_callback is not None:
validation_log = self.validation_callback(predictions, references)
for k, v in validation_log.items():
self.log("val_"+k, v, sync_dist=True)