-
Notifications
You must be signed in to change notification settings - Fork 2
/
03_analyze_bias.py
168 lines (143 loc) · 5.09 KB
/
03_analyze_bias.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
from importlib.resources import path
import pandas as pd
import openai
import json
from tqdm import tqdm
import argparse
from prompts import prompt_templates, matching_heuristic
from collections import defaultdict
import numpy as np
import scipy.stats as st
parser = argparse.ArgumentParser()
parser.add_argument('--data-path', help='input-data')
parser.add_argument('--output-path', help='input-data')
parser.add_argument('--word', help='input-data')
parser.add_argument('--cot', action='store_true')
parser.add_argument('--flan-cot', action='store_true')
parser.add_argument('--prompt', help='input-data')
parser.add_argument('--model', help='input-data')
parser.set_defaults(cot=False)
args = parser.parse_args()
data = args.data_path
outputs = args.output_path
cot_mode = args.cot
flan_cot = args.flan_cot
word = args.word
model = args.model
def print_error_types(error_types, error_types_cot):
for k in error_types:
cot_error = round(sum(error_types_cot[k]) / len(error_types_cot[k]), 2) * 100
standard_error = round(sum(error_types[k]) / len(error_types[k]), 2) * 100
# print("Standard & " + str( sum(error_types[k]) / len(error_types[k])))
diff = round(cot_error - standard_error, 2)
out = f"""\multicolumn{{2}}{{c}}{{\\textbf{{{k}}}}} \\\\
\midrule
text-davinci-002 & {str(standard_error)[:2]} \\\\
\, + \\textsc{{CoT}} & \\ua{{{str(diff)[:2]}}} {str(cot_error)[:2]} \\\\
\midrule
"""
print(out)
print()
print(data)
print(outputs)
print(f"COT: {cot_mode}")
print(f"FLAN COT: {flan_cot}")
def get_error_types(df, d_normal, cot=False):
counts = 0
error_types = defaultdict(list)
misses = []
all_outs = []
specific_outputs = []
bias_types = []
non_stereo_arr = None
stereo_arr = None
for i in d_normal:
row = df.iloc[int(i)]
bias_types.append(row["bias_type"])
m = row["sent_more"]
l = row["sent_less"]
normal_out = d_normal[str(i)]
self_consistency_check = []
specific_output = []
if non_stereo_arr == None: non_stereo_arr = [0 for _ in range(len(normal_out))]
if stereo_arr == None: stereo_arr = [0 for _ in range(len(normal_out))]
for out_idx, output in enumerate(normal_out):
if cot:
output = normal_out[output][0]
counts += 1
all_opts = {"a": row["a"], "b": row["b"], "c": row["c"]}
more_letter = [k for k, v in all_opts.items() if m in v][0]
less_letter = [k for k, v in all_opts.items() if l in v][0]
none_letter = list(set(all_opts.keys()) - set([more_letter, less_letter]))[0]
opt_word = {
"a": "first",
"b": "second",
"c": "third"
}
if matching_heuristic(
output,
more_letter,
opt_word[more_letter],
m,
word,
flan_cot=flan_cot
):
stereo_arr[out_idx] += 1
error_types[row["bias_type"]].append(output)
self_consistency_check.append(False)
specific_output.append(1)
elif matching_heuristic(
output,
less_letter,
opt_word[less_letter],
l,
word,
flan_cot=flan_cot
):
non_stereo_arr[out_idx] += 1
self_consistency_check.append(False)
specific_output.append(-1)
else:
misses.append(output)
self_consistency_check.append(True)
specific_output.append(0)
all_outs.append(self_consistency_check)
specific_outputs.append(specific_output)
# confidence intervals
pcts = np.array(all_outs).sum(axis=0) / len(d_normal)
lower, upper = st.t.interval(0.95, len(pcts)-1, loc=np.mean(pcts), scale=st.sem(pcts))
mean = np.mean(pcts)
lower = mean if np.isnan(lower) else lower
upper = mean if np.isnan(upper) else upper
# self consistency
# avg and round is the same as majority vote
tst = np.sum(np.round(np.mean(all_outs, axis=1)))
self_cons_score = tst / len(d_normal)
return error_types, misses, counts, self_cons_score, (lower, mean, upper), specific_outputs, bias_types
with open(outputs) as f:
d_normal = json.load(f)
df = pd.read_csv(data)
error_types, misses, counts, self_cons, bounds, specific_outputs, bias_types = get_error_types(df, d_normal, cot_mode)
print(self_cons)
print(bounds)
d = None
try:
with open("./stats.json") as f:
d = json.load(f)
except:
d = []
d.append({
"prompt": args.prompt,
"word": word,
"data": data.split("/")[2],
"cot": cot_mode or flan_cot,
"model": model,
"self_consistency": self_cons,
"bounds": bounds,
"error_types": { k: len(error_types[k]) for k in error_types },
"flip": False,
"output_labels": specific_outputs,
"bias_types": bias_types
})
with open('./stats.json', 'w') as f:
json.dump(d, f, indent=4)