-
Notifications
You must be signed in to change notification settings - Fork 9
/
model.py
279 lines (244 loc) · 10.8 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.nn import CrossEntropyLoss, MSELoss
from transformers import BertPreTrainedModel, RobertaModel, BertModel
from sklearn.covariance import EmpiricalCovariance
class BertForSequenceClassification(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, config.num_labels)
self.init_weights()
def forward(
self,
input_ids=None,
attention_mask=None,
labels=None,
):
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
)
pooled_output = pooled = outputs[1]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
if self.config.loss == 'margin':
dist = ((pooled.unsqueeze(1) - pooled.unsqueeze(0)) ** 2).mean(-1)
mask = (labels.unsqueeze(1) == labels.unsqueeze(0)).float()
mask = mask - torch.diag(torch.diag(mask))
neg_mask = (labels.unsqueeze(1) != labels.unsqueeze(0)).float()
max_dist = (dist * mask).max()
cos_loss = (dist * mask).sum(-1) / (mask.sum(-1) + 1e-3) + (F.relu(max_dist - dist) * neg_mask).sum(-1) / (neg_mask.sum(-1) + 1e-3)
cos_loss = cos_loss.mean()
else:
norm_pooled = F.normalize(pooled, dim=-1)
cosine_score = torch.exp(norm_pooled @ norm_pooled.t() / 0.3)
mask = (labels.unsqueeze(1) == labels.unsqueeze(0)).float()
cosine_score = cosine_score - torch.diag(torch.diag(cosine_score))
mask = mask - torch.diag(torch.diag(mask))
cos_loss = cosine_score / cosine_score.sum(dim=-1, keepdim=True)
cos_loss = -torch.log(cos_loss + 1e-5)
cos_loss = (mask * cos_loss).sum(-1) / (mask.sum(-1) + 1e-3)
cos_loss = cos_loss.mean()
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
loss = loss + self.config.alpha * cos_loss
output = (logits,) + outputs[2:]
output = output + (pooled,)
return ((loss, cos_loss) + output) if loss is not None else output
def compute_ood(
self,
input_ids=None,
attention_mask=None,
labels=None,
):
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
)
pooled_output = pooled = outputs[1]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
ood_keys = None
softmax_score = F.softmax(logits, dim=-1).max(-1)[0]
maha_score = []
for c in self.all_classes:
centered_pooled = pooled - self.class_mean[c].unsqueeze(0)
ms = torch.diag(centered_pooled @ self.class_var @ centered_pooled.t())
maha_score.append(ms)
maha_score = torch.stack(maha_score, dim=-1)
maha_score = maha_score.min(-1)[0]
maha_score = -maha_score
norm_pooled = F.normalize(pooled, dim=-1)
cosine_score = norm_pooled @ self.norm_bank.t()
cosine_score = cosine_score.max(-1)[0]
energy_score = torch.logsumexp(logits, dim=-1)
ood_keys = {
'softmax': softmax_score.tolist(),
'maha': maha_score.tolist(),
'cosine': cosine_score.tolist(),
'energy': energy_score.tolist(),
}
return ood_keys
def prepare_ood(self, dataloader=None):
self.bank = None
self.label_bank = None
for batch in dataloader:
self.eval()
batch = {key: value.cuda() for key, value in batch.items()}
labels = batch['labels']
outputs = self.bert(
input_ids=batch['input_ids'],
attention_mask=batch['attention_mask'],
)
pooled = outputs[1]
if self.bank is None:
self.bank = pooled.clone().detach()
self.label_bank = labels.clone().detach()
else:
bank = pooled.clone().detach()
label_bank = labels.clone().detach()
self.bank = torch.cat([bank, self.bank], dim=0)
self.label_bank = torch.cat([label_bank, self.label_bank], dim=0)
self.norm_bank = F.normalize(self.bank, dim=-1)
N, d = self.bank.size()
self.all_classes = list(set(self.label_bank.tolist()))
self.class_mean = torch.zeros(max(self.all_classes) + 1, d).cuda()
for c in self.all_classes:
self.class_mean[c] = (self.bank[self.label_bank == c].mean(0))
centered_bank = (self.bank - self.class_mean[self.label_bank]).detach().cpu().numpy()
precision = EmpiricalCovariance().fit(centered_bank).precision_.astype(np.float32)
self.class_var = torch.from_numpy(precision).float().cuda()
class RobertaClassificationHead(nn.Module):
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
def forward(self, features):
x = features[:, 0, :]
x = self.dropout(x)
x = self.dense(x)
x = pooled = torch.tanh(x)
x = self.dropout(x)
x = self.out_proj(x)
return x, pooled
class RobertaForSequenceClassification(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.config = config
self.num_labels = config.num_labels
self.roberta = RobertaModel(config, add_pooling_layer=False)
self.classifier = RobertaClassificationHead(config)
self.init_weights()
def forward(
self,
input_ids=None,
attention_mask=None,
labels=None,
):
outputs = self.roberta(
input_ids,
attention_mask=attention_mask,
)
sequence_output = outputs[0]
logits, pooled = self.classifier(sequence_output)
loss = None
if labels is not None:
if self.config.loss == 'margin':
dist = ((pooled.unsqueeze(1) - pooled.unsqueeze(0)) ** 2).mean(-1)
mask = (labels.unsqueeze(1) == labels.unsqueeze(0)).float()
mask = mask - torch.diag(torch.diag(mask))
neg_mask = (labels.unsqueeze(1) != labels.unsqueeze(0)).float()
max_dist = (dist * mask).max()
cos_loss = (dist * mask).sum(-1) / (mask.sum(-1) + 1e-3) + (F.relu(max_dist - dist) * neg_mask).sum(-1) / (neg_mask.sum(-1) + 1e-3)
cos_loss = cos_loss.mean()
else:
norm_pooled = F.normalize(pooled, dim=-1)
cosine_score = torch.exp(norm_pooled @ norm_pooled.t() / 0.3)
mask = (labels.unsqueeze(1) == labels.unsqueeze(0)).float()
cosine_score = cosine_score - torch.diag(torch.diag(cosine_score))
mask = mask - torch.diag(torch.diag(mask))
cos_loss = cosine_score / cosine_score.sum(dim=-1, keepdim=True)
cos_loss = -torch.log(cos_loss + 1e-5)
cos_loss = (mask * cos_loss).sum(-1) / (mask.sum(-1) + 1e-3)
cos_loss = cos_loss.mean()
if self.num_labels == 1:
loss_fct = MSELoss()
loss = loss_fct(logits.view(-1), labels.view(-1))
else:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
loss = loss + self.config.alpha * cos_loss
output = (logits,) + outputs[2:]
output = output + (pooled,)
return ((loss, cos_loss) + output) if loss is not None else output
def compute_ood(
self,
input_ids=None,
attention_mask=None,
labels=None,
):
outputs = self.roberta(
input_ids,
attention_mask=attention_mask,
)
sequence_output = outputs[0]
logits, pooled = self.classifier(sequence_output)
ood_keys = None
softmax_score = F.softmax(logits, dim=-1).max(-1)[0]
maha_score = []
for c in self.all_classes:
centered_pooled = pooled - self.class_mean[c].unsqueeze(0)
ms = torch.diag(centered_pooled @ self.class_var @ centered_pooled.t())
maha_score.append(ms)
maha_score = torch.stack(maha_score, dim=-1)
maha_score = maha_score.min(-1)[0]
maha_score = -maha_score
norm_pooled = F.normalize(pooled, dim=-1)
cosine_score = norm_pooled @ self.norm_bank.t()
cosine_score = cosine_score.max(-1)[0]
energy_score = torch.logsumexp(logits, dim=-1)
ood_keys = {
'softmax': softmax_score.tolist(),
'maha': maha_score.tolist(),
'cosine': cosine_score.tolist(),
'energy': energy_score.tolist(),
}
return ood_keys
def prepare_ood(self, dataloader=None):
self.bank = None
self.label_bank = None
for batch in dataloader:
self.eval()
batch = {key: value.cuda() for key, value in batch.items()}
labels = batch['labels']
outputs = self.roberta(
input_ids=batch['input_ids'],
attention_mask=batch['attention_mask'],
)
sequence_output = outputs[0]
logits, pooled = self.classifier(sequence_output)
if self.bank is None:
self.bank = pooled.clone().detach()
self.label_bank = labels.clone().detach()
else:
bank = pooled.clone().detach()
label_bank = labels.clone().detach()
self.bank = torch.cat([bank, self.bank], dim=0)
self.label_bank = torch.cat([label_bank, self.label_bank], dim=0)
self.norm_bank = F.normalize(self.bank, dim=-1)
N, d = self.bank.size()
self.all_classes = list(set(self.label_bank.tolist()))
self.class_mean = torch.zeros(max(self.all_classes) + 1, d).cuda()
for c in self.all_classes:
self.class_mean[c] = (self.bank[self.label_bank == c].mean(0))
centered_bank = (self.bank - self.class_mean[self.label_bank]).detach().cpu().numpy()
precision = EmpiricalCovariance().fit(centered_bank).precision_.astype(np.float32)
self.class_var = torch.from_numpy(precision).float().cuda()