-
Notifications
You must be signed in to change notification settings - Fork 17
/
compute_and_save_neuron_agg_effect.py
194 lines (168 loc) · 6.16 KB
/
compute_and_save_neuron_agg_effect.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
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Compute the aggregate effects for each individual neuron.
Save the effects as $model_neuron_effects.csv.
Usage:
python compute_and_save_neuron_agg_effect.py $result_file_path $model_name
"""
import os
import sys
import pandas as pd
def analyze_effect_results(results_df, effect, word, alt, savefig=None):
# calculate odds.
if alt == "man":
odds_base = (
results_df["candidate1_base_prob"] / results_df["candidate2_base_prob"]
)
odds_intervention = (
results_df["candidate1_prob"] / results_df["candidate2_prob"]
)
else:
odds_base = (
results_df["candidate2_base_prob"] / results_df["candidate1_base_prob"]
)
odds_intervention = (
results_df["candidate2_prob"] / results_df["candidate1_prob"]
)
odds_ratio = odds_intervention / odds_base
results_df["odds_ratio"] = odds_ratio
if word == "all":
# average over words
results_df = results_df.groupby(["layer", "neuron"], as_index=False).mean()
else:
# choose one word
results_df = results_df[results_df["word"] == word]
results_df = results_df.pivot("neuron", "layer", "odds_ratio")
def get_all_effects(fname, direction="woman"):
"""
Give fname from a direct effect file
"""
# Step 1: Load results for current folder and gender
print(fname)
indirect_result_df = pd.read_csv(fname)
analyze_effect_results(
results_df=indirect_result_df, effect="indirect", word="all", alt=direction
)
fname = fname.replace("_indirect_", "_direct_")
direct_result_df = pd.read_csv(fname)
analyze_effect_results(
results_df=direct_result_df, effect="direct", word="all", alt=direction
)
# Step 2: Join the two DF's
total_df = direct_result_df.join(
indirect_result_df, lsuffix="_direct", rsuffix="_indirect"
)[
[
"base_string_direct",
"layer_direct",
"neuron_direct",
"odds_ratio_indirect",
"odds_ratio_direct",
]
]
total_df["total_causal_effect"] = (
total_df["odds_ratio_indirect"] + total_df["odds_ratio_direct"] - 1
)
return total_df
def main(folder_name="results/20191114_neuron_intervention/", model_name="distilgpt2"):
profession_stereotypicality = {}
with open("experiment_data/professions.json") as f:
for l in f:
for p in eval(l):
profession_stereotypicality[p[0]] = {
"stereotypicality": p[2],
"definitional": p[1],
"total": p[2] + p[1],
"max": max([p[2], p[1]], key=abs),
}
fnames = [
f
for f in os.listdir(folder_name)
if "_" + model_name + ".csv" in f and f.endswith("csv")
]
paths = [os.path.join(folder_name, f) for f in fnames]
woman_files = [
f
for f in paths
if "woman_indirect" in f
if os.path.exists(f.replace("indirect", "direct"))
]
woman_dfs = []
for path in woman_files:
woman_dfs.append(get_all_effects(path))
woman_df = pd.concat(woman_dfs)
man_files = [
f
for f in paths
if "_man_indirect" in f
if os.path.exists(f.replace("indirect", "direct"))
]
man_dfs = []
for path in man_files:
man_dfs.append(get_all_effects(path, "man"))
man_df = pd.concat(man_dfs)
# Compute Extra Info
def get_profession(s):
# Discard PADDING TEXT used in XLNet
if model_name.startswith('xlnet'): s = s.split('<eos>')[-1]
return s.split()[1]
def get_template(s):
# Discard PADDING TEXT used in XLNet
if model_name.startswith('xlnet'): s = s.split('<eos>')[-1]
initial_string = s.split()
initial_string[1] = "_"
return " ".join(initial_string)
man_df["profession"] = man_df["base_string_direct"].apply(get_profession)
man_df["template"] = man_df["base_string_direct"].apply(get_template)
woman_df["profession"] = woman_df["base_string_direct"].apply(get_profession)
woman_df["template"] = woman_df["base_string_direct"].apply(get_template)
def get_stereotypicality(vals):
return profession_stereotypicality[vals]["total"]
def get_definitionality(vals):
return abs(profession_stereotypicality[vals]["definitional"])
man_df["stereotypicality"] = man_df["profession"].apply(get_stereotypicality)
woman_df["stereotypicality"] = woman_df["profession"].apply(get_stereotypicality)
# Exclude very definitional examples.
man_df["definitional"] = man_df["profession"].apply(get_definitionality)
woman_df["definitional"] = woman_df["profession"].apply(get_definitionality)
man_df = man_df[man_df["definitional"] > 0.75]
woman_df = woman_df[woman_df["definitional"] > 0.75]
# Merge effect based on directionality.
overall_df = pd.concat(
[
man_df[man_df["stereotypicality"] < 0],
woman_df[woman_df["stereotypicality"] >= 0],
]
)
# Save some RAM, next step is _expensive_!
del man_df
del woman_df
overall_df["neuron"] = (
overall_df["layer_direct"].map(str) + "-" + overall_df["neuron_direct"].map(str)
)
neuron_effect_df = (
overall_df.groupby("neuron")
.agg(
{
"layer_direct": ["mean"],
"neuron_direct": ["mean"],
"odds_ratio_indirect": ["mean", "std"],
"odds_ratio_direct": ["mean", "std"],
"total_causal_effect": ["mean", "std"],
}
)
.reset_index()
)
neuron_effect_df.columns = [
"_".join(col).strip() for col in neuron_effect_df.columns.values
]
path_name = os.path.join(folder_name, model_name + "_neuron_effects.csv")
neuron_effect_df.to_csv(path_name)
print("Effect csv saved to {}".format(path_name))
if __name__ == "__main__":
if len(sys.argv) != 3:
print("USAGE: python ", sys.argv[0], "<folder_name> <model_name>")
# e.g., results/20191114...
folder_name = sys.argv[1]
# gpt2, gpt2-medium, gpt2-large
model_name = sys.argv[2]
main(folder_name, model_name)