-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval.py
More file actions
278 lines (215 loc) · 13.6 KB
/
eval.py
File metadata and controls
278 lines (215 loc) · 13.6 KB
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
import torch
from tqdm import tqdm
import sys
import re
from collections import Counter
import string
from torch.utils.data import DataLoader
import time
#Taken from https://github.com/Alab-NII/2wikimultihop/blob/main/2wikimultihop_evaluate_v1.1.py
def normalize_answer(s):
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(remove_articles(remove_punc(lower(s))))
def f1_score(prediction, ground_truth):
normalized_prediction = normalize_answer(prediction)
normalized_ground_truth = normalize_answer(ground_truth)
ZERO_METRIC = (0, 0, 0)
if normalized_prediction in ['yes', 'no', 'noanswer'] and normalized_prediction != normalized_ground_truth:
return ZERO_METRIC
if normalized_ground_truth in ['yes', 'no', 'noanswer'] and normalized_prediction != normalized_ground_truth:
return ZERO_METRIC
prediction_tokens = normalized_prediction.split()
ground_truth_tokens = normalized_ground_truth.split()
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
num_same = sum(common.values())
if num_same == 0:
return ZERO_METRIC
precision = 1.0 * num_same / len(prediction_tokens)
recall = 1.0 * num_same / len(ground_truth_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return f1, precision, recall
def exact_match_score(prediction, ground_truth):
return (normalize_answer(prediction) == normalize_answer(ground_truth))
#-------------------------------------------------------------------------------------------------
import torch.nn as nn
from src.utils.trainer_utils import load_model_checkpoint
from typing import Union
def evaluate_one_hop_wiki(model : Union[nn.Module],
tokenizer,
test_dataloader : DataLoader,
model_checkpoint_path : str,
device = 'cuda' if torch.cuda.is_available() else 'cpu'):
load_model_checkpoint(model, model_checkpoint_path, device)
model.to(device)
model.eval()
total_em = 0
total_f1 = 0
prediction_vs_label = {}
progress_bar = tqdm(test_dataloader, leave=True, desc=f"Test - Knowledge Integration", file = sys.stdout, dynamic_ncols=True)
with torch.no_grad():
for batch_idx, (input_str, label) in enumerate(progress_bar):
input_ids = tokenizer(input_str, padding=True, truncation=True, return_tensors='pt')['input_ids'].to(device)
attention_mask = tokenizer(input_str, padding=True, truncation=True, return_tensors='pt')['attention_mask'].to(device)
outputs = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
max_length=50,
num_beams = 5,
early_stopping=True
)
decoded_predictions = tokenizer.batch_decode(outputs, skip_special_tokens=True)
_f1_score = sum([f1_score(pred, truth)[0] for pred, truth, in zip(decoded_predictions, label)])
em_score = sum([1 if exact_match_score(pred, truth) else 0 for pred, truth in zip(decoded_predictions, label)])
total_em += em_score
total_f1 += _f1_score
prediction_vs_label[batch_idx] = f'Prediction: {decoded_predictions[0]} \n Label: {label[0]}'
avg_em_perc = total_em / len(test_dataloader.dataset)
avg_f1_perc = total_f1 / len(test_dataloader.dataset)
print(f"Test - AvgEM: {avg_em_perc:.4f} | AvgF1: {avg_f1_perc:.4f}")
return prediction_vs_label
from src.models import SoftPromptModel
def extract_answer(sequence):
"""Extract the e3 component from a sequence string."""
try:
return sequence.split(" ; ")[-1].strip()
except IndexError:
raise ValueError(f"Sequence '{sequence}' is not properly formatted.")
def evaluate_random_walk_training(finetuned_model : SoftPromptModel,
tokenizer,
test_dataloader : DataLoader,
#model_checkpoint_path : str,
#hopping_soft_prompt_checkpoint_path : str,
device = 'cuda' if torch.cuda.is_available() else 'cpu'):
inference_speed_per_iteration = []
finetuned_model.to(device)
finetuned_model.eval()
total_em = 0
total_f1 = 0
prediction_vs_label = {}
progress_bar = tqdm(test_dataloader, leave=True, desc=f"Test - Random Walk Training", file=sys.stdout, dynamic_ncols=True)
with torch.no_grad():
for batch_idx, batch in enumerate(progress_bar):
start_time = time.perf_counter()
incomplete_sequence, complete_sequence = batch
inputs = tokenizer(incomplete_sequence, padding=True, truncation=True, return_tensors = 'pt')
input_ids = inputs.input_ids.to(device)
attention_mask = inputs.attention_mask.to(device)
outputs = finetuned_model.generate(input_ids=input_ids,
attention_mask=attention_mask,
max_length=256,
num_beams = 5,
early_stopping=True)
decoded_predictions = tokenizer.batch_decode(outputs, skip_special_tokens=True)
end_time = time.perf_counter()
inference_speed_per_iteration.append((end_time - start_time))
_f1_score = sum([f1_score(pred, truth)[0] for pred, truth, in zip(decoded_predictions, complete_sequence)])
em_score = sum([1 if exact_match_score(pred, truth) else 0 for pred, truth in zip(decoded_predictions, complete_sequence)])
total_em += em_score
total_f1 += _f1_score
progress_bar.set_description(f'Test - Random Walk Training - EM: {total_em / ((batch_idx+1) * test_dataloader.batch_size)} | F1: {total_f1 / ((batch_idx+1) * test_dataloader.batch_size)}')
for idx, (input, pred, label) in enumerate(zip(incomplete_sequence, decoded_predictions, complete_sequence)):
if pred.strip().lower() != label.strip().lower():
index = batch_idx*test_dataloader.batch_size+idx
prediction_vs_label[index] = {}
prediction_vs_label[index]['input'] = input
prediction_vs_label[index]['prediction'] = pred
prediction_vs_label[index]['label'] = label
if batch_idx <= 5:
print('\n', f'Prediction: {decoded_predictions[0]} \n Label: {complete_sequence[0]}')
avg_em_perc = total_em / len(test_dataloader.dataset)
avg_f1_perc = total_f1 / len(test_dataloader.dataset)
sum_time = sum(inference_speed_per_iteration)
avg_time = sum_time / len(inference_speed_per_iteration)
print(f"Test - AvgEM: {avg_em_perc:.4f} | AvgF1: {avg_f1_perc:.4f}")
print(f"All Prediction took in total: {sum_time:.4f} seconds")
print(f"In average one iteration took: {avg_time:.4f} seconds")
print(f"Test - AvgEM: {avg_em_perc:.4f} | AvgF1: {avg_f1_perc:.4f}")
print(f"Total Correct Predictions: {total_em}/{len(test_dataloader.dataset)}")
return prediction_vs_label
import random
def evaluate_parse_then_hop_training(parsing_model: SoftPromptModel,
hopping_model: SoftPromptModel,
tokenizer,
test_dataloader : DataLoader,
do_extract_answer = False,
#model_checkpoint_path : str,
#hopping_soft_prompt_checkpoint_path: str,
#parsing_soft_prompt_checkpoint_path: str,
device = 'cuda' if torch.cuda.is_available() else 'cpu'):
print(f"We check metric on: {'Only Answer' if do_extract_answer else 'Whole Path'}")
inference_speed_per_iteration = []
parsing_model.to(device)
hopping_model.to(device)
parsing_model.eval()
hopping_model.eval()
total_em = 0
total_f1 = 0
progress_bar = tqdm(test_dataloader, leave=True, desc=f"Test - Parse Then Hop", file=sys.stdout, dynamic_ncols=True)
prediction_vs_label = {}
with torch.no_grad():
for batch_idx, batch in enumerate(progress_bar):
start_time = time.perf_counter()
question, complete_sequence = batch
# print(f"{complete_sequence = }")
# print(f"{question = }")
#parts = complete_sequence.split(' ; ') # Strip each part to remove extra spaces
#if len(parts) > 5:
# parts[0] = '| '.join([parts[0], parts[1]]) # Some parts have ; in the first entity we replace it with a |
#parts = [part.strip() for part in parts] #Some have double whitespaces we remove them and join with one whitespace
#complete_sequence = ' ; '.join(parts)
# print(f"{complete_sequence = }")
inputs_question = tokenizer(question, padding=True, truncation=True, return_tensors = 'pt').to(device)
incomplete_sequence = parsing_model.generate(input_ids=inputs_question.input_ids, attention_mask=inputs_question.attention_mask,
max_length=256,
num_beams = 10,
early_stopping=True)
decoded_incomplete_sequence = tokenizer.batch_decode(incomplete_sequence, skip_special_tokens=True)
inputs_incomplete_sequence = tokenizer(decoded_incomplete_sequence, padding=True, truncation=True, return_tensors='pt').to(device)
predictions = hopping_model.generate(input_ids=inputs_incomplete_sequence.input_ids, attention_mask=inputs_incomplete_sequence.attention_mask,
max_length=256,
num_beams = 5,
early_stopping=True)
decoded_predictions = tokenizer.batch_decode(predictions, skip_special_tokens=True)
end_time = time.perf_counter()
inference_speed_per_iteration.append((end_time - start_time))
if do_extract_answer:
_f1_score = sum([f1_score(extract_answer(pred), extract_answer(truth))[0] for pred, truth, in zip(decoded_predictions, complete_sequence)])
em_score = sum([1 if exact_match_score(extract_answer(pred), extract_answer(truth)) else 0 for pred, truth in zip(decoded_predictions, complete_sequence)])
else:
_f1_score = sum([f1_score(pred, truth)[0] for pred, truth, in zip(decoded_predictions, complete_sequence)])
em_score = sum([1 if exact_match_score(pred, truth) else 0 for pred, truth in zip(decoded_predictions, complete_sequence)])
progress_bar.set_description(f'Test - Parse Then Hop - EM: {total_em / ((batch_idx+1) * test_dataloader.batch_size)} | F1: {total_f1 / ((batch_idx+1) * test_dataloader.batch_size)}')
total_em += em_score
total_f1 += _f1_score
for idx, (input, pred, label) in enumerate(zip(incomplete_sequence, decoded_predictions, complete_sequence)):
if pred.strip().lower() != label.strip().lower():
index = batch_idx*test_dataloader.batch_size+idx
prediction_vs_label[index] = {}
prediction_vs_label[index]['incomplete_sequence'] = decoded_incomplete_sequence
prediction_vs_label[index]['prediction'] = pred
prediction_vs_label[index]['label'] = label
if extract_answer(pred) != extract_answer(label):
print("Wrong Prediction:")
print(f"Label: {label}")
print(f"Prediction: {pred}")
print("Answers:")
print(f"Label: {extract_answer(label)}")
print(f"Prediction: {extract_answer(pred)}")
if batch_idx <= 5:
print('\n', f'Prediction: {extract_answer(decoded_predictions[0])} \n Label: {extract_answer(complete_sequence[0])}')
avg_em_perc = total_em / len(test_dataloader.dataset)
avg_f1_perc = total_f1 / len(test_dataloader.dataset)
sum_time = sum(inference_speed_per_iteration)
avg_time = sum_time / len(inference_speed_per_iteration)
print(f"Test - AvgEM: {avg_em_perc:.4f} | AvgF1: {avg_f1_perc:.4f}")
print(f"All Prediction took in total: {sum_time:.4f} seconds")
print(f"In average one iteration took: {avg_time:.4f} seconds")
return prediction_vs_label