forked from sebastianGehrmann/CausalMediationAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_neuron_split_total_effect.py
153 lines (132 loc) · 4.91 KB
/
compute_neuron_split_total_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
import os
import sys
import numpy as np
import pandas as pd
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]
# fnames[:5], paths[:5]
woman_files = [
f
for f in paths
if "woman_indirect" in f
if os.path.exists(f.replace("indirect", "direct"))
]
male_means_model = []
female_means_model = []
male_means_def = []
female_means_def = []
for path in woman_files:
df = pd.read_csv(path).groupby("base_string").agg("mean").reset_index()
def get_profession(s):
# Discard PADDING TEXT used in XLNet
if model_name.startswith('xlnet'): s = s.split('<eos>')[-1]
return s.split()[1]
# Set up filtering by stereotypicality
def get_definitionality(vals):
return abs(profession_stereotypicality[vals]["definitional"])
def get_stereotypicality(vals):
return profession_stereotypicality[vals]["total"]
df["profession"] = df["base_string"].apply(get_profession)
df["definitional"] = df["profession"].apply(get_definitionality)
df["stereotypicality"] = df["profession"].apply(get_stereotypicality)
"""
FILTERING
"""
# Remove examples that are too definitional
df = df[df["definitional"] < 0.75]
# Ignore outliers with < 1% for he or she
df = df[df["candidate1_base_prob"] > 0.01]
df = df[df["candidate2_base_prob"] > 0.01]
"""
TOTAL EFFECTS
"""
# Compute base_ratios for man (he/she) and woman (she/he)
df["man_he_she_effect"] = (
df["candidate1_alt1_prob"] / df["candidate2_alt1_prob"]
)
df["woman_she_he_effect"] = (
df["candidate2_alt2_prob"] / df["candidate1_alt2_prob"]
)
# Compute profession effect
df["base_he_she_effect"] = (
df["candidate1_base_prob"] / df["candidate2_base_prob"]
)
df["base_she_he_effect"] = (
df["candidate2_base_prob"] / df["candidate1_base_prob"]
)
# Compute both directions total effect
df["he_she_total_effect"] = df["man_he_she_effect"] / df["base_he_she_effect"]
df["she_he_total_effect"] = df["woman_she_he_effect"] / df["base_she_he_effect"]
"""
Compute the effects of:
male -> woman | she/he
female -> man | he/she
"""
# (1) Filter by model direction
female_mean_model = df[df["base_she_he_effect"] > 1.0][
"he_she_total_effect"
].values
female_means_model.extend(female_mean_model)
male_mean_model = df[df["base_he_she_effect"] > 1.0][
"she_he_total_effect"
].values
male_means_model.extend(male_mean_model)
# (2) Filter by stereotype
female_mean_def = df[df["stereotypicality"] < 0.0]["he_she_total_effect"].values
female_means_def.extend(female_mean_def)
male_mean_def = df[df["stereotypicality"] > 0.0]["she_he_total_effect"].values
male_means_def.extend(male_mean_def)
# print("The total effect of this model is {:.3f}".format(np.mean(means)-1))
print(
"The total (female profession (model) -> man) effect of this model is {:.3f}".format(
np.mean(male_means_model) - 1
)
)
print(
"The total (male profession (model) -> woman) effect of this model is {:.3f}".format(
np.mean(female_means_model) - 1
)
)
print(
"The combined effect is {:.3f}".format(
np.mean(female_means_model + male_means_model) - 1
)
)
print(
"The total (female profession (def) -> man) effect of this model is {:.3f}".format(
np.mean(male_means_def) - 1
)
)
print(
"The total (male profession (def) -> woman) effect of this model is {:.3f}".format(
np.mean(female_means_def) - 1
)
)
print(
"The combined effect is {:.3f}".format(
np.mean(female_means_def + male_means_def) - 1
)
)
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)