-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluation.py
343 lines (258 loc) · 14.5 KB
/
evaluation.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
#!/usr/bin/env python3
'''
Model evaluation functions.
When training your multitask model, you will find it useful to run
model_eval_multitask to be able to evaluate your model on the 3 tasks in the
development set.
Before submission, your code needs to call test_model_multitask(args, model, device) to generate
your predictions. We'll evaluate these predictions against our labels on our end,
which is how the leaderboard will be updated.
The provided test_model() function in multitask_classifier.py **already does this for you**,
so unless you change it you shouldn't need to call anything from here
explicitly aside from model_eval_multitask.
'''
import torch
from torch.utils.data import DataLoader
from sklearn.metrics import classification_report, f1_score, recall_score, accuracy_score
from tqdm import tqdm
import numpy as np
import os
from preprocessing.datasets import load_multitask_data, load_multitask_test_data, \
SentenceClassificationDataset, SentenceClassificationTestDataset, \
SentencePairDataset, SentencePairTestDataset
from tensorboard_utils import createConfusionMatrix
TQDM_DISABLE = False
# Evaluate a multitask model for accuracy.on SST only.
def model_eval_sst(dataloader, model, device):
'''Evaluate a model on the SST dataset only.'''
model.eval() # switch to eval model, will turn off randomness like dropout
y_true = []
y_pred = []
sents = []
sent_ids = []
for step, batch in enumerate(tqdm(dataloader, desc=f'eval', disable=TQDM_DISABLE)):
b_ids, b_mask, b_labels, b_sents, b_sent_ids = batch['token_ids'],batch['attention_mask'], \
batch['labels'], batch['sents'], batch['sent_ids']
b_ids = b_ids.to(device)
b_mask = b_mask.to(device)
logits = model.predict_sentiment(b_ids, b_mask)
logits = logits.detach().cpu().numpy()
preds = np.argmax(logits, axis=1).flatten()
b_labels = b_labels.flatten()
y_true.extend(b_labels)
y_pred.extend(preds)
sents.extend(b_sents)
sent_ids.extend(b_sent_ids)
f1 = f1_score(y_true, y_pred, average='macro')
acc = accuracy_score(y_true, y_pred)
return acc, f1, y_pred, y_true, sents, sent_ids
def model_eval_paraphrase(paraphrase_dataloader, model, device):
'''Evaluate a model on the paraphrase detection dataset.'''
model.eval() # switch to eval model, will turn off randomness like dropout
with torch.no_grad():
para_y_true = []
para_y_pred = []
para_sent_ids = []
# Evaluate paraphrase detection.
for step, batch in enumerate(tqdm(paraphrase_dataloader, desc=f'eval', disable=TQDM_DISABLE)):
(b_ids1, b_mask1,
b_ids2, b_mask2,
b_labels, b_sent_ids) = (batch['token_ids_1'], batch['attention_mask_1'],
batch['token_ids_2'], batch['attention_mask_2'],
batch['labels'], batch['sent_ids'])
b_ids1 = b_ids1.to(device)
b_mask1 = b_mask1.to(device)
b_ids2 = b_ids2.to(device)
b_mask2 = b_mask2.to(device)
logits = model.predict_paraphrase(b_ids1, b_mask1, b_ids2, b_mask2)
y_hat = logits.sigmoid().round().flatten().cpu().numpy()
b_labels = b_labels.flatten().cpu().numpy()
para_y_pred.extend(y_hat)
para_y_true.extend(b_labels)
para_sent_ids.extend(b_sent_ids)
paraphrase_accuracy = np.mean(np.array(para_y_pred) == np.array(para_y_true))
return paraphrase_accuracy, para_y_true, para_y_pred, para_sent_ids
def model_eval_sts(sts_dataloader, model, device):
'''Evaluate a model on the semantic textual similarity dataset.'''
model.eval() # switch to eval model, will turn off randomness like dropout
with torch.no_grad():
sts_y_true = []
sts_y_pred = []
sts_sent_ids = []
# Evaluate semantic textual similarity.
for step, batch in enumerate(tqdm(sts_dataloader, desc=f'eval', disable=TQDM_DISABLE)):
(b_ids1, b_mask1,
b_ids2, b_mask2,
b_labels, b_sent_ids) = (batch['token_ids_1'], batch['attention_mask_1'],
batch['token_ids_2'], batch['attention_mask_2'],
batch['labels'], batch['sent_ids'])
b_ids1 = b_ids1.to(device)
b_mask1 = b_mask1.to(device)
b_ids2 = b_ids2.to(device)
b_mask2 = b_mask2.to(device)
logits = model.predict_similarity(b_ids1, b_mask1, b_ids2, b_mask2)
y_hat = logits.flatten().cpu().numpy()
b_labels = b_labels.flatten().cpu().numpy()
sts_y_pred.extend(y_hat)
sts_y_true.extend(b_labels)
sts_sent_ids.extend(b_sent_ids)
pearson_mat = np.corrcoef(sts_y_pred,sts_y_true)
sts_corr = pearson_mat[1][0]
return sts_corr, sts_y_true, sts_y_pred, sts_sent_ids
def model_eval_sentiment(sentiment_dataloader, model, device):
'''Evaluate a model on the sentiment classification dataset.'''
model.eval() # switch to eval model, will turn off randomness like dropout
with torch.no_grad():
sst_y_true = []
sst_y_pred = []
sst_sent_ids = []
# Evaluate sentiment classification.
for step, batch in enumerate(tqdm(sentiment_dataloader, desc=f'eval', disable=TQDM_DISABLE)):
b_ids, b_mask, b_labels, b_sent_ids = batch['token_ids'], batch['attention_mask'], batch['labels'], batch['sent_ids']
b_ids = b_ids.to(device)
b_mask = b_mask.to(device)
logits = model.predict_sentiment(b_ids, b_mask)
y_hat = logits.argmax(dim=-1).flatten().cpu().numpy()
b_labels = b_labels.flatten().cpu().numpy()
sst_y_pred.extend(y_hat)
sst_y_true.extend(b_labels)
sst_sent_ids.extend(b_sent_ids)
sentiment_accuracy = np.mean(np.array(sst_y_pred) == np.array(sst_y_true))
return sentiment_accuracy, sst_y_true, sst_y_pred, sst_sent_ids
# Perform model evaluation in terms by averaging accuracies across tasks.
def model_eval_multitask(sentiment_dataloader,
paraphrase_dataloader,
sts_dataloader,
model, device, writer = None, epoch = None, tensorboard = False):
'''Evaluate a model on the sentiment classification, paraphrase detection, and semantic textual similarity datasets.'''
paraphrase_accuracy, para_y_true, para_y_pred, para_sent_ids = model_eval_paraphrase(paraphrase_dataloader, model, device)
sts_corr, sts_y_true, sts_y_pred, sts_sent_ids = model_eval_sts(sts_dataloader, model, device)
sentiment_accuracy, sst_y_true, sst_y_pred, sst_sent_ids = model_eval_sentiment(sentiment_dataloader, model, device)
if tensorboard and writer is not None:
writer.add_figure("Confusion matrix SST", createConfusionMatrix(sst_y_true, sst_y_pred), epoch)
writer.add_figure("Confusion matrix Para", createConfusionMatrix(para_y_true, para_y_pred), epoch)
# writer.add_figure("Confusion matrix STS", createConfusionMatrix((np.round(sts_y_true,1) * 10).astype(int), (np.round(sts_y_pred,1) * 10).astype(int)), epoch)
return (paraphrase_accuracy, para_y_pred, para_sent_ids,
sentiment_accuracy,sst_y_pred, sst_sent_ids,
sts_corr, sts_y_pred, sts_sent_ids)
# Perform model evaluation in terms by averaging accuracies across tasks.
def model_eval_test_multitask(sentiment_dataloader,
paraphrase_dataloader,
sts_dataloader,
model, device):
model.eval() # switch to eval model, will turn off randomness like dropout
with torch.no_grad():
para_y_pred = []
para_sent_ids = []
# Evaluate paraphrase detection.
for step, batch in enumerate(tqdm(paraphrase_dataloader, desc=f'eval', disable=TQDM_DISABLE)):
(b_ids1, b_mask1,
b_ids2, b_mask2,
b_sent_ids) = (batch['token_ids_1'], batch['attention_mask_1'],
batch['token_ids_2'], batch['attention_mask_2'],
batch['sent_ids'])
b_ids1 = b_ids1.to(device)
b_mask1 = b_mask1.to(device)
b_ids2 = b_ids2.to(device)
b_mask2 = b_mask2.to(device)
logits = model.predict_paraphrase(b_ids1, b_mask1, b_ids2, b_mask2)
y_hat = logits.sigmoid().round().flatten().cpu().numpy()
para_y_pred.extend(y_hat)
para_sent_ids.extend(b_sent_ids)
sts_y_pred = []
sts_sent_ids = []
# Evaluate semantic textual similarity.
for step, batch in enumerate(tqdm(sts_dataloader, desc=f'eval', disable=TQDM_DISABLE)):
(b_ids1, b_mask1,
b_ids2, b_mask2,
b_sent_ids) = (batch['token_ids_1'], batch['attention_mask_1'],
batch['token_ids_2'], batch['attention_mask_2'],
batch['sent_ids'])
b_ids1 = b_ids1.to(device)
b_mask1 = b_mask1.to(device)
b_ids2 = b_ids2.to(device)
b_mask2 = b_mask2.to(device)
logits = model.predict_similarity(b_ids1, b_mask1, b_ids2, b_mask2)
y_hat = logits.flatten().cpu().numpy()
sts_y_pred.extend(y_hat)
sts_sent_ids.extend(b_sent_ids)
sst_y_pred = []
sst_sent_ids = []
# Evaluate sentiment classification.
for step, batch in enumerate(tqdm(sentiment_dataloader, desc=f'eval', disable=TQDM_DISABLE)):
b_ids, b_mask, b_sent_ids = batch['token_ids'], batch['attention_mask'], batch['sent_ids']
b_ids = b_ids.to(device)
b_mask = b_mask.to(device)
logits = model.predict_sentiment(b_ids, b_mask)
y_hat = logits.argmax(dim=-1).flatten().cpu().numpy()
sst_y_pred.extend(y_hat)
sst_sent_ids.extend(b_sent_ids)
return (para_y_pred, para_sent_ids,
sst_y_pred, sst_sent_ids,
sts_y_pred, sts_sent_ids)
def test_model_multitask(args, model, device):
# Creates a folder prediction
prediction_folder = args.log_dir + '/predictions'
if not os.path.exists(prediction_folder):
os.makedirs(prediction_folder)
sst_test_data, num_labels,para_test_data, sts_test_data = \
load_multitask_data(args.sst_test,args.para_test, args.sts_test, split='test')
sst_dev_data, num_labels,para_dev_data, sts_dev_data = \
load_multitask_data(args.sst_dev,args.para_dev,args.sts_dev,split='dev')
sst_test_data = SentenceClassificationTestDataset(sst_test_data, args)
sst_dev_data = SentenceClassificationDataset(sst_dev_data, args)
sst_test_dataloader = DataLoader(sst_test_data, shuffle=True, batch_size=args.batch_size,
collate_fn=sst_test_data.collate_fn)
sst_dev_dataloader = DataLoader(sst_dev_data, shuffle=False, batch_size=args.batch_size,
collate_fn=sst_dev_data.collate_fn)
para_test_data = SentencePairTestDataset(para_test_data, args)
para_dev_data = SentencePairDataset(para_dev_data, args)
para_test_dataloader = DataLoader(para_test_data, shuffle=True, batch_size=args.batch_size,
collate_fn=para_test_data.collate_fn)
para_dev_dataloader = DataLoader(para_dev_data, shuffle=False, batch_size=args.batch_size,
collate_fn=para_dev_data.collate_fn)
sts_test_data = SentencePairTestDataset(sts_test_data, args)
sts_dev_data = SentencePairDataset(sts_dev_data, args, isRegression=True)
sts_test_dataloader = DataLoader(sts_test_data, shuffle=True, batch_size=args.batch_size,
collate_fn=sts_test_data.collate_fn)
sts_dev_dataloader = DataLoader(sts_dev_data, shuffle=False, batch_size=args.batch_size,
collate_fn=sts_dev_data.collate_fn)
dev_paraphrase_accuracy, dev_para_y_pred, dev_para_sent_ids, \
dev_sentiment_accuracy,dev_sst_y_pred, dev_sst_sent_ids, dev_sts_corr, \
dev_sts_y_pred, dev_sts_sent_ids = model_eval_multitask(sst_dev_dataloader,
para_dev_dataloader,
sts_dev_dataloader, model, device)
test_para_y_pred, test_para_sent_ids, test_sst_y_pred, \
test_sst_sent_ids, test_sts_y_pred, test_sts_sent_ids = \
model_eval_test_multitask(sst_test_dataloader,
para_test_dataloader,
sts_test_dataloader, model, device)
with open(args.log_dir + args.sst_dev_out, "w+") as f:
print(f"dev sentiment acc :: {dev_sentiment_accuracy :.5f}")
f.write(f"id \t Predicted_Sentiment \n")
for p, s in zip(dev_sst_sent_ids, dev_sst_y_pred):
f.write(f"{p} , {s} \n")
with open(args.log_dir + args.sst_test_out, "w+") as f:
f.write(f"id \t Predicted_Sentiment \n")
for p, s in zip(test_sst_sent_ids, test_sst_y_pred):
f.write(f"{p} , {s} \n")
with open(args.log_dir + args.para_dev_out, "w+") as f:
print(f"dev paraphrase acc :: {dev_paraphrase_accuracy :.5f}")
f.write(f"id \t Predicted_Is_Paraphrase \n")
for p, s in zip(dev_para_sent_ids, dev_para_y_pred):
f.write(f"{p} , {s} \n")
with open(args.log_dir + args.para_test_out, "w+") as f:
f.write(f"id \t Predicted_Is_Paraphrase \n")
for p, s in zip(test_para_sent_ids, test_para_y_pred):
f.write(f"{p} , {s} \n")
with open(args.log_dir + args.sts_dev_out, "w+") as f:
print(f"dev sts corr :: {dev_sts_corr :.5f}")
f.write(f"id \t Predicted_Similiary \n")
for p, s in zip(dev_sts_sent_ids, dev_sts_y_pred):
f.write(f"{p} , {s} \n")
with open(args.log_dir + args.sts_test_out, "w+") as f:
f.write(f"id \t Predicted_Similiary \n")
for p, s in zip(test_sts_sent_ids, test_sts_y_pred):
f.write(f"{p} , {s} \n")
# Mean accuracy
print(f"Mean accuracy :: {np.mean([dev_sentiment_accuracy, dev_paraphrase_accuracy, dev_sts_corr]) :.5f}")