-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval_utils.py
executable file
·263 lines (225 loc) · 7.94 KB
/
eval_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
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
import glob, json
import numpy as np
import argparse
import torch
from torchmetrics.text.rouge import ROUGEScore
rougeScore = ROUGEScore()
from bert_score import score
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
import nltk
def get_bleu(recover, reference):
return sentence_bleu(
[reference.split()],
recover.split(),
smoothing_function=SmoothingFunction().method4,
)
def selectBest(sentences):
selfBleu = [[] for i in range(len(sentences))]
for i, s1 in enumerate(sentences):
for j, s2 in enumerate(sentences):
score = get_bleu(s1, s2)
selfBleu[i].append(score)
for i, s1 in enumerate(sentences):
selfBleu[i][i] = 0
idx = np.argmax(np.sum(selfBleu, -1))
return sentences[idx]
def diversityOfSet(sentences):
selfBleu = []
# print(sentences)
for i, sentence in enumerate(sentences):
for j in range(i + 1, len(sentences)):
# print(sentence, sentences[j])
score = get_bleu(sentence, sentences[j])
selfBleu.append(score)
if len(selfBleu) == 0:
selfBleu.append(0)
div4 = distinct_n_gram_inter_sent(sentences, 4)
return np.mean(selfBleu), div4
def distinct_n_gram(hypn, n):
dist_list = []
for hyp in hypn:
hyp_ngrams = []
hyp_ngrams += nltk.ngrams(hyp.split(), n)
total_ngrams = len(hyp_ngrams)
unique_ngrams = len(list(set(hyp_ngrams)))
if total_ngrams == 0:
return 0
dist_list.append(unique_ngrams / total_ngrams)
return np.mean(dist_list)
def distinct_n_gram_inter_sent(hypn, n):
hyp_ngrams = []
for hyp in hypn:
hyp_ngrams += nltk.ngrams(hyp.split(), n)
total_ngrams = len(hyp_ngrams)
unique_ngrams = len(list(set(hyp_ngrams)))
if total_ngrams == 0:
return 0
dist_n = unique_ngrams / total_ngrams
return dist_n
def _evaluate(_folder, _mbr, _eos, _sos, _sep, _pad, candidate_num=-1):
print("start evaluation ")
files = sorted(glob.glob(f"{_folder}/*json"))
assert len(files) >= candidate_num
if candidate_num > 0:
files = files[:candidate_num]
print(f"*** only evaluate {candidate_num} candidates ***")
else:
print(f"** evaluate {len(files)} candidates **")
for _file in files:
print(_file)
sample_num = 0
with open(files[0], "r") as f:
for row in f:
sample_num += 1
recoverDict, referenceDict, sourceDict = {}, {}, {}
for i in range(sample_num):
recoverDict[i] = []
referenceDict[i] = []
sourceDict[i] = []
div4, selfBleu = [], []
_dict = {"sample_num": sample_num}
for path in files:
print(path)
sources, references, recovers, bleu, rougel, avg_len, dist1 = (
[],
[],
[],
[],
[],
[],
[],
)
with open(path, "r") as f:
cnt = 0
for row in f:
source = json.loads(row)["source"].strip()
reference = json.loads(row)["reference"].strip()
recover = json.loads(row)["recover"].strip()
source = source.replace(_eos, "").replace(_sos, "")
reference = (
reference.replace(_eos, "").replace(_sos, "").replace(_sep, "")
)
recover = (
recover.replace(_eos, "")
.replace(_sos, "")
.replace(_sep, "")
.replace(_pad, "")
)
sources.append(source)
references.append(reference)
recovers.append(recover)
avg_len.append(len(recover.split(" ")))
bleu.append(get_bleu(recover, reference))
rougel.append(
rougeScore(recover, reference)["rougeL_fmeasure"].tolist()
)
dist1.append(distinct_n_gram([recover], 1))
recoverDict[cnt].append(recover)
referenceDict[cnt].append(reference)
sourceDict[cnt].append(source)
cnt += 1
P, R, F1 = score(
recovers,
references,
model_type="microsoft/deberta-xlarge-mnli",
lang="en",
verbose=True,
)
if len(files) == 1:
print("*" * 30)
_dict.update(
bleu=np.mean(bleu),
rouge=np.mean(rougel),
berscore=torch.mean(F1).item(),
dist1=np.mean(dist1),
avg_len=np.mean(avg_len),
)
print(_dict)
return _dict
if len(files) > 1:
print("*" * 30)
print("Compute diversity...")
print("*" * 30)
for k, v in recoverDict.items():
if len(v) == 0:
continue
sb, d4 = diversityOfSet(v)
selfBleu.append(sb)
div4.append(d4)
_dict.update(selfBleu=np.mean(selfBleu), div4=np.mean(div4))
if not _mbr:
return _dict
else:
print("*" * 30)
print("MBR...")
print("*" * 30)
bleu = []
rougel = []
avg_len = []
dist1 = []
recovers = []
references = []
sources = []
for k, v in recoverDict.items():
if len(v) == 0 or len(referenceDict[k]) == 0:
continue
recovers.append(selectBest(v))
references.append(referenceDict[k][0])
sources.append(sourceDict[k][0])
for source, reference, recover in zip(sources, references, recovers):
bleu.append(get_bleu(recover, reference))
rougel.append(
rougeScore(recover, reference)["rougeL_fmeasure"].tolist()
)
avg_len.append(len(recover.split(" ")))
dist1.append(distinct_n_gram([recover], 1))
# print(len(recovers), len(references), len(recovers))
P, R, F1 = score(
recovers,
references,
model_type="microsoft/deberta-xlarge-mnli",
lang="en",
verbose=True,
)
print("MBR " + "*" * 30)
_dict.update(
bleu=np.mean(bleu),
rouge=np.mean(rougel),
berscore=torch.mean(F1).item(),
dist1=np.mean(dist1),
avg_len=np.mean(avg_len),
candidate_num=len(files),
)
print(_dict)
return _dict
return _dict
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="decoding args.")
parser.add_argument(
"--folder",
type=str,
default="diffusion_models/diffuseq_qqp_h128_lr0.0001_t2000_sqrt_lossaware_seed102_test-cc20230605-09:38:38/samples",
help="path to the folder of decoded texts",
)
parser.add_argument("--mbr", action="store_true", help="mbr decoding or not")
parser.add_argument(
"--sos", type=str, default="[CLS]", help="start token of the sentence"
)
parser.add_argument(
"--eos", type=str, default="[SEP]", help="end token of the sentence"
)
parser.add_argument(
"--sep", type=str, default="[SEP]", help="sep token of the sentence"
)
parser.add_argument(
"--pad", type=str, default="[PAD]", help="pad token of the sentence"
)
args = parser.parse_args()
_evaluate(
_folder=args.folder,
_mbr=args.mbr,
_eos=args.eos,
_sos=args.sos,
_sep=args.sep,
_pad=args.pad,
)