forked from ZixuanKe/LifelongSentClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattribution_utils.py
201 lines (184 loc) · 8.94 KB
/
attribution_utils.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
import numpy as np
import statistics
import torch
def aggregate_local_to_global(attributions,predictions,targets,tokens):
print("Aggregating attributions from local to global")
print('*'*10)
all_tokens = []
for example in tokens:
all_tokens += example
all_tokens = list(set(all_tokens))
print("Total unique features:", len(all_tokens))
mxtx_tp = np.where((predictions==0) & (predictions==targets))[0]
mxtx_tn = np.where((predictions==1) & (predictions==targets))[0]
attr_pos_mxtx = np.clip(attributions, a_min=0, a_max=None)
attr_neg_mxtx = np.clip(attributions, a_min=None, a_max=0)
check1 = 90
check2 = 10
# Collect attributions of each feature across samples
# true positives
attr_mxtx_tp = attr_pos_mxtx[mxtx_tp]
mxtx_pos_token_global_attr = {}
for train_idx,attr in zip(mxtx_tp,attr_mxtx_tp):
train_tokens = tokens[train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = 1 if attr_val>check_attr1 else 0
if token in mxtx_pos_token_global_attr:
mxtx_pos_token_global_attr[token].append(attr_val)
else:
mxtx_pos_token_global_attr[token] = [attr_val]
attr_mxtx_tp = attr_neg_mxtx[mxtx_tp]
mxtx_neg_token_global_attr = {}
for train_idx,attr in zip(mxtx_tp,attr_mxtx_tp):
train_tokens = tokens[train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = -1 if attr_val<check_attr2 else 0
if token in mxtx_neg_token_global_attr:
mxtx_neg_token_global_attr[token].append(attr_val*-1)
else:
mxtx_neg_token_global_attr[token] = [attr_val*-1]
# true negatives
attr_mxtx_tn = attr_neg_mxtx[mxtx_tn]
for train_idx,attr in zip(mxtx_tn,attr_mxtx_tn):
train_tokens = tokens[train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = -1 if attr_val<check_attr2 else 0
if token in mxtx_pos_token_global_attr:
mxtx_pos_token_global_attr[token].append(attr_val*-1)
else:
mxtx_pos_token_global_attr[token] = [attr_val*-1]
attr_mxtx_tn = attr_pos_mxtx[mxtx_tn]
for train_idx,attr in zip(mxtx_tn,attr_mxtx_tn):
train_tokens = tokens[train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = 1 if attr_val>check_attr1 else 0
if token in mxtx_neg_token_global_attr:
mxtx_neg_token_global_attr[token].append(attr_val)
else:
mxtx_neg_token_global_attr[token] = [attr_val]
# false positives
attr_mxtx_fp = attr_pos_mxtx[mxtx_fp]
for train_idx,attr in zip(mxtx_fp,attr_mxtx_fp):
train_tokens = tokens[str(j)][train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = 1 if attr_val>check_attr1 else 0
if token in mxtx_pos_token_global_attr:
mxtx_pos_token_global_attr[token].append(attr_val)
else:
mxtx_pos_token_global_attr[token] = [attr_val]
attr_mxtx_fp = attr_neg_mxtx[mxtx_fp]
for train_idx,attr in zip(mxtx_fp,attr_mxtx_fp):
train_tokens = tokens[str(j)][train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = -1 if attr_val<check_attr2 else 0
if token in mxtx_neg_token_global_attr:
mxtx_neg_token_global_attr[token].append(attr_val*-1)
else:
mxtx_neg_token_global_attr[token] = [attr_val*-1]
# false negatives
attr_mxtx_fn = attr_neg_mxtx[mxtx_fn]
for train_idx,attr in zip(mxtx_fn,attr_mxtx_fn):
train_tokens = tokens[str(j)][train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = -1 if attr_val<check_attr2 else 0
if token in mxtx_pos_token_global_attr:
mxtx_pos_token_global_attr[token].append(attr_val*-1)
else:
mxtx_pos_token_global_attr[token] = [attr_val*-1]
attr_mxtx_fn = attr_pos_mxtx[mxtx_fn]
for train_idx,attr in zip(mxtx_fn,attr_mxtx_fn):
train_tokens = tokens[str(j)][train_idx]
token_attr = attr[1:len(train_tokens)+1]
check_attr1=np.percentile(token_attr,check1)
check_attr2=np.percentile(token_attr,check2)
for token,attr_val in zip(train_tokens,token_attr):
attr_val = 1 if attr_val>check_attr1 else 0
if token in mxtx_neg_token_global_attr:
mxtx_neg_token_global_attr[token].append(attr_val)
else:
mxtx_neg_token_global_attr[token] = [attr_val]
# Aggregate attributions of each feature to global attributions
mxtx_pos_mean_global_attr_dict = {}
pos_cnt=0
mxtx_neg_mean_global_attr_dict = {}
neg_cnt=0
for token in mxtx_pos_token_global_attr.keys():
token_mean = statistics.mean(mxtx_pos_token_global_attr[token])
mxtx_pos_mean_global_attr_dict[token] = token_mean
if token_mean>0:
pos_cnt += 1
for token in mxtx_neg_token_global_attr.keys():
token_mean = statistics.mean(mxtx_neg_token_global_attr[token])
mxtx_neg_mean_global_attr_dict[token] = token_mean
if token_mean>0:
neg_cnt += 1
print("Top 10% features for class 0:",pos_cnt)
print("Top 10% features for class 1:",neg_cnt)
global_attr = {}
global_attr['pos'] = mxtx_pos_mean_global_attr_dict
global_attr['neg'] = mxtx_neg_mean_global_attr_dict
return global_attr
def get_batch_targets(attributions,classes,batch_tokens,global_attr):
targets = []
# Note: Assumes the function is called only for samples with true predictions, using pred class attributions
# Or for all samples, using actual class attributions
for attr,cls,example in zip(attributions,classes,batch_tokens): # loop through each example
if cls==0:
global_attr_pred = global_attr['pos']
global_attr_opp = global_attr['neg']
else:
global_attr_pred = global_attr['neg']
global_attr_opp = global_attr['pos']
example_target = []
for token in example: # loop through each token and set an attribution target
if token in global_attr_pred:
# Regularize if the feature had high attribution previously towards predicted class(to prevent forgetting)
if global_attr_pred[token]>=0.5:
target_attr = 1
# Else, regularize in opposite direction if it had high attribution towards opposite class previously (to prevent forgetting)
elif token in global_attr_opp:
if global_attr_opp[token]>=0.5:
target_attr = -1
# Otherwise, do not regularize (to allow learning)
else:
target_attr = attr
else:
target_attr = attr
elif token in global_attr_pred:
# Else, regularize in opposite direction if it had high attribution towards opposite class previously (to prevent forgetting)
if global_attr_pred[token]>=0.5:
target_attr = 1
# Otherwise, do not regularize (to allow learning)
else:
target_attr = attr
else:
# Do not regularize if it's a new feature not seen in previous tasks (to allow learning)
# Note: Since we do not calculate global attr for cls, sep and pad tokens, attributions for those tokens will not be regularized
# Does this work as intended during backprop?
target_attr = attr
example_target.append(target_attr)
targets.append(example_target)
targets = torch.Tensor(targets)
assert targets.shape==attributions.shape
return targets