-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetrics.py
401 lines (322 loc) · 12.8 KB
/
metrics.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import torch
from .seqeval_modified import f1_score, accuracy_score, recall_score, precision_score, classification_report
from sklearn.metrics import accuracy_score as sk_accuracy_score
from sklearn.metrics import precision_score as sk_precision_score
from sklearn.metrics import recall_score as sk_recall_score
from sklearn.metrics import fbeta_score as sk_fbeta_score
from .wnuteval import doc_to_toks, doc_to_entities, fmt_results, get_tagged_entities, calc_results, get_tags, filter_entities
NER_LABELS = ('O', 'I-LOC', 'I-PER', 'I-MISC', 'I-ORG', 'B-LOC', 'B-PER', 'B-MISC', 'B-ORG')
def get_TP(pred, real, positive=1, ignore=None):
if ignore is None:
return ((pred == positive) & (real == positive)).int().sum()
else:
return ((pred == positive) & (real == positive) & (real != ignore)).int().sum()
def get_TN(pred, real, positive=1, ignore=None):
if ignore is None:
return ((pred == 0) & (real == 0)).int().sum()
else:
return ((pred == 0) & (real == 0) & (real != ignore)).int().sum()
def get_FP(pred, real, positive=1, ignore=None):
if ignore is None:
return ((pred == positive) & (real == 0)).int().sum()
else:
return ((pred == positive) & (real == 0) & (real != ignore)).int().sum()
def get_FN(pred, real, positive=1, ignore=None):
if ignore is None:
return ((pred == 0) & (real == positive)).int().sum()
else:
return ((pred == 0) & (real == positive) & (real != ignore)).int().sum()
def classify(data, multiclass=True):
if multiclass:
classif = data.argmax(dim=1)
else:
classif = (data >= 0.5).long()
return classif
def get_accuracy(pred, real, positive=1):
pred = classify(pred)
same = (pred == real).view(-1).int().sum()
return same.float() / pred.view(-1).shape[0]
def get_precision(pred, real, positive=1):
pred = classify(pred)
TP = ((pred == positive) & (real == positive)).int().sum()
FP = ((pred == positive) & (real == 0)).int().sum()
if (TP + FP).item() == 0:
return torch.tensor([0.])
return TP.float() / (TP + FP)
def get_recall(pred, real, positive=1):
pred = classify(pred)
TP = ((pred == positive) & (real == positive)).int().sum()
FN = ((pred == 0) & (real == positive)).int().sum()
if (TP + FN).item() == 0:
return torch.tensor([0.])
return TP.float() / (TP + FN)
def get_f05(precision, recall, positive=1):
if precision == 0 and recall == 0:
return 0.
return (1 + 0.5 ** 2) * precision * recall / (0.5 ** 2 * precision + recall)
def get_ner_metrics(y_true, y_pred, digits=4, average="micro", skipreport=False):
precision = precision_score(y_true, y_pred, average=average)
recall = recall_score(y_true, y_pred, average=average)
if precision + recall == 0:
f05score = 0
else:
f05score = (1 + 0.5 ** 2) * precision * recall / (0.5 ** 2 * precision + recall)
return {
"report": None if skipreport else classification_report(y_true, y_pred, digits=digits),
"precision": precision,
"f1score": f1_score(y_true, y_pred, average=average),
"recall": recall,
"accuracy": accuracy_score(y_true, y_pred),
"f05score": f05score
}
def get_wnut_metrics(y_true, y_pred, digits=4, average="micro"):
lines = []
for s in range(len(y_true)):
line = []
for t in range(len(y_true[s])):
pred = y_pred[s][t]
if pred == "[CLS]" or pred == "[SEP]" or pred == "[PAD]":
pred = "O"
line.append(f"PLACEHOLDER\t{y_true[s][t]}\t{pred}")
lines.extend(line)
lines.append("")
tokens = doc_to_toks(lines)
all_entities = doc_to_entities(lines)
# report results
_sys = 'sys_1'
# throw out 'O' tags to get overall p/r/f
tagged_entities = get_tagged_entities(all_entities)
results = {'all': calc_results(all_entities['gold'], all_entities[_sys], surface_form=False),
'tagged': calc_results(tagged_entities['gold'], tagged_entities[_sys], False),
'tokens': calc_results(tokens['gold'], tokens[_sys], surface_form=False)}
accuracy = results['tokens'].correct / results['tokens'].gold
precision = results['tagged'].p
recall = results['tagged'].r
f1score = results['tagged'].f
tags = get_tags(all_entities['gold'])
lines = ["precision\trecall\tf1_score\tsupport\n"]
for tag in sorted(tags):
ents = {src: filter_entities(entities, lambda e: e.tag == tag)
for src, entities in all_entities.items()}
results = calc_results(ents['gold'], ents[_sys], False)
lines.append(f"{tag}\t{results.p}\t{results.r}\t{results.f}\t0")
lines += [""]
lines += [f"micro\t{precision}\t{recall}\t{f1score}\t0"]
lines += [f"macro\t{precision}\t{recall}\t{f1score}\t0"]
report = "\n".join(lines)
return {
"report": report,
"precision": precision,
"f1score": f1score,
"recall": recall,
"accuracy": accuracy,
"f05score": f1score
}
def get_nli_metrics(y_true, y_pred, digits=4):
y_true = y_true[0]
y_pred = y_pred[0]
TPs = [0., 0., 0.]
FPs = [0., 0., 0.]
FNs = [0., 0., 0.]
TNs = [0., 0., 0.]
labels = ["neutral", "entailment", "contradiction"]
other = 0.
for s_true, s_pred in zip(y_true, y_pred):
for i, l in enumerate(labels):
if s_true == s_pred == labels[i]:
TPs[i] += 1
elif s_true == s_pred != labels[i]:
TNs[i] += 1
elif s_true == labels[i] and s_pred != labels[i]:
FNs[i] += 1
elif s_true != labels[i] and s_pred == labels[i]:
FPs[i] += 1
else:
other += 1
accuracies = []
precisions = []
recalls = []
f1_scores = []
f05_scores = []
for i in range(len(labels)):
if (TPs[i] + FPs[i] + FNs[i] + TNs[i]) == 0:
accuracy = 0.
else:
accuracy = (TPs[i] + TNs[i]) / (TPs[i] + FPs[i] + FNs[i] + TNs[i])
if (TPs[i] + FPs[i]) == 0:
precision = 0.
else:
precision = TPs[i] / (TPs[i] + FPs[i])
if (TPs[i] + FNs[i]) == 0:
recall = 0.
else:
recall = TPs[i] / (TPs[i] + FNs[i])
if precision == 0 and recall == 0:
f05 = 0
f1s = 0
else:
beta = 0.5
f05 = (1 + beta ** 2) * TPs[i] / ((1 + beta ** 2) * TPs[i] + beta ** 2 * FNs[i] + FPs[i])
beta = 1.
f1s = (1 + beta ** 2) * TPs[i] / ((1 + beta ** 2) * TPs[i] + beta ** 2 * FNs[i] + FPs[i])
accuracies.append(accuracy)
precisions.append(precision)
recalls.append(recall)
f1_scores.append(f1s)
f05_scores.append(f05)
to_write = [
["accuracy:", f"{accuracies[0]:.{digits}f}\t{accuracies[1]:.{digits}f}\t{accuracies[2]:.{digits}f}"],
["precision:", f"{precisions[0]:.{digits}f}\t{precisions[1]:.{digits}f}\t{precisions[2]:.{digits}f}"],
["recall:", f"{recalls[0]:.{digits}f}\t{recalls[1]:.{digits}f}\t{recalls[2]:.{digits}f}"],
["f05 score:", f"{f05_scores[0]:.{digits}f}\t{f05_scores[1]:.{digits}f}\t{f05_scores[2]:.{digits}f}\t"]
]
col_width = max(len(word) for row in to_write for word in row) + 2 # padding
report = "\n".join(["".join(word.rjust(col_width) for word in row) for row in to_write])
return {
"report": report,
"precision": sum(precisions)/len(labels),
"f1score": sum(f1_scores)/len(labels),
"f05score": sum(f05_scores)/len(labels),
"recall": sum(recalls)/len(labels),
"accuracy": sum(accuracies)/len(labels),
"TP": sum(TPs),
"FP": sum(FPs),
"FN": sum(FNs),
"TN": sum(TNs),
"other": other
}
def get_ir_metrics(y_true, y_pred, digits=4):
y_true = y_true[0]
y_pred = y_pred[0]
accuracy = sk_accuracy_score(y_true, y_pred)
precision = sk_precision_score(y_true, y_pred, average="micro")
recall = sk_recall_score(y_true, y_pred, average="micro")
f1_score = sk_fbeta_score(y_true, y_pred, beta=1., average="micro")
f05_score = sk_fbeta_score(y_true, y_pred, beta=.5, average="micro")
to_write = [
["accuracy:", f"{accuracy:.{digits}f}"],
["precision:", f"{precision:.{digits}f}"],
["recall:", f"{recall:.{digits}f}"],
["f05 score:", f"{f05_score:.{digits}f}"],
["f1 score:", f"{f1_score:.{digits}f}"],
]
col_width = max(len(word) for row in to_write for word in row) + 2 # padding
report = "\n".join(["".join(word.rjust(col_width) for word in row) for row in to_write])
return {
"report": report,
"precision": precision,
"f1score": f1_score,
"f05score": f05_score,
"recall": recall,
"accuracy": accuracy,
"TP": 0,
"FP": 0,
"FN": 0,
"TN": 0,
"other": 0
}
def get_ged_metrics(y_true, y_pred, digits=4):
TP = 0
FP = 0
FN = 0
TN = 0
other = 0
for s_true, s_pred in zip(y_true, y_pred):
for w_true, w_pred in zip(s_true, s_pred):
if w_true == w_pred == 'c':
TN += 1
elif w_true == w_pred == 'i':
TP += 1
elif w_true == 'c' and w_pred == 'i':
FP += 1
elif w_true == 'i' and w_pred == 'c':
FN += 1
else:
other += 1
TP, FP, FN, TN = float(TP), float(FP), float(FN), float(TN)
if (TP + FP + FN + TN) == 0:
accuracy = 0.
else:
accuracy = (TP + TN) / (TP + FP + FN + TN)
if (TP + FP) == 0:
precision = 0.
else:
precision = TP / (TP + FP)
if (TP + FN) == 0:
recall = 0.
else:
recall = TP / (TP + FN)
if precision == 0 and recall == 0:
f05 = 0
f1s = 0
else:
beta = 0.5
f05 = (1 + beta ** 2) * TP / ((1 + beta ** 2) * TP + beta ** 2 * FN + FP)
beta = 1.
f1s = (1 + beta ** 2) * TP / ((1 + beta ** 2) * TP + beta ** 2 * FN + FP)
to_write = [
["accuracy:", f"{accuracy:.{digits}f}"],
["precision:", f"{precision:.{digits}f}"],
["recall:", f"{recall:.{digits}f}"],
["f05 score:", f"{f05:.{digits}f}"]
]
col_width = max(len(word) for row in to_write for word in row) + 2 # padding
report = "\n".join(["".join(word.rjust(col_width) for word in row) for row in to_write])
return {
"report": report,
"precision": precision,
"f1score": f1s,
"f05score": f05,
"recall": recall,
"accuracy": accuracy,
"TP": int(TP),
"FP": int(FP),
"FN": int(FN),
"TN": int(TN),
"other": other
}
def get_all_metrics(pred, real, positive=1, multiclass=True, ner=False):
if ner and multiclass:
real = [NER_LABELS[i] for i in real]
pred = [NER_LABELS[i] for i in pred]
precision = precision_score(real, pred)
recall = recall_score(real, pred)
accuracy = accuracy_score(real, pred)
f1s = f1_score(real, pred)
return {
"accuracy": (accuracy, accuracy * 100.),
"precision": (precision, precision * 100.),
"recall": (recall, recall * 100.),
"f1s": (f1s, f1s * 100.)
}
else:
TP = get_TP(pred, real, positive=positive)
FN = get_FN(pred, real, positive=positive)
FP = get_FP(pred, real, positive=positive)
TN = get_TN(pred, real, positive=positive)
accuracy = ((TP + TN).float() / (TP + FP + FN + TN)).item()
if (TP + FP).item() == 0:
precision = 0.
else:
precision = (TP.float() / (TP + FP)).item()
if (TP + FN).item() == 0:
recall = 0.
else:
recall = (TP.float() / (TP + FN)).item()
if precision == 0 and recall == 0:
f05 = 0
f1s = 0
else:
f05 = (1 + 0.5 ** 2) * precision * recall / (0.5 ** 2 * precision + recall)
f1s = 2 * precision * recall / (precision + recall)
return {
"TP": int(TP.item()),
"FN": int(FN.item()),
"FP": int(FP.item()),
"TN": int(TN.item()),
"accuracy": accuracy,
"precision": precision,
"recall": recall,
"f05score": f05,
"f1score": f1s
}