File size: 12,269 Bytes
650cfb0 |
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 |
import pandas as pd
import json
from numpy.ma.extras import average
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix
model_name = 'llama3-8b-nei-sft' # LLM_SFT
# model_name = 'llama3-8b-nei-guide-r1-final' # LLM_SI1
# model_name = 'llama3-8b-nei-guide-r2-final' # LLM_SI2
# Run for report results.
file_path = './testset/FEVEROUS.json'
save = []
dataset_name = []
def process_line(line, label):
line = line.strip() # Remove any leading/trailing whitespace
if 'support' in line or 'Support' in line:
return 1
else:
return 0
def process_file(file_path, labels):
results = []
with open(file_path, 'r') as file:
for i, line in enumerate(file):
if i >= len(labels):
raise IndexError(f"Not enough labels for line {i + 1}")
processed_value = process_line(line, labels[i])
results.append(processed_value)
return results
prediction_path = f'results_llama/FEVEROUS-{model_name}.txt'
label = []
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
for item in raw_data:
label.append(int(item['label'] == 'supports'))
prediction = process_file(prediction_path, label)
# 计算准确率
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
# 计算混淆矩阵
tn, fp, fn, tp = confusion_matrix(label, prediction).ravel()
type1_error = fp / (fp + tn) if (fp + tn) > 0 else 0
print(f'Type 1 Error (False Positive Rate): {type1_error: .2f}')
type2_error = fn / (fn + tp) if (fn + tp) > 0 else 0
print(f'Type 2 Error (False Negative Rate): {type2_error: .2f}')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f} {type1_error * 100: .2f} {type2_error * 100: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append('FEVEROUS')
for hop in [2, 3, 4]:
file_path = './testset/HOVER.json'
prediction_path = f'results_llama/HOVER-{model_name}.txt'
label0 = []
# Open the file and read line by line
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
for item in raw_data:
label0.append(int(item['label'] == 'supports'))
prediction0 = process_file(prediction_path, label0)
label = []
prediction = []
for i in range(len(prediction0)):
if raw_data[i]['num_hops'] == hop:
label.append(label0[i])
prediction.append(prediction0[i])
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
tn, fp, fn, tp = confusion_matrix(label, prediction).ravel()
type1_error = fp / (fp + tn) if (fp + tn) > 0 else 0
print(f'Type 1 Error (False Positive Rate): {type1_error: .2f}')
type2_error = fn / (fn + tp) if (fn + tp) > 0 else 0
print(f'Type 2 Error (False Negative Rate): {type2_error: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append(f'HOVER-{hop}hop')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f} {type1_error * 100: .2f} {type2_error * 100: .2f}')
file_path = './testset/FEVEROUS.json'
prediction_path = f'results_llama/Open_FEVEROUS-{model_name}.txt'
label = []
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
for item in raw_data:
label.append(int(item['label'] == 'supports'))
prediction = process_file(prediction_path, label)
# 计算准确率
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
tn, fp, fn, tp = confusion_matrix(label, prediction).ravel()
type1_error = fp / (fp + tn) if (fp + tn) > 0 else 0
print(f'Type 1 Error (False Positive Rate): {type1_error: .2f}')
type2_error = fn / (fn + tp) if (fn + tp) > 0 else 0
print(f'Type 2 Error (False Negative Rate): {type2_error: .2f}')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f} {type1_error * 100: .2f} {type2_error * 100: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append('Open_FEVEROUS')
for hop in [2, 3, 4]:
file_path = './testset/HOVER.json'
prediction_path = f'results_llama/Open_HOVER-{model_name}.txt'
label0 = []
# Open the file and read line by line
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
for item in raw_data:
label0.append(int(item['label'] == 'supports'))
prediction0 = process_file(prediction_path, label0)
label = []
prediction = []
for i in range(len(prediction0)):
if raw_data[i]['num_hops'] == hop:
label.append(label0[i])
prediction.append(prediction0[i])
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
tn, fp, fn, tp = confusion_matrix(label, prediction).ravel()
type1_error = fp / (fp + tn) if (fp + tn) > 0 else 0
print(f'Type 1 Error (False Positive Rate): {type1_error: .2f}')
type2_error = fn / (fn + tp) if (fn + tp) > 0 else 0
print(f'Type 2 Error (False Negative Rate): {type2_error: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append(f'Open_HOVER-{hop}hop')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f} {type1_error * 100: .2f} {type2_error * 100: .2f}')
file_path = './testset/LLM-AggreFact_test.json'
prediction_path = f'results_llama/LLM-AggreFact_test-{model_name}.txt'
label = []
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
for item in raw_data:
label.append(int(item['label'] == 1))
prediction = process_file(prediction_path, label)
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
tn, fp, fn, tp = confusion_matrix(label, prediction).ravel()
type1_error = fp / (fp + tn) if (fp + tn) > 0 else 0
print(f'Type 1 Error (False Positive Rate): {type1_error: .2f}')
type2_error = fn / (fn + tp) if (fn + tp) > 0 else 0
print(f'Type 2 Error (False Negative Rate): {type2_error: .2f}')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f} {type1_error * 100: .2f} {type2_error * 100: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append('LLM-AggreFact')
def process_line_nei(line, label):
line = line.strip() # Remove any leading/trailing whitespace
if line == "['support']" or line == "['Support']":
return 2
elif line == "['refute']" or line == "['Refute']":
return 0
else:
return 1
def process_file_nei(file_path, labels):
results = []
with open(file_path, 'r') as file:
for i, line in enumerate(file):
if i >= len(labels):
raise IndexError(f"Not enough labels for line {i + 1}")
processed_value = process_line_nei(line, labels[i])
results.append(processed_value)
return results
# for name in ['Scifact_train', 'Scifact_dev', 'Healthver_test']:
for name in ['Healthver_test', 'Open_Healthver_test', 'Scifact_train', 'Scifact_dev', 'VitaminC_dev', 'VitaminC_test']:
file_path = f'./testset/{name}.json'
# Example usage
prediction_path = f'results_llama/{name}-{model_name}.txt'
label = []
# Open the file and read line by line
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
if name == 'Healthver_test' or name == 'Open_Healthver_test':
for item in raw_data:
if item['label'] == 'Supports':
label.append(int(2))
elif item['label'] == 'Neutral':
label.append(int(1))
else:
label.append(int(0))
elif name == 'VitaminC_dev' or name == 'VitaminC_test':
for item in raw_data:
if item['label'] == 'SUPPORTS':
label.append(int(2))
elif item['label'] == 'NOT ENOUGH INFO':
label.append(int(1))
else:
label.append(int(0))
else:
for item in raw_data:
if item['label'] == 'SUPPORT':
label.append(int(2))
elif item['label'] == 'UNKNOWN':
label.append(int(1))
else:
label.append(int(0))
prediction = process_file_nei(prediction_path, label)
# 计算准确率
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append(name + ' w NEI')
# for name in ['Scifact_train', 'Scifact_dev', 'Healthver_test']:
for name in ['Healthver_test', 'Open_Healthver_test', 'Scifact_train', 'Scifact_dev', 'VitaminC_dev', 'VitaminC_test']:
file_path = f'./testset/{name}.json'
# Example usage
prediction_path = f'results_llama/{name}-{model_name}.txt'
label = []
with open(file_path, 'r', encoding='utf-8') as file:
raw_data = json.load(file)
if name == 'Healthver_test' or name == 'Open_Healthver_test':
for item in raw_data:
if item['label'] == 'Supports':
label.append(int(1))
else:
label.append(int(0))
elif name == 'VitaminC_dev' or name == 'VitaminC_test':
for item in raw_data:
if item['label'] == 'SUPPORTS':
label.append(int(1))
else:
label.append(int(0))
else:
for item in raw_data:
if item['label'] == 'SUPPORT':
label.append(int(1))
else:
label.append(int(0))
prediction = process_file(prediction_path, label)
accuracy = sum([int(label[i] == prediction[i]) for i in range(len(label))]) / len(label)
print(f'Accuracy: {accuracy: .4f}')
macro_f1 = f1_score(label, prediction, average='macro')
print(f'Macro F1 Score: {macro_f1: .4f}')
print(f'{accuracy * 100: .2f} {macro_f1 * 100: .2f}')
save.append(f'{macro_f1 * 100: .2f}')
dataset_name.append(name + ' w/o NEI')
import pandas as pd
from tabulate import tabulate
show_rank = []
show_rank.append([0, 1, 2, 3, 15, 9])
show_rank.append([4, 5, 6, 7, 16, 10])
show_rank.append([8, 17, 11, 18, 12, 19, 13, 20, 14])
dataset_name_show = []
performance_show = []
for rank in show_rank:
dataset_temp = []
save_temp = []
for number in rank:
dataset_temp.append(dataset_name[number])
save_temp.append(save[number])
dataset_name_show.append(dataset_temp)
performance_show.append(save_temp)
df1 = pd.DataFrame({
"Dataset Name": dataset_name_show[0],
"Performance": performance_show[0]
})
df2 = pd.DataFrame({
"Dataset Name": dataset_name_show[1],
"Performance": performance_show[1]
})
df3 = pd.DataFrame({
"Dataset Name": dataset_name_show[2],
"Performance": performance_show[2]
})
for table in range(3):
for i in range(len(performance_show[table])):
print(f'{performance_show[table][i]}', end=' ')
print('')
table = tabulate(df1.T, headers='keys', tablefmt='fancy_grid')
print(table)
table = tabulate(df2.T, headers='keys', tablefmt='fancy_grid')
print(table)
table = tabulate(df3.T, headers='keys', tablefmt='fancy_grid')
print(table)
|