-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhuman_YAGO3_eval_rel.py
177 lines (141 loc) · 5.94 KB
/
human_YAGO3_eval_rel.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
prompts = []
labels = []
with open("data/YAGO3-10/YAGO3_relation_test_human.tsv", "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
tmp = line.strip().split("\t")
#print(tmp)
prompts.append(tmp[0])
labels.append(tmp[1])
correct_count = 0
lines_to_write = []
with open("data/YAGO3-10/pred_instructions_llama_rel.csv", "r", encoding="utf-8") as f:
txt = f.read()
for (i, p) in enumerate(prompts):
begin_idx = txt.find(p)
end_idx = txt[begin_idx:].find("\n")
target_line = txt[begin_idx: begin_idx + end_idx]
#print(p)
#print(target_line)
lines_to_write.append(target_line + "\n")
res_begin_idx = target_line.rfind(",")
res = target_line[res_begin_idx + 1:]
#print(res)
label = labels[i]
if res == label:
correct_count += 1
with open("data/YAGO3-10/pred_instructions_llama_rel_100.csv", "w", encoding="utf-8") as f:
f.writelines(lines_to_write)
print("LLaMA-7B acc: ", correct_count, 1.0 * correct_count/len(labels))
correct_count = 0
lines_to_write = []
with open("data/YAGO3-10/pred_instructions_llama_rel13b.csv", "r", encoding="utf-8") as f:
txt = f.read()
for (i, p) in enumerate(prompts):
begin_idx = txt.find(p)
end_idx = txt[begin_idx:].find("\n")
target_line = txt[begin_idx: begin_idx + end_idx]
#print(p)
#print(target_line)
lines_to_write.append(target_line + "\n")
res_begin_idx = target_line.rfind(",")
res = target_line[res_begin_idx + 1:]
#print(res)
label = labels[i]
if res == label:
correct_count += 1
with open("data/YAGO3-10/pred_instructions_llama_rel13b_100.csv", "w", encoding="utf-8") as f:
f.writelines(lines_to_write)
print("LLaMA-13B acc: ", correct_count, 1.0 * correct_count/len(labels))
correct_count = 0
lines_to_write = []
with open("data/YAGO3-10/pred_instructions_llama_rel_raw.csv", "r", encoding= "utf-8") as f:
txt = f.read()
for (i, p) in enumerate(prompts):
begin_idx = txt.find(p)
end_idx = txt[begin_idx:].find("\n")
target_line = txt[begin_idx: begin_idx + end_idx]
#print(target_line)
lines_to_write.append(target_line + "\n")
res_begin_idx = target_line.find("|plays for.")
res = target_line[res_begin_idx + len("|plays for."):]
#print(res)
#print(label)
label = labels[i]
if res.find(label) != -1 and res.find("Please choose your answer from:") == -1:
correct_count += 1
#print(target_line, res, label)
with open("data/YAGO3-10/pred_instructions_llama_raw_rel_100.csv", "w", encoding= "utf-8") as f:
f.writelines(lines_to_write)
print("LLaMA-7B raw acc: ", correct_count, 1.0 * correct_count/len(labels))
correct_count = 0
lines_to_write = []
with open("data/YAGO3-10/pred_instructions_llama_rel_raw13B.csv", "r", encoding= "utf-8") as f:
txt = f.read()
for (i, p) in enumerate(prompts):
begin_idx = txt.find(p)
end_idx = txt[begin_idx:].find("\n")
target_line = txt[begin_idx: begin_idx + end_idx]
#print(target_line)
lines_to_write.append(target_line + "\n")
res_begin_idx = target_line.find("|plays for.")
res = target_line[res_begin_idx + len("|plays for."):]
#print(res)
#print(label)
label = labels[i]
if res.find(label) != -1 and res.find("Please choose your answer from:") == -1:
correct_count += 1
#print(target_line, res, label)
with open("data/YAGO3-10/pred_instructions_llama_raw13B_rel_100.csv", "w", encoding= "utf-8") as f:
f.writelines(lines_to_write)
print("LLaMA-13B raw acc: ", correct_count, 1.0 * correct_count/len(labels))
selected_ids = []
lines_to_write = []
with open("data/YAGO3-10/test_instructions_glm_rel.json", "r", encoding= "utf-8") as f:
lines = f.readlines()
for p in prompts:
for (idx, line) in enumerate(lines):
if line.find(p) != -1:
selected_ids.append(idx)
lines_to_write.append(line)
break
print(selected_ids)
with open("data/YAGO3-10/test_instructions_glm_rel_100.json", "w", encoding= "utf-8") as f:
f.writelines(lines_to_write)
correct_count = 0
lines_to_write = []
#print(len(selected_ids), len(labels))
with open("data/YAGO3-10/r_generated_predictions.txt", "r", encoding= "utf-8") as f:
lines = f.readlines()
selected_lines = [lines[x] for x in selected_ids]
for (i, target_line) in enumerate(selected_lines):
target_line = target_line.strip()
#print(target_line)
lines_to_write.append(target_line + "\n")
res_begin_idx = target_line.find("\"predict\": \"")
res = target_line[res_begin_idx + len("\"predict\": \""): -2]
label = labels[i]
#print(res, label)
if res == label:
correct_count += 1
with open("data/YAGO3-10/r_generated_predictions_100.txt", "w", encoding= "utf-8") as f:
f.writelines(lines_to_write)
print("GLM acc: ", correct_count, 1.0 * correct_count/len(labels))
correct_count = 0
lines_to_write = []
#print(len(selected_ids), len(labels))
with open("data/YAGO3-10/raw_r_generated_predictions.txt", "r", encoding= "utf-8") as f:
lines = f.readlines()
selected_lines = [lines[x] for x in selected_ids]
for (i, target_line) in enumerate(selected_lines):
target_line = target_line.strip()
#print(target_line)
lines_to_write.append(target_line + "\n")
res_begin_idx = target_line.find("\"predict\": \"")
res = target_line[res_begin_idx + len("\"predict\": \""): -2]
label = labels[i]
if res.find(label) != -1:
correct_count += 1
with open("data/YAGO3-10/raw_r_generated_predictions_100.txt", "w", encoding= "utf-8") as f:
f.writelines(lines_to_write)
print("GLM raw acc: ", correct_count, 1.0 * correct_count/len(labels))