-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
executable file
·206 lines (171 loc) · 6.07 KB
/
utils.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
import os
import torch
from squad_metrics import (
compute_predictions_log_probs,
compute_predictions_logits,
squad_evaluate,
)
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
import torch.nn as nn
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class SquadResult(object):
def __init__(self, unique_id, start_logits, end_logits, start_top_index=None, end_top_index=None, cls_logits=None):
self.start_logits = start_logits
self.end_logits = end_logits
self.unique_id = unique_id
if start_top_index:
self.start_top_index = start_top_index
self.end_top_index = end_top_index
self.cls_logits = cls_logits
def to_list(tensor):
return tensor.detach().cpu().tolist()
def evaluateQA(model, corpus, task, path):
dataset, examples, features = corpus.dataset, corpus.examples, corpus.features
tokenizer = corpus.tokenizer
# Note that DistributedSampler samples randomly
eval_sampler = SequentialSampler(dataset)
eval_dataloader = DataLoader(dataset, sampler=eval_sampler, batch_size=12)
all_results = []
for batch in eval_dataloader:
model.eval()
with torch.no_grad():
inputs = {
"input_ids": batch[0],
"attention_mask": batch[1],
"token_type_ids": batch[2],
"answer_start": None,
"answer_end": None,
}
example_indices = batch[3]
outputs = model('qa',inputs)
for i, example_index in enumerate(example_indices):
eval_feature = features[example_index.item()]
unique_id = int(eval_feature.unique_id)
output = [to_list(output[i]) for output in outputs]
start_logits, end_logits = output
result = SquadResult(unique_id, start_logits, end_logits)
all_results.append(result)
# Compute predictions
output_prediction_file = os.path.join(path,"predictions_{}.json".format(task))
output_nbest_file = os.path.join(path,"nbest_predictions_{}.json".format(task))
output_null_log_odds_file = None
predictions = compute_predictions_logits(
examples,
features,
all_results,
20,
30,
True,
output_prediction_file,
output_nbest_file,
output_null_log_odds_file,
False,
False,
0.0,
tokenizer,
)
# Compute the F1 and exact scores.
results = squad_evaluate(examples, predictions)
return results
def evaluateNLI(model, data, device):
with torch.no_grad():
total_loss = 0.0
correct = 0.0
total = 0.0
matrix = [[0 for _ in range(3)] for _ in range(3)]
for j,batch in enumerate(data):
batch['label'] = batch['label'].to(device)
output = model.forward('sc',batch)
loss, logits = output[0].mean(), output[1]
prediction = torch.argmax(logits, dim=1)
correct += torch.sum(prediction == batch['label']).item()
for k in range(batch['label'].shape[0]):
matrix[batch['label'][k]][prediction[k]] += 1
total += batch['label'].shape[0]
total_loss += loss.item()
total_loss /= len(data)
return total_loss, correct / total#, matrix
def evaluatePA(model, data, device):
with torch.no_grad():
total_loss = 0.0
correct = 0.0
total = 0.0
matrix = [[0 for _ in range(2)] for _ in range(2)]
for j,batch in enumerate(data):
batch['label'] = batch['label'].to(device)
output = model.forward('pa',batch)
loss, logits = output[0].mean(), output[1]
prediction = torch.argmax(logits, dim=1)
correct += torch.sum(prediction == batch['label']).item()
for k in range(batch['label'].shape[0]):
matrix[batch['label'][k]][prediction[k]] += 1
total += batch['label'].shape[0]
total_loss += loss.item()
total_loss /= len(data)
return total_loss, correct / total#, matrix
def macro_f1(array):
n = len(array)
p = [0 for _ in range(n-1)]
r = [0 for _ in range(n-1)]
f1 = [0 for _ in range(n-1)]
for i in range(n-1):
p[i] = array[i][i] / sum([array[j][i] for j in range(n-1)]) if sum([array[j][i] for j in range(n-1)]) != 0 else 0
r[i] = array[i][i] / sum([array[i][j] for j in range(n-1)]) if sum([array[i][j] for j in range(n-1)]) != 0 else 0
f1[i] = 2*p[i]*r[i] / (p[i] + r[i]) if (p[i] + r[i] != 0) else 0
return sum(f1) / (n-1)
def evaluateNER(model, data, device):
with torch.no_grad():
total_loss = 0.0
correct = 0.0
total = 0.0
matrix = [[0 for _ in range(10)] for _ in range(10)]
for j,batch in enumerate(data):
batch['mask'] = batch['mask'].to(device)
batch['label_ids'] = batch['label_ids'].to(device)
output = model.forward('tc',batch)
loss, logits = output[0].mean(), output[1]
active_loss = batch['mask'].view(-1) == 1
active_logits = logits.view(-1, 10)[active_loss]
active_labels = batch['label_ids'].view(-1)[active_loss]
prediction = torch.argmax(active_logits, dim=1)
correct += torch.sum(prediction == active_labels).item()
for k in range(active_labels.shape[0]):
matrix[active_labels[k]][prediction[k]] += 1
total += active_labels.shape[0]
total_loss += loss.item()
total_loss /= len(data)
return total_loss, correct / total# macro_f1(matrix)
def evaluatePOS(model, data, device):
with torch.no_grad():
total_loss = 0.0
correct = 0.0
total = 0.0
for j,batch in enumerate(data):
batch['mask'] = batch['mask'].to(device)
batch['label_ids'] = batch['label_ids'].to(device)
output = model.forward('po',batch)
loss, logits = output[0].mean(), output[1]
active_loss = batch['mask'].view(-1) == 1
active_logits = logits.view(-1, 18)[active_loss]
active_labels = batch['label_ids'].view(-1)[active_loss]
prediction = torch.argmax(active_logits, dim=1)
correct += torch.sum(prediction == active_labels).item()
total += active_labels.shape[0]
total_loss += loss.item()
total_loss /= len(data)
return total_loss, correct / total
def evaluateRC(model,data,device):
with torch.no_grad():
total_loss = 0.0
correct = 0.0
total = 0.0
for j,batch in enumerate(data):
batch['answer_tok_pos'] = batch['answer_tok_pos'].to(device)
output = model.forward('rc',batch)
loss, logits = output[0].mean(), output[1]
prediction = torch.argmax(logits, dim=1)
correct += torch.sum(prediction == batch['answer_tok_pos']).item()
total += batch['answer_tok_pos'].shape[0]
total_loss += loss.item()
total_loss /= len(data)
return total_loss, correct / total