-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhuman_FB13_eval.py
181 lines (148 loc) · 6.55 KB
/
human_FB13_eval.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
prompts = []
labels = []
with open("data/FB13/FB13_test_human.tsv", "r") as f:
lines = f.readlines()
for line in lines:
tmp = line.strip().split("\t")
prompts.append(tmp[1])
labels.append(tmp[0])
correct_count = 0
lines_to_write = []
with open("data/FB13/pred_instructions_llama.csv", "r") 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("?,")
res = target_line[res_begin_idx + 3: -1]
#print(res)
label = labels[i]
if (res.find("Yes,") != -1 or res.find(" yes") != -1) and label == "1":
correct_count += 1
#print(line, res, label)
elif (res.find("No") != -1 or res.find("not") != -1 or res.find("n't") != -1 or res.find("no") != -1) and label == "-1":
correct_count += 1
with open("data/FB13/pred_instructions_llama_100.csv", "w") 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/FB13/pred_instructions_llama13b.csv", "r") 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("?,")
res = target_line[res_begin_idx + 3: -1]
#print(res)
label = labels[i]
if (res.find("Yes,") != -1 or res.find(" yes") != -1) and label == "1":
correct_count += 1
#print(line, res, label)
elif (res.find("No") != -1 or res.find("not") != -1 or res.find("n't") != -1 or res.find("no") != -1) and label == "-1":
correct_count += 1
with open("data/FB13/pred_instructions_llama13b_100.csv", "w") 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/FB13/pred_instructions_llama_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("?,")
res = target_line[res_begin_idx + 3: -1]
#print(res)
label = labels[i]
if (res.find("Yes,") != -1 or res.find(" yes") != -1) and label == "1":
correct_count += 1
#print(line, res, label)
elif (res.find("No") != -1 or res.find("not") != -1 or res.find("n't") != -1 or res.find("no") != -1) and label == "-1":
correct_count += 1
with open("data/FB13/pred_instructions_llama_raw_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/FB13/pred_instructions_llama_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("?,")
res = target_line[res_begin_idx + 3: -1]
#print(res)
label = labels[i]
if (res.find("Yes,") != -1 or res.find(" yes") != -1) and label == "1":
correct_count += 1
#print(line, res, label)
elif (res.find("No") != -1 or res.find("not") != -1 or res.find("n't") != -1 or res.find("no") != -1) and label == "-1":
correct_count += 1
with open("data/FB13/pred_instructions_llama_raw13b_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/FB13/test_instructions_glm.json", "r") 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/FB13/test_instructions_glm_100.json", "w", encoding= "utf-8") as f:
f.writelines(lines_to_write)
correct_count = 0
lines_to_write = []
with open("data/FB13/generated_predictions.txt", "r") 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]
#print(res)
label = labels[i]
if (res.find("Yes,") != -1 or res.find(" yes") != -1) and label == "1":
correct_count += 1
#print(line, res, label)
elif (res.find("No") != -1 or res.find("not") != -1 or res.find("n't") != -1 or res.find("no") != -1) and label == "-1":
correct_count += 1
with open("data/FB13/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 = []
with open("data/FB13/fb13_raw_generated_predictions.txt", "r", encoding= "utf-8") as f:
lines = f.readlines()
for (i, target_line) in enumerate(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]
#print(res)
label = labels[i]
if (res.find("Yes,") != -1 or res.find(" yes") != -1) and label == "1":
correct_count += 1
#print(line, res, label)
elif (res.find("No") != -1 or res.find("not") != -1 or res.find("n't") != -1 or res.find("no") != -1) and label == "-1":
correct_count += 1
print("GLM raw acc: ", correct_count, 1.0 * correct_count/len(labels))