-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathattention_path.py
More file actions
326 lines (278 loc) · 11.4 KB
/
Copy pathattention_path.py
File metadata and controls
326 lines (278 loc) · 11.4 KB
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
from tqdm import trange
import numpy as np
import tensorflow as tf
def greedy_path(attentions,
examples,
reverse=False,
aggr_over_head='max',
add_residual=False,
normalize=False,
return_idx=False):
## Aggregate attention socres over multiple heads
if aggr_over_head == 'max':
attentions = np.max(attentions, axis=2)
elif aggr_over_head == 'mean':
attentions = np.mean(attentions, axis=2)
else:
attentions = aggr_over_head(attentions)
if add_residual:
residual_att = np.eye(attentions.shape[2])[None, None, :]
aug_att_mat = attentions + residual_att
if normalize:
aug_att_mat = aug_att_mat / np.sum(
aug_att_mat, axis=-1, keepdims=True)
else:
aug_att_mat = attentions
## (batch, seq_len, attend_in, attend_out)
N, L, K, K = attentions.shape
attentions = np.log(attentions)
all_example_path_scores = []
if not reverse:
for n in trange(N):
per_attention, per_instance = attentions[n], examples[n]
target_idx = per_instance.target_idx
sentence = per_instance.tokens
all_path_scores = []
for k in range(len(sentence)):
path = [sentence[k]]
path_score = 0
current_idx = k
for l in range(1, L - 1):
i = np.argmax(per_attention[l, current_idx, :])
path_score += per_attention[l, current_idx, i]
if return_idx:
path.append(i)
else:
path.append(sentence[i])
current_idx = i
if return_idx:
path.append(target_idx)
else:
path.append(sentence[target_idx])
path_score += per_attention[-1, current_idx, target_idx]
all_path_scores.append([path, path_score])
all_example_path_scores.append(all_path_scores)
else:
for n in trange(N):
per_attention, per_instance = attentions[n], examples[n]
target_idx = per_instance.target_idx
sentence = per_instance.tokens
all_path_scores = []
for k in range(len(sentence)):
path_score = 0
path = [target_idx]
current_idx = target_idx
for l in range(L - 1, -1, -1):
if l == 0:
i = k
else:
i = np.argmax(per_attention[l, :, current_idx])
path_score += per_attention[l, i, current_idx]
current_idx = i
if return_idx:
path.append(current_idx)
else:
path.append(sentence[current_idx])
all_path_scores.append([path[::-1] + [np.exp(path_score)]])
all_example_path_scores.append(all_path_scores)
return all_example_path_scores
def beam_search_path(attentions,
examples,
aggr_over_head='max',
excludings=['[CLS]', '[SEP]', None],
beam_width=10,
return_all_beams=False):
if aggr_over_head == 'max':
attentions = np.max(attentions, axis=2)
elif aggr_over_head == 'mean':
attentions = np.mean(attentions, axis=2)
else:
attentions = aggr_over_head(attentions)
N, L, K, K = attentions.shape
all_example_path_scores = []
for n in trange(N):
per_attention, per_instance = attentions[n], examples[n]
target_idx = per_instance.target_idx
sentence = per_instance.tokens
all_path_scores = []
for k in range(len(sentence)):
if sentence[k] not in excludings:
## First layer
queue = [[k]]
current_idx = k
q_score = np.array([0])
## Hidden layers
for l in range(1, L - 1):
new_queue = []
new_q_score = []
for prev_path, q_s in zip(queue, q_score):
for new_node in range(K):
new_path = list(prev_path) + [new_node]
new_queue.append(new_path)
new_q_score.append(q_s +
per_attention[l, prev_path[-1],
new_node])
new_q_score = np.array(new_q_score)
new_queue = np.asarray(new_queue)
new_path_id = np.argsort(new_q_score)[::-1][:beam_width]
queue = new_queue[new_path_id]
q_score = new_q_score[new_path_id]
queue = list(queue)
## Last layer
new_queue = []
new_q_score = []
for prev_path, q_s in zip(queue, q_score):
new_queue.append(list(prev_path) + [target_idx])
new_q_score.append(q_s + per_attention[-1, prev_path[-1],
target_idx])
new_q_score = np.array(new_q_score)
if not return_all_beams:
best_path_id = np.argmax(new_q_score)
best_path = new_queue[best_path_id]
best_q_score = new_q_score[best_path_id]
all_path_scores.append([best_path, best_q_score])
else:
best_path_idx = np.argsort(new_q_score)[::-1][:beam_width]
new_queue = np.asarray(new_queue)
best_paths = new_queue[best_path_idx]
best_q_scores = new_q_score[best_path_idx]
all_path_scores.append([best_paths, best_q_scores])
all_example_path_scores.append(all_path_scores)
return all_example_path_scores
def dp_path(attentions,
examples,
aggr_over_head='mean',
add_residual=True,
normalize=True):
if aggr_over_head == 'max':
attentions = np.max(attentions, axis=2)
elif aggr_over_head == 'mean':
attentions = np.mean(attentions, axis=2)
else:
attentions = aggr_over_head(attentions)
if add_residual:
residual_att = np.eye(attentions.shape[2])[None, None, :]
aug_att_mat = attentions + residual_att
if normalize:
aug_att_mat = aug_att_mat / np.sum(
aug_att_mat, axis=-1, keepdims=True)
else:
aug_att_mat = attentions
attentions = aug_att_mat
N, L, K, K = attentions.shape
def _find_path(att, target):
log_prob = np.log(att) # L, K, K
all_paths = []
all_paths_score = []
for s in range(K):
dp = np.zeros((L + 1, K))
# initialize with edges between s to nodes at first layer
dp[1] = log_prob[0, s]
for l in range(2, dp.shape[0]):
if l != dp.shape[0] - 1:
for k in range(K):
# for the k-th node in l-th layer
# dp[l, k] = max_{i \in K} dp[l-1][i]+log_prob[l-1][i][k]
dp[l, k] = np.max(dp[l - 1] + log_prob[l - 1, :, k])
else:
dp[l,
target] = np.max(dp[l - 1] + log_prob[l - 1, :, target])
all_paths_score.append(dp[-1, target])
c_node = target
path = [c_node]
for l in range(dp.shape[0] - 1, 0, -1):
for k in range(K):
if dp[l,
c_node] == dp[l - 1, k] + log_prob[l - 1, k, c_node]:
path.append(k)
c_node = path[-1]
path = path[::-1]
all_paths.append(path)
all_paths = np.asarray(all_paths)
all_paths_score = np.asarray(all_paths_score)
all_paths_score = np.exp(all_paths_score)
return all_paths_score, all_paths
all_paths = []
all_scores = []
for att, e in zip(attentions, examples):
try:
target = e.target_idx
except:
target = 0
paths_score, paths = _find_path(att, target)
all_paths.append(paths)
all_scores.append(paths_score)
# (K, N) (K, N, L+1)
return np.asarray(all_scores), np.asarray(all_paths), np.asarray(attentions)
def attention_rollout(attentions,
examples,
aggr_over_head='mean',
add_residual=True,
normalize=True):
if aggr_over_head == 'max':
attentions = np.max(attentions, axis=2)
elif aggr_over_head == 'mean':
attentions = np.mean(attentions, axis=2)
else:
attentions = aggr_over_head(attentions)
if add_residual:
residual_att = np.eye(attentions.shape[2])[None, None, :]
aug_att_mat = attentions + residual_att
if normalize:
aug_att_mat = aug_att_mat / np.sum(
aug_att_mat, axis=-1, keepdims=True)
else:
aug_att_mat = attentions
for i in range(1, aug_att_mat.shape[1]):
aug_att_mat[:, i] = tf.matmul(tf.constant(aug_att_mat[:, i]),
tf.constant(aug_att_mat[:,
i - 1])).numpy()
target_idx = np.array(
[examples[n].target_idx for n in range(len(examples))])
scores = aug_att_mat[np.arange(aug_att_mat.shape[0]), :, target_idx]
attn_rollout = aug_att_mat[:, -1]
return attn_rollout, scores
def attention_flow(attentions,
examples,
aggr_over_head='mean',
add_residual=True,
normalize=True):
if aggr_over_head == 'max':
attentions = np.max(attentions, axis=2)
elif aggr_over_head == 'mean':
attentions = np.mean(attentions, axis=2)
else:
attentions = aggr_over_head(attentions)
if add_residual:
residual_att = np.eye(attentions.shape[2])[None, None, :]
aug_att_mat = attentions + residual_att
if normalize:
aug_att_mat = aug_att_mat / np.sum(
aug_att_mat, axis=-1, keepdims=True)
else:
aug_att_mat = attentions
def _af(att_mat, tokens):
adj_mat, labels_to_index = agu.get_adjmat(mat=att_mat,
input_tokens=tokens)
G = agu.draw_attention_graph(adj_mat,
labels_to_index,
n_layers=att_mat.shape[0],
length=att_mat.shape[-1])
output_nodes = []
input_nodes = []
for key in labels_to_index:
if 'L6' in key:
output_nodes.append(key)
if labels_to_index[key] < att_mat.shape[-1]:
input_nodes.append(key)
flow_values = agu.compute_flows(G,
labels_to_index,
input_nodes,
length=att_mat.shape[-1])
return flow_values
AF = []
for i in trange(attentions.shape[0]):
att = attentions[i]
e = examples[i]
AF.append(_af(att, e.tokens))
return AF