-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrun_bert_sv.py
360 lines (268 loc) · 12.9 KB
/
run_bert_sv.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
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
from attention_graph_util import *
import seaborn as sns
import itertools
import matplotlib as mpl
import networkx as nx
import os
from util import constants
from absl import app
from absl import flags
import pandas as pd
from util.models import MODELS
from util.tasks import TASKS
from attention_graph_util import *
from util.config_util import get_task_params
from notebooks.notebook_utils import *
from util import inflect
from tqdm import tqdm
from scipy.stats import spearmanr
import math
rc={'font.size': 10, 'axes.labelsize': 10, 'legend.fontsize': 10.0,
'axes.titlesize': 32, 'xtick.labelsize': 20, 'ytick.labelsize': 16}
plt.rcParams.update(**rc)
mpl.rcParams['axes.linewidth'] = .5 #set the value globally
import torch
from transformers import *
from transformers import BertConfig, BertForMaskedLM, BertTokenizer
from transformers import DistilBertTokenizer, DistilBertModel
def offset_convertor(encoded_input_task, task_offset, task_encoder, tokenizer):
string_part1 = task_encoder.decode(encoded_input_task[:task_offset])
tokens_part1 = tokenizer.tokenize(string_part1)
return len(tokens_part1)
def get_raw_att_relevance(full_att_mat, input_tokens, layer=-1, output_index=0):
raw_rel = full_att_mat[layer].sum(axis=0)[output_index]/full_att_mat[layer].sum(axis=0)[output_index].sum()
return raw_rel
def get_joint_relevance(full_att_mat, input_tokens, layer=-1, output_index=0):
att_sum_heads = full_att_mat.sum(axis=1) / full_att_mat.shape[1]
joint_attentions = compute_joint_attention(att_sum_heads, add_residual=True)
relevance_attentions = joint_attentions[layer][output_index]
return relevance_attentions
def get_flow_relevance(full_att_mat, input_tokens, layer, output_index):
input_tokens = input_tokens
res_att_mat = full_att_mat.sum(axis=1)/full_att_mat.shape[1]
res_att_mat = res_att_mat + np.eye(res_att_mat.shape[1])[None,...]
res_att_mat = res_att_mat / res_att_mat.sum(axis=-1)[...,None]
res_adj_mat, res_labels_to_index = get_adjmat(mat=res_att_mat, input_tokens=input_tokens)
A = res_adj_mat
res_G=nx.from_numpy_matrix(A, create_using=nx.DiGraph())
for i in np.arange(A.shape[0]):
for j in np.arange(A.shape[1]):
nx.set_edge_attributes(res_G, {(i,j): A[i,j]}, 'capacity')
output_nodes = ['L'+str(layer+1)+'_'+str(output_index)]
input_nodes = []
for key in res_labels_to_index:
if res_labels_to_index[key] < full_att_mat.shape[-1]:
input_nodes.append(key)
flow_values = compute_node_flow(res_G, res_labels_to_index, input_nodes=input_nodes, output_nodes=output_nodes, length=full_att_mat.shape[-1])
n_layers = full_att_mat.shape[0]
length = full_att_mat.shape[-1]
final_layer_attention = flow_values[(layer+1)*length:, layer*length:(layer+1)*length]
relevance_attention_flow = final_layer_attention[output_index]
return relevance_attention_flow
task_name = 'word_sv_agreement_lm'
task_params = get_task_params(batch_size=1)
task = TASKS[task_name](task_params, data_dir='../InDist/data')
cl_token = task.sentence_encoder().encode(constants.bos)
task_tokenizer = task.sentence_encoder()._tokenizer
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertForMaskedLM.from_pretrained('distilbert-base-uncased',
output_hidden_states=True,
output_attentions=True)
all_examples_x = []
all_examples_vp = []
all_examples_y = []
all_examples_attentions = []
all_examples_blankout_relevance = []
all_examples_grads = []
all_examples_inputgrads = []
n_batches = 1000
all_examples_accuracies = []
infl_eng = inflect.engine()
verb_infl, noun_infl = gen_inflect_from_vocab(infl_eng, '../InDist/notebooks/wiki.vocab')
test_data = task.databuilder.as_dataset(split='validation', batch_size=1)
for examples in tqdm(test_data):
sentence = task.sentence_encoder().decode(examples['sentence'][0])
verb_position = examples['verb_position'][0].numpy()+1 #+1 because of adding cls.
verb_position = offset_convertor(examples['sentence'][0], verb_position, task.sentence_encoder(), tokenizer)
sentence = ['cls']+tokenizer.tokenize(sentence)+['sep']
all_examples_vp.append(verb_position)
sentence[verb_position] = tokenizer.mask_token
tf_input_ids = tokenizer.encode(sentence)
input_ids = torch.tensor([tf_input_ids])
s_shape = input_ids.shape
batch_size, length = s_shape[0], s_shape[1]
actual_verb = examples['verb'][0].numpy().decode("utf-8")
inflected_verb = verb_infl[actual_verb]
actual_verb_index = tokenizer.encode(tokenizer.tokenize(actual_verb))[1]
inflected_verb_index = tokenizer.encode(tokenizer.tokenize(inflected_verb))[1]
all_examples_x.append(input_ids)
embeded_inputs = torch.autograd.Variable(model.distilbert.embeddings(input_ids), requires_grad=True)
predictions = model(inputs_embeds=embeded_inputs)
logits = predictions[0][0]
probs = torch.nn.Softmax(dim=-1)(logits)
actual_verb_score = probs[verb_position][actual_verb_index]
inflected_verb_score = probs[verb_position][inflected_verb_index]
main_diff_score = actual_verb_score - inflected_verb_score
all_examples_accuracies.append(main_diff_score > 0)
main_diff_score.backward()
grads = embeded_inputs.grad
grad_scores = abs(np.sum(grads.detach().numpy(), axis=-1))
input_grad_scores = abs(np.sum((grads * embeded_inputs).detach().numpy(), axis=-1))
all_examples_grads.append(grad_scores)
all_examples_inputgrads.append(input_grad_scores)
hidden_states, attentions = predictions[-2:]
_attentions = [att.detach().numpy() for att in attentions]
attentions_mat = np.asarray(_attentions)[:,0]
all_examples_attentions.append(attentions_mat)
# Repeating examples and replacing one token at a time with unk
batch_size = 1
max_len = input_ids.shape[1]
# Repeat each example 'max_len' times
x = input_ids
extended_x = np.reshape(np.tile(x[:,None,...], (1, max_len, 1)),(-1,x.shape[-1]))
# Create unk sequences and unk mask
unktoken = tokenizer.encode([tokenizer.mask_token])[1]
unks = unktoken * np.eye(max_len)
unks = np.tile(unks, (batch_size, 1))
unk_mask = (unktoken - unks)/unktoken
# Replace one token in each repeatition with unk
extended_x = extended_x * unk_mask + unks
# Get the new output
extended_predictions = model(torch.tensor(extended_x, dtype=torch.int64))
extended_logits = extended_predictions[0]
extended_probs = torch.nn.Softmax(dim=-1)(extended_logits)
extended_correct_probs = extended_probs[:,verb_position,actual_verb_index]
extended_wrong_probs = extended_probs[:,verb_position,inflected_verb_index]
extended_diff_scores = extended_correct_probs - extended_wrong_probs
# Save the difference in the probability predicted for the correct class
diffs = abs(main_diff_score - extended_diff_scores)
all_examples_blankout_relevance.append(diffs.detach())
n_batches -= 1
if n_batches <= 0:
break
print("compute raw relevance scores ...")
all_examples_raw_relevance = {}
for l in np.arange(0,6):
all_examples_raw_relevance[l] = []
for i in tqdm(np.arange(len(all_examples_x))):
tokens = tokenizer.decode(all_examples_x[i][0].numpy())
vp = all_examples_vp[i]
length = len(tokens)
attention_relevance = get_raw_att_relevance(all_examples_attentions[i], tokens, layer=l, output_index=vp)
all_examples_raw_relevance[l].append(np.asarray(attention_relevance))
print("compute joint relevance scores ...")
all_examples_joint_relevance = {}
for l in np.arange(0,6):
all_examples_joint_relevance[l] = []
for i in tqdm(np.arange(len(all_examples_x))):
tokens = tokenizer.decode(all_examples_x[i][0].numpy())
vp = all_examples_vp[i]
length = len(tokens)
attention_relevance = get_joint_relevance(all_examples_attentions[i], tokens, layer=l, output_index=vp)
all_examples_joint_relevance[l].append(np.asarray(attention_relevance))
print("compute flow relevance scores ...")
all_examples_flow_relevance = {}
for l in np.arange(0,6):
all_examples_flow_relevance[l] = []
for i in tqdm(np.arange(len(all_examples_x))):
tokens = tokenizer.decode(all_examples_x[i][0].numpy())
vp = all_examples_vp[i]
length = len(tokens)
attention_relevance = get_flow_relevance(all_examples_attentions[i], tokens, layer=l, output_index=vp)
all_examples_flow_relevance[l].append(np.asarray(attention_relevance))
raw_sps_blank = []
raw_sps_grad = []
raw_sps_inputgrad = []
joint_sps_blank = []
joint_sps_grad = []
joint_sps_inputgrad = []
flow_sps_blank = []
flow_sps_grad = []
flow_sps_inputgrad = []
for l in np.arange(0,6):
print("###############Layer ",l, "#############")
print('raw blankout')
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_raw_relevance[l][i],all_examples_blankout_relevance[i].numpy())
if not math.isnan(sp[0]):
raw_sps_blank.append(sp[0])
else:
raw_sps_blank.append(0)
print(np.mean(raw_sps_blank), np.std(raw_sps_blank))
print('raw inputgrad')
print(all_examples_raw_relevance[l][0].shape, all_examples_inputgrads[0][0].shape)
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_raw_relevance[l][i],all_examples_inputgrads[i][0])
if not math.isnan(sp[0]):
raw_sps_inputgrad.append(sp[0])
else:
raw_sps_inputgrad.append(0)
print(np.mean(raw_sps_inputgrad), np.std(raw_sps_inputgrad))
print('raw grad')
print(all_examples_raw_relevance[l][0].shape, all_examples_grads[0][0].shape)
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_raw_relevance[l][i],all_examples_grads[i][0])
if not math.isnan(sp[0]):
raw_sps_grad.append(sp[0])
else:
raw_sps_grad.append(0)
print(np.mean(raw_sps_grad), np.std(raw_sps_grad))
print('joint blankout')
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_joint_relevance[l][i],all_examples_blankout_relevance[i].numpy())
if not math.isnan(sp[0]):
joint_sps_blank.append(sp[0])
else:
joint_sps_blank.append(0)
print(np.mean(joint_sps_blank), np.std(joint_sps_blank))
print('joint grad')
print(all_examples_joint_relevance[l][0].shape, all_examples_grads[0][0].shape)
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_joint_relevance[l][i],all_examples_grads[i][0])
if not math.isnan(sp[0]):
joint_sps_grad.append(sp[0])
else:
joint_sps_grad.append(0)
print(np.mean(joint_sps_grad), np.std(joint_sps_grad))
print('joint inputgrad')
print(all_examples_joint_relevance[l][0].shape, all_examples_inputgrads[0][0].shape)
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_joint_relevance[l][i],all_examples_inputgrads[i][0])
if not math.isnan(sp[0]):
joint_sps_inputgrad.append(sp[0])
else:
joint_sps_inputgrad.append(0)
print(np.mean(joint_sps_inputgrad), np.std(joint_sps_inputgrad))
print('flow')
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_flow_relevance[l][i],all_examples_blankout_relevance[i].numpy())
if not math.isnan(sp[0]):
flow_sps_blank.append(sp[0])
else:
flow_sps_blank.append(0)
print(np.mean(flow_sps_blank), np.std(flow_sps_blank))
print('flow grad')
print(all_examples_joint_relevance[l][0].shape, all_examples_grads[0][0].shape)
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_flow_relevance[l][i],all_examples_grads[i][0])
if not math.isnan(sp[0]):
flow_sps_grad.append(sp[0])
else:
flow_sps_grad.append(0)
print(np.mean(flow_sps_grad), np.std(flow_sps_grad))
print('flow inputgrad')
print(all_examples_joint_relevance[l][0].shape, all_examples_inputgrads[0][0].shape)
for i in np.arange(len(all_examples_x)):
sp = spearmanr(all_examples_flow_relevance[l][i],all_examples_inputgrads[i][0])
if not math.isnan(sp[0]):
flow_sps_inputgrad.append(sp[0])
else:
flow_sps_inputgrad.append(0)
print(np.mean(flow_sps_inputgrad), np.std(flow_sps_inputgrad))
np.save('all_examples_flow_relevance', all_examples_flow_relevance)
np.save('all_examples_joint_relevance', all_examples_joint_relevance)
np.save('all_examples_blankout_relevance', all_examples_blankout_relevance)
np.save('all_examples_grads', all_examples_grads)