-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_neuron_effect_rumour.py
410 lines (339 loc) · 13.8 KB
/
compute_neuron_effect_rumour.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import os
import sys
import argparse
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from scipy.stats import pearsonr
parser = argparse.ArgumentParser(description="Run a set of neuron experiments analysis.")
parser.add_argument(
"-type",
type=str,
default="indirect",
help="""which type of intervention""",
)
parser.add_argument(
"-run",type=str,default='main',help="task selection [main, analysis]")
parser.add_argument(
"-num_classes",type=int, default=2, help='number of final prediction class')
parser.add_argument(
"-input_data_dir",type=str,default='/finetune_data/story_v4.pkl',help="The input data file path")
opt = parser.parse_args()
def gen_intervention_types():
return ['direct','indirect']
def gen_data_dicts(input_data_dir):
input_data = pd.read_pickle(input_data_dir)
input_rumour_dict = {}
input_is_turnaround_dict = {}
for index, row in input_data.iterrows():
input_rumour_dict[row['source_id']] = row['is_rumour']
source_turnaround_lst = row['turnaround_lst']
source_id = row['source_id']
for i, j in enumerate(source_turnaround_lst):
k = source_id + '_' +str(i)
input_is_turnaround_dict[k] = j
return input_rumour_dict, input_is_turnaround_dict
def compute_total_effect(row):
"""Compute the total effect based on the bias directionality."""
if row["base_c1_effect"] >= 1.0:
return row["alt1_effect"] / row["base_c1_effect"]
else:
return row["alt2_effect"] / row["base_c2_effect"]
def filtered_mean(df, column_name, profession_stereotypicality, model_name):
"""Get the mean effects after excluding strictly definitional professions."""
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_stereotypicality(vals):
return abs(profession_stereotypicality[vals]["definitional"])
df["profession"] = df["base_string"].apply(get_profession)
df["definitional"] = df["profession"].apply(get_stereotypicality)
return df[df["definitional"] < 0.75][column_name].mean()
def extract_source_id_loc(row):
combo = row['index']
combo_1 = combo.split('_')[0]
return combo_1
def extract_loc(row):
combo = row['index']
combo_2 = combo.split('_')[1]
return combo_2
def cal_base_effect(row):
base_eff = row['candidate1_base_prob']/row['candidate2_base_prob']
return base_eff
def cal_alt_effect(row):
alt_eff = row['candidate1_alt1_prob']/row['candidate2_alt1_prob']
return alt_eff
def cal_total_effect(row):
total_eff = row['alt_effect'] / row['base_effect']
return total_eff
from scipy.special import rel_entr, kl_div
from scipy.spatial.distance import jensenshannon
from scipy.stats import wasserstein_distance
def cal_total_effect_multi(row):
#base_prob = [row['candidate1_base_prob'],row['candidate2_base_prob'],row['candidate3_base_prob']]
#alt_prob = [row['candidate1_alt1_prob'],row['candidate2_alt1_prob'],row['candidate3_alt1_prob']]
#calculate (P || Q)
base_prob = row['base_prob']
alt_prob = row['alt_prob']
total_effect = sum(rel_entr(alt_prob, base_prob))
return total_effect
def cal_layer_effect_multi(row):
raw_intervention_res = row['intervention_res']
#raw_base_res = row['base_layer_res']
keys = list(row['intervention_res'].keys())
#print('keys ',keys)
final_res = {}
for k in keys:
final_res[k]= sum(rel_entr(raw_intervention_res[k][0],row['base_prob']))
return final_res
def cal_total_effect_multi_w(row):
base_prob = row['base_prob']
alt_prob = row['alt_prob']
total_effect = wasserstein_distance(alt_prob, base_prob)
return total_effect
def cal_total_effect_multi_j(row):
base_prob = row['base_prob']
alt_prob = row['alt_prob']
total_effect = jensenshannon(alt_prob, base_prob)
return total_effect
def cal_layer_effect_multi_w(row):
raw_intervention_res = row['intervention_res']
#raw_base_res = row['base_prob']
keys = list(row['intervention_res'].keys())
#print(keys)
final_res = {}
for k in keys:
#print('raw_intervention_res[k]',raw_intervention_res[k])
#print("row['base_prob'] ", row['base_prob'])
final_res[k]= wasserstein_distance(raw_intervention_res[k][0],row['base_prob'])
return final_res
def cal_layer_effect_multi_j(row):
raw_intervention_res = row['intervention_res']
raw_base_res = row['base_prob']
keys = list(row['intervention_res'].keys())
final_res = {}
for k in keys:
final_res[k]= jensenshannon(raw_intervention_res[k][0],row['base_prob'])
return final_res
def cal_total_effect_tv_norm(row):
base_prob = row['base_prob']
alt_prob = row['alt_prob']
total_tv = cal_tv_norm(alt_prob, base_prob)
return total_tv
def cal_layer_tv_norm(row):
intervention_res = row['intervention_res']
keys = list(row['intervention_res'].keys())
layer_tv_norm = {}
for k in keys:
layer_tv_norm[k] = cal_tv_norm(intervention_res[k][0],row['base_prob'])
return layer_tv_norm
def cal_total_effect_linf(row):
base_prob = row['base_prob']
alt_prob = row['alt_prob']
total_linf = cal_rel_linf_metric(alt_prob, base_prob)
return total_linf
def cal_layer_linf(row):
keys = list(row['intervention_res'].keys())
layer_linf = {}
for k in keys:
layer_linf[k] = cal_rel_linf_metric(row['intervention_res'][k][0],row['base_prob'])
return layer_linf
#cal for statistical distance or total variation norm
def cal_tv_norm(p,q):
P = np.array(p)
Q = np.array(q)
tv_norm = 0.5 * np.linalg.norm(P - Q, ord=1)
return tv_norm
def cal_rel_linf_metric(p,q):
P = np.array(p)
Q = np.array(q)
log_max_ratio = np.log(np.maximum(P/Q,Q/P))
rel_linf_exp = np.max(log_max_ratio)
rel_linf = np.exp(rel_linf_exp)
return rel_linf
def extract_layer_effect(row):
raw_layer_res = row['intervention_res']
keys = list(row['intervention_res'].keys())
final_res = {}
for k in keys:
final_res[k]= raw_layer_res[k][0][0] / raw_layer_res[k][0][1]
return final_res
'''
def extract_layer_effect_multi(row):
raw_layer_res = row['intervention_res']
keys = list(row['intervention_res'].keys())
final_res = {}
for k in keys:
final_res[k] =
return final_res
'''
def analysis():
direct_file = 'rumour_test_X_direct_roberta-base_res_analysis.pkl'
indirect_file = 'rumour_test_X_indirect_roberta-base_res_analysis.pkl'
direct_df = pd.read_pickle(direct_file)
indirect_df = pd.read_pickle(indirect_file)
direct_df_lite = direct_df[['source_id','intervention_loc','base_effect','alt_effect','layer_effect','total_effect']]
direct_df_lite.rename(columns={'base_effect': 'direct_base_effect', 'alt_effect': 'direct_alt_effect','layer_effect':'direct_layer_effect','total_effect':'direct_total_effect'}, inplace=True)
indirect_df_lite = indirect_df[['source_id','intervention_loc','base_effect','alt_effect','layer_effect','total_effect']]
indirect_df_lite.rename(columns={'base_effect': 'indirect_base_effect', 'alt_effect': 'indirect_alt_effect','layer_effect':'indirect_layer_effect','total_effect':'indirect_total_effect'}, inplace=True)
overall_effect_df_lite = pd.merge(direct_df_lite, indirect_df_lite, on=['source_id','intervention_loc'])
overall_effect_df_lite.to_pickle('rumour_test_overall_roberta-base.pkl')
def analysis_multi(direct_file, indirect_file):
direct_df = pd.read_pickle(direct_file)
indirect_df = pd.read_pickle(indirect_file)
def match_rumour_label(row, input_rumour_dict):
return input_rumour_dict[row['source_id']]
def match_turnaround_label(row, input_is_turnaround_dict):
return input_is_turnaround_dict[row['index']]
def decision_chaging(row):
base_decision = np.argmax(row['base_prob'])
alt_decision = np.argmax(row['alt_prob'])
res = 2
if base_decision == alt_decision:
res = 0
else:
res = 1
return res
def main_multi(input_data_dir):
intervention_types = gen_intervention_types()
input_rumour_dict, input_is_turnaround_dict = gen_data_dicts(input_data_dir)
#out_lst = []
for intervention_type in intervention_types:
data_file_name = 'rumour_test_X_'+intervention_type+'_roberta-base'
data_file = data_file_name + '.pkl'
input_data = pd.read_pickle(data_file)
input_data.reset_index(inplace=True)
input_data['source_id'] = input_data.apply(lambda row: extract_source_id_loc(row), axis=1)
input_data['intervention_loc'] = input_data.apply(lambda row: extract_loc(row), axis=1)
#input_data['base_effect'] = input_data.apply(lambda row: cal_base_effect_multi(row),axis=1)
#input_data['alt_effect'] = input_data.apply(lambda row: cal_alt_effect_multi(row),axis=1)
input_data['layer_effect'] = input_data.apply(lambda row: cal_layer_effect_multi(row),axis=1)
input_data['layer_effect_w'] = input_data.apply(lambda row: cal_layer_effect_multi_w(row),axis=1)
input_data['layer_effect_j'] = input_data.apply(lambda row: cal_layer_effect_multi_j(row),axis=1)
input_data['total_effect'] = input_data.apply(lambda row: cal_total_effect_multi(row),axis=1)
input_data['total_effect_w'] = input_data.apply(lambda row: cal_total_effect_multi_w(row),axis=1)
input_data['total_effect_j'] = input_data.apply(lambda row: cal_total_effect_multi_j(row),axis=1)
input_data['total_effect_tv'] = input_data.apply(lambda row: cal_total_effect_tv_norm(row),axis=1)
input_data['total_effect_linf'] = input_data.apply(lambda row: cal_total_effect_linf(row),axis=1)
input_data['layer_effect_tv'] = input_data.apply(lambda row: cal_layer_tv_norm(row),axis=1)
input_data['layer_effect_linf'] = input_data.apply(lambda row: cal_layer_linf(row),axis=1)
input_data['is_rumour'] = input_data.apply(lambda row: match_rumour_label(row, input_rumour_dict),axis=1)
input_data['is_turnaround'] = input_data.apply(lambda row: match_turnaround_label(row, input_is_turnaround_dict),axis=1)
input_data['decision_change'] = input_data.apply(lambda row: decision_chaging(row),axis=1)
#new_data['base_effect'] = new_data.apply(lambda row: cal_base_effect(row),axis=1)
#new_data['alt_effect'] = new_data.apply(lambda row: cal_alt_effect(row),axis=1)
#new_data['layer_effect'] = new_data.apply(lambda row: extract_layer_effect(row),axis=1)
input_data.to_pickle(data_file_name+'_multi_res_analysis.pkl')
#out_lst.append(data_file_name+'_multi_res_analysis.pkl')
def main(type='indirect'):
data_file_name = 'rumour_test_X_'+type+'_roberta-base'
data_file = data_file_name + '.pkl'
new_data = pd.read_pickle(data_file)
new_data.reset_index(inplace=True)
new_data['source_id'] = new_data.apply(lambda row: extract_source_id_loc(row), axis=1)
new_data['intervention_loc'] = new_data.apply(lambda row: extract_loc(row), axis=1)
new_data['base_effect'] = new_data.apply(lambda row: cal_base_effect(row),axis=1)
new_data['alt_effect'] = new_data.apply(lambda row: cal_alt_effect(row),axis=1)
new_data['layer_effect'] = new_data.apply(lambda row: extract_layer_effect(row),axis=1)
new_data['total_effect'] = new_data.apply(lambda row: cal_total_effect(row),axis=1)
new_data.to_pickle(data_file_name+'_res_analysis.pkl')
'''
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"))
]
means = []
he_means = []
she_means = []
# For correlations.
all_female_effects = []
for path in woman_files:
temp_df = pd.read_csv(path).groupby("base_string").agg("mean").reset_index()
temp_df["alt1_effect"] = (
temp_df["candidate1_alt1_prob"] / temp_df["candidate2_alt1_prob"]
)
temp_df["alt2_effect"] = (
temp_df["candidate2_alt2_prob"] / temp_df["candidate1_alt2_prob"]
)
temp_df["base_c1_effect"] = (
temp_df["candidate1_base_prob"] / temp_df["candidate2_base_prob"]
)
temp_df["base_c2_effect"] = (
temp_df["candidate2_base_prob"] / temp_df["candidate1_base_prob"]
)
temp_df["he_total_effect"] = temp_df["alt1_effect"] / temp_df["base_c1_effect"]
temp_df["she_total_effect"] = temp_df["alt2_effect"] / temp_df["base_c2_effect"]
temp_df["total_effect"] = temp_df.apply(compute_total_effect, axis=1)
mean_he_total = filtered_mean(
temp_df, "he_total_effect", profession_stereotypicality, model_name
)
mean_she_total = filtered_mean(
temp_df, "she_total_effect", profession_stereotypicality, model_name
)
mean_total = filtered_mean(
temp_df, "total_effect", profession_stereotypicality, model_name
)
he_means.append(mean_he_total)
she_means.append(mean_she_total)
means.append(mean_total)
all_female_effects.append(temp_df[["base_string", "she_total_effect"]])
print("The total effect of this model is {:.3f}".format(np.mean(means) - 1))
print(
"The total (male) effect of this model is {:.3f}".format(np.mean(he_means) - 1)
)
print(
"The total (female) effect of this model is {:.3f}".format(
np.mean(she_means) - 1
)
)
# Part 2: Get correlations.
all_female_total_effects = pd.concat(all_female_effects)
all_female_total_effects = all_female_total_effects.rename(
columns={"she_total_effect": "total_effect"}
)
x_vals = []
y_vals = []
labels = []
for index, row in all_female_total_effects.iterrows():
labels.append(row["base_string"])
y_vals.append(row["total_effect"])
x_vals.append(
profession_stereotypicality[
row["base_string"].split()[1] if not model_name.startswith('xlnet')
else row["base_string"].split('<eos>')[-1].split()[1]
]["total"]
)
profession_df = pd.DataFrame(
{"example": labels, "Bias": x_vals, "Total Effect": np.log(y_vals)}
)
plt.figure(figsize=(10, 3))
ax = sns.lineplot(
"Bias", "Total Effect", data=profession_df, markers=True, dashes=True
)
ax.set_yticks([0, 1, 2, 3, 4, 5, 6])
ax.set_yticklabels(["$e^0$", "$e^1$", "$e^2$", "$e^3$", "$e^4$", "$e^5$"])
sns.despine()
plt.savefig(os.path.join(folder_name, "neuron_profession_correlation.pdf"))
effect_corr = pearsonr(profession_df["Bias"], profession_df["Total Effect"])
print("================")
print(
"The correlation between bias value and (log) effect is {:.2f} (p={:.3f})".format(
effect_corr[0], effect_corr[1]
)
)
'''
if __name__ == "__main__":
if opt.run == 'main':
if opt.num_classes == 2:
main(opt.type)
elif opt.num_classes > 2:
#gen_data_dicts()
main_multi(opt.input_data_dir)
elif opt.run == 'analysis':
analysis()