-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
346 lines (300 loc) · 11.3 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
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
import random
import json
import numpy as np
import math
import scipy
class CompareResultObject:
def __init__(
self,
raw_prob_A=0,
raw_prob_B=0,
raw_prob_C=0,
uncertainty=1,
logit_A=0,
logit_B=0,
logit_C=0,
):
self.raw_prob_A = raw_prob_A
self.raw_prob_B = raw_prob_B
self.raw_prob_C = raw_prob_C
prob_sum = raw_prob_A + raw_prob_B + raw_prob_C
self.prob_A = raw_prob_A / prob_sum
self.prob_B = raw_prob_B / prob_sum
self.prob_C = raw_prob_C / prob_sum
self.uncertainty = uncertainty
self.logit_A = logit_A
self.logit_B = logit_B
self.logit_C = logit_C
def calibraet_shift(self, shifts):
shifted_prob_A = self.raw_prob_A / np.exp(shifts["A"])
shifted_prob_B = self.raw_prob_B / np.exp(shifts["B"])
shifted_prob_C = self.raw_prob_C / np.exp(shifts["C"])
prob_sum = shifted_prob_A + shifted_prob_B + shifted_prob_C
self.prob_A = shifted_prob_A / prob_sum
self.prob_B = shifted_prob_B / prob_sum
self.prob_C = shifted_prob_C / prob_sum
def __str__(self) -> str:
string = f"prob_A: {round(self.prob_A,2)}, prob_B: {round(self.prob_B,2)}, prob_C: {round(self.prob_C,2)}, uncertainty: {round(self.uncertainty,3)} \n"
string += f"raw_prob_A: {round(self.raw_prob_A,2)}, raw_prob_B: {round(self.raw_prob_B,2)}, raw_prob_C: {round(self.raw_prob_C,2)}"
return string
def __getitem__(self, key):
return getattr(self, key, None)
def __setitem__(self, key, value):
setattr(self, key, value)
def to_json(self):
return {
"prob_A": float(self.prob_A),
"prob_B": float(self.prob_B),
"prob_C": float(self.prob_C),
"uncertainty": float(self.uncertainty),
"raw_prob_A": float(self.raw_prob_A),
"raw_prob_B": float(self.raw_prob_B),
"raw_prob_C": float(self.raw_prob_C),
}
@staticmethod
def from_json(json_obj):
instance = CompareResultObject(
json_obj["prob_A"],
json_obj["prob_B"],
json_obj["prob_C"],
json_obj["uncertainty"],
)
instance.prob_A = json_obj["prob_A"]
instance.prob_B = json_obj["prob_B"]
instance.prob_C = json_obj["prob_C"]
return instance
def calculate_uncertainty(probablities):
probablities = np.array(probablities) + 1e-9
entropy = -np.sum(probablities * np.log(probablities))
return entropy
############################################################################
###### Load datasets
############################################################################
def load_jsonl(file_path):
with open(file_path) as f:
lines = f.readlines()
return [json.loads(line) for line in lines]
def load_json(file_path):
with open(file_path) as f:
return json.load(f)
def load_gsm8k(data_path, cot=False):
data = load_jsonl(data_path)
if cot:
questions = [dp["question"] + "\nLet's think step by step." for dp in data]
else:
questions = [dp["question"] for dp in data]
# responses_doc = [dp['responses'] for dp in data]
responses_doc = []
for dp in data:
responses = []
for r in dp["responses"]:
responses.append(r.replace("\n\n", "\n"))
responses_doc.append(responses)
return questions, responses_doc
def load_summEval(path, flat_output=True, truncate_num_for_eval=None):
data_summ_eval = load_jsonl(path)
if truncate_num_for_eval:
data_summ_eval = data_summ_eval[: 16 * truncate_num_for_eval]
input = []
for i in range(len(data_summ_eval)):
input.append(data_summ_eval[i]["text"])
output = []
for i in range(len(data_summ_eval)):
output.append(data_summ_eval[i]["decoded"])
# coherence
coherence_scores = []
for i in range(len(data_summ_eval)):
coherence = [
anootation["coherence"]
for anootation in data_summ_eval[i]["expert_annotations"]
]
coherence_scores.append(round(sum(coherence) / len(coherence), 0))
# turker_annotations
# fluency
fluency_scores = []
for i in range(len(data_summ_eval)):
fluency = [
anootation["fluency"]
for anootation in data_summ_eval[i]["expert_annotations"]
]
fluency_scores.append(round(sum(fluency) / len(fluency), 1))
# relevance
relevance_scores = []
for i in range(len(data_summ_eval)):
relevance = [
anootation["relevance"]
for anootation in data_summ_eval[i]["expert_annotations"]
]
relevance_scores.append(round(sum(relevance) / len(relevance), 1))
# consistency
consistency_scores = []
for i in range(len(data_summ_eval)):
consistency = [
anootation["consistency"]
for anootation in data_summ_eval[i]["expert_annotations"]
]
consistency_scores.append(round(sum(consistency) / len(consistency), 1))
if flat_output:
return (
input,
output,
{
"coherence": coherence_scores,
"fluency": fluency_scores,
"relevance": relevance_scores,
"consistency": consistency_scores,
},
)
else:
candidate_num = 16
(
input_doc,
output_doc,
coherence_doc,
fluency_doc,
relevance_doc,
consistency_doc,
) = ([], [], [], [], [], [])
for i in range(0, len(input), candidate_num):
input_doc.append(input[i : i + candidate_num])
output_doc.append(output[i : i + candidate_num])
coherence_doc.append(coherence_scores[i : i + candidate_num])
fluency_doc.append(fluency_scores[i : i + candidate_num])
relevance_doc.append(relevance_scores[i : i + candidate_num])
consistency_doc.append(consistency_scores[i : i + candidate_num])
return (
input_doc,
output_doc,
{
"coherence": coherence_doc,
"fluency": fluency_doc,
"relevance": relevance_doc,
"consistency": consistency_doc,
},
)
def load_newsroom(path, flat_output=True, truncate_num_for_eval=None):
with open(path, "r") as file:
newsroom = json.load(file)
file.close()
data = newsroom
if truncate_num_for_eval:
data = data[: 7 * truncate_num_for_eval]
input = [dp["source"].replace("</p><p>", " ") for dp in data]
output = [dp["system_output"] for dp in data]
coherence = [round(dp["scores"]["coherence"], 1) for dp in data]
fluency = [round(dp["scores"]["fluency"], 1) for dp in data]
informativeness = [round(dp["scores"]["informativeness"], 1) for dp in data]
relevance = [round(dp["scores"]["relevance"], 1) for dp in data]
if flat_output:
return (
input,
output,
{
"coherence": coherence,
"fluency": fluency,
"informativeness": informativeness,
"relevance": relevance,
},
)
else:
candidate_num = 7
(
input_doc,
output_doc,
coherence_doc,
fluency_doc,
informativeness_doc,
relevance_doc,
) = ([], [], [], [], [], [])
for i in range(0, len(input), candidate_num):
input_doc.append(input[i : i + candidate_num])
output_doc.append(output[i : i + candidate_num])
coherence_doc.append(coherence[i : i + candidate_num])
fluency_doc.append(fluency[i : i + candidate_num])
informativeness_doc.append(informativeness[i : i + candidate_num])
relevance_doc.append(relevance[i : i + candidate_num])
return (
input_doc,
output_doc,
{
"coherence": coherence_doc,
"fluency": fluency_doc,
"informativeness": informativeness_doc,
"relevance": relevance_doc,
},
)
def load_TopicalChat(path, truncate_num_for_eval=None):
data = load_json(path)
if truncate_num_for_eval:
data = data[: 5 * truncate_num_for_eval]
input_doc = []
output_doc = []
overall_doc = []
natural_doc = []
engaging_doc = []
for i in range(len(data)):
input = []
facts = []
output = []
natural = []
overall = []
engaging = []
for r in data[i]["responses"]:
# Process input string to conversational format
input_string = data[i]["context"]
input_list = input_string.split("\n")
if input_list[-1] == "":
input_list = input_list[:-1]
input_list_with_user = []
for idx, line in enumerate(input_list):
if idx % 2 == 0:
input_list_with_user.append("Person 1: " + line)
next_round_person = "Person 2: "
if idx % 2 == 1:
input_list_with_user.append("Person 2: " + line)
next_round_person = "Person 1: "
input.append("\n".join(input_list_with_user))
facts.append(data[i]["fact"])
output.append(next_round_person + r["response"].strip())
natural.append(round(sum(r["Natural"]) / len(r["Natural"]), 0))
overall.append(round(sum(r["Overall"]) / len(r["Overall"]), 0))
engaging.append(round(sum(r["Engaging"]) / len(r["Engaging"]), 0))
input_doc.append(input)
output_doc.append(output)
overall_doc.append(overall)
natural_doc.append(natural)
engaging_doc.append(engaging)
return (
input_doc,
output_doc,
{"overall": overall_doc, "natural": natural_doc, "engaging": engaging_doc},
)
############################################################################
###### Helper Functions
############################################################################
def shuffle_lists(*args):
"""Shuffle multiple lists together and return the shuffled lists."""
# Check if all lists are of the same length
if len(set(map(len, args))) != 1:
raise ValueError("All lists must be of the same length")
# Combine the lists element-wise
combined_lists = list(zip(*args))
random.shuffle(combined_lists)
# Unzip the combined list into separate lists
shuffled_lists = zip(*combined_lists)
return [list(lst) for lst in shuffled_lists]
def calculate_correlation(reference_score, predicted_score, print_result=True):
spearman_corr, _ = scipy.stats.spearmanr(reference_score, predicted_score)
if math.isnan(spearman_corr):
spearman_corr = (
1
if all(element == reference_score[0] for element in reference_score)
else 0
)
kendall_tau, _ = scipy.stats.kendalltau(reference_score, predicted_score)
if print_result:
print("Spearmans correlation: %.3f" % spearman_corr)
print("Kendall tau: %.3f" % kendall_tau)
# mae = mean_absolute_error(reference_score, predicted_score)
# print('MAE: %.3f' % mae)
return spearman_corr, kendall_tau # , mae