-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraph_utils.py
More file actions
472 lines (440 loc) · 20.7 KB
/
Copy pathgraph_utils.py
File metadata and controls
472 lines (440 loc) · 20.7 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import copy
import numpy as np
import plotly.graph_objects as go
import scipy
from utils import fill_text_blank
class InfGraph():
def __init__(self, examples, num_word, num_layer, mask_index, inf,
comb_greedy_val, comb_greedy_attn, heatmap, attn_heatmap,
indices_attn):
self.examples = examples
self.num_word = num_word
self.num_layer = num_layer
self.mask_index = mask_index
self.inf = inf
self.comb_greedy_val = comb_greedy_val
self.comb_greedy_attn = comb_greedy_attn
self.heatmap = heatmap
self.attn_heatmap = attn_heatmap
self.indices_attn = indices_attn
def plot_distribution(self, ind):
eind = ind[0]
traces = [
go.Bar(y=fill_text_blank(self.examples[eind].tokens, self.num_word),
x=self.inf[ind].mean(0),
name='input influence',
orientation='h'),
go.Bar(y=fill_text_blank(self.examples[eind].tokens, self.num_word),
x=self.comb_greedy_val[ind].mean(0),
name='word-level influence',
orientation='h'),
go.Bar(y=fill_text_blank(self.examples[eind].tokens, self.num_word),
x=self.comb_greedy_attn[ind].mean(0),
name='attention-head path influence',
orientation='h')
]
return traces
def hm2layer_connection(self,
ind,
scaler=3,
plot_attention_connection=True):
def mode(lst):
if len(lst) == 0:
return -2, 0
else:
return scipy.stats.mode(lst)[0][0], scipy.stats.mode(lst)[1][0]
layer_connection_pos = np.zeros(
(self.num_layer, self.num_word, self.num_word))
layer_connection_neg = np.zeros(
(self.num_layer, self.num_word, self.num_word))
attention_connection = [[[[]
for _ in range(self.num_word)]
for _ in range(self.num_word)]
for _ in range(self.num_layer)]
heatmap = copy.deepcopy(self.heatmap)
if self.num_layer == 6:
heatmap[:, self.mask_index, :, :] = 0
for i in ind:
tmp_idx = np.where(heatmap[i] == 1)
idx3 = tmp_idx[2].reshape(-1, self.num_layer)
idx4 = np.hstack((tmp_idx[0].reshape(-1,
self.num_layer)[:, 0][:, None],
idx3[:, :-1])).flatten()
for z1, z2, z3, z4 in zip(tmp_idx[1], idx4, tmp_idx[2], tmp_idx[0]):
layer_connection_pos[z1, z2, z3] += 1
for i in ind:
tmp_idx = np.where(heatmap[i] == -1)
idx3 = tmp_idx[2].reshape(-1, self.num_layer)
idx4 = np.hstack((tmp_idx[0].reshape(-1,
self.num_layer)[:, 0][:, None],
idx3[:, :-1])).flatten()
for z1, z2, z3, z4 in zip(tmp_idx[1], idx4, tmp_idx[2], tmp_idx[0]):
layer_connection_neg[z1, z2, z3] += -1
if plot_attention_connection:
for i in ind:
try:
tmp_idx = np.where(self.attn_heatmap[i] >= -1)
idx3 = tmp_idx[2].reshape(-1, self.num_layer)
idx4 = np.hstack(
(tmp_idx[0].reshape(-1, self.num_layer)[:, 0][:, None],
idx3[:, :-1])).flatten()
for z1, z2, z3, z4 in zip(tmp_idx[1], idx4, tmp_idx[2],
tmp_idx[0]):
attention_connection[z1][z2][z3].append(
int(self.indices_attn[i][z1][z4]))
except:
pass
attention_connection = np.array([[[mode(a)
for a in a1]
for a1 in a2]
for a2 in attention_connection])
else:
attention_connection = None
return layer_connection_pos / len(
ind) * scaler, layer_connection_neg / len(
ind) * scaler, attention_connection
def make_graph(self,
pos,
ind,
tokens=None,
scaler=3,
show_legend = True,
plot_attention_connection=True,
width_scaler=.08):
colors_bar = ['orange', 'purple', 'blue']
colors = ['green', 'red']
legend_threshold = 1
color = 'black'
dashes = [None, 'dash']
layer_connection, layer_connection_neg, attention_connection = self.hm2layer_connection(
ind, scaler, plot_attention_connection=plot_attention_connection)
if attention_connection is None:
attention_connection = np.zeros(
(self.num_layer, self.num_word, self.num_word), dtype=np.int32)
attention_connection_cnt = np.zeros(
(self.num_layer, self.num_word, self.num_word), dtype=np.int32)
else:
attention_connection_cnt = attention_connection[:, :, :, 1]
attention_connection = attention_connection[:, :, :, 0]
layers = np.reshape(np.arange(self.num_word * (self.num_layer + 1)),
(self.num_layer + 1, self.num_word))
layers = layers[:, ::-1]
edges_x = []
edges_y = []
node_x = []
node_y = []
edge_width = []
traces = []
attention_node_x = []
attention_node_y = []
attention_node_n = []
legend_flag = [False, False, False, False]
legend_flag_track = [True, True, True, True]
if not plot_attention_connection:
legend_names = [
'pos, skip ', 'attn baseline', 'neg, skip', 'neg, attn'
]
else:
legend_names = ['pos, skip', 'pos, attn', 'neg, skip', 'neg, attn']
for k in range(len(layer_connection)):
connection = layer_connection[k]
for i in range(connection.shape[0]):
for j in range(connection.shape[1]):
if connection[i][j] != 0:
val = connection[i][j]
if val > 0:
color = colors[0]
else:
color = colors[1]
source_node = layers[k][i]
target_node = layers[k + 1][j]
source_pos = pos[source_node]
target_pos = pos[target_node]
if source_pos[0] != target_pos[0]:
if attention_connection[k, i, j] == -1:
# if val < 0:
dash = dashes[1]
legend_idx = 0
else:
dash = dashes[0]
legend_idx = 1
if attention_connection_cnt[k, i,
j] > len(ind) / 4:
attention_node_x.append(
(source_pos[0] + target_pos[0]) / 2)
attention_node_y.append(
(source_pos[1] + target_pos[1]) / 2)
attention_node_n.append(
attention_connection[k, i, j] + 1)
if np.abs(val) > legend_threshold:
legend_flag[legend_idx] = True
else:
legend_flag[legend_idx] = False
edges_x = [source_pos[0], target_pos[0], ""]
edges_y = [source_pos[1], target_pos[1], ""]
# width = np.sqrt(np.abs(val)) *
# sqrt_scaler
width = np.abs(val)
edge_trace = go.Scatter(
x=edges_x,
y=edges_y,
line=dict(width=width, color=color, dash=dash),
showlegend=legend_flag[legend_idx] &
legend_flag_track[legend_idx],
name=legend_names[legend_idx],
hoverinfo='text',
text="(" + str(source_node) + "," +
str(target_node) + ")",
opacity=0.8,
mode='lines')
if legend_flag[legend_idx]:
legend_flag_track[legend_idx] = False
traces.append(edge_trace)
if layer_connection_neg is not None:
color = colors[1]
for k in range(len(layer_connection_neg)):
connection = layer_connection_neg[k]
for i in range(connection.shape[0]):
for j in range(connection.shape[1]):
if connection[i][j] != 0:
if attention_connection[k, i, j] == -1:
dash = dashes[1]
legend_idx = 2
else:
dash = dashes[0]
legend_idx = 3
val = connection[i][j]
source_node = layers[k][i]
target_node = layers[k + 1][j]
source_pos = pos[source_node]
target_pos = pos[target_node]
if source_pos[0] != target_pos[0]:
if attention_connection_cnt[k, i, j] > len(
ind) / 3 and attention_connection[
k, i, j] != -1:
if not ((source_pos[0] + target_pos[0]) /
2 in attention_node_x and
(source_pos[1] + target_pos[1]) /
2 in attention_node_y):
attention_node_x.append(
(source_pos[0] + target_pos[0]) / 2)
attention_node_y.append(
(source_pos[1] + target_pos[1]) / 2)
attention_node_n.append(
attention_connection[k, i, j] + 1)
if np.abs(val) > legend_threshold:
legend_flag[legend_idx] = True
else:
legend_flag[legend_idx] = False
edges_x = [source_pos[0], target_pos[0], ""]
edges_y = [
source_pos[1] - 0.05, target_pos[1] - 0.05,
""
]
width = np.abs(val)
edge_trace = go.Scatter(
x=edges_x,
y=edges_y,
showlegend=legend_flag[legend_idx] &
legend_flag_track[legend_idx],
line=dict(width=width,
color=color,
dash=dash),
name=legend_names[legend_idx],
hoverinfo='text',
text="(" + str(source_node) + "," +
str(target_node) + ")",
opacity=0.8,
mode='lines')
if legend_flag[legend_idx]:
legend_flag_track[legend_idx] = False
traces.append(edge_trace)
node_x = []
node_y = []
for node in pos:
x, y = pos[node][0], pos[node][1]
node_x.append(x)
node_y.append(y)
if tokens is None:
tokens = self.examples[ind][0].tokens
node_trace_with_text = go.Scatter(x=node_x[:self.num_word],
y=node_y[:self.num_word],
showlegend=False,
mode='markers+text',
text=tokens,
textposition='middle left',
cliponaxis=False,
textfont=dict(size=23),
hoverinfo='text',
marker=dict(showscale=False,
symbol='square',
opacity=0.7,
reversescale=True,
color='white',
size=20,
line=dict(color='black',
width=8),
line_width=2))
node_trace_mask = go.Scatter(
x=[node_x[-self.num_word + self.mask_index]],
y=[node_y[-self.num_word + self.mask_index]],
mode='markers',
textposition='middle left',
hoverinfo='text',
showlegend=False,
marker=dict(showscale=False,
symbol='circle',
opacity=0.7,
reversescale=True,
color='white',
size=20,
line=dict(color='black', width=8),
line_width=2))
node_trace_last = go.Scatter(x=node_x[-self.num_word:],
y=node_y[-self.num_word:],
mode='markers',
textposition='middle left',
hoverinfo='text',
showlegend=False,
marker=dict(showscale=False,
symbol='circle',
opacity=0.3,
reversescale=True,
color='white',
size=10,
line=dict(color='black',
width=8),
line_width=2))
node_trace = go.Scatter(x=node_x[self.num_word:-self.num_word],
y=node_y[self.num_word:-self.num_word],
mode='markers',
text=layers[1:].flatten(),
textposition='middle left',
hoverinfo='text',
showlegend=False,
marker=dict(showscale=False,
symbol='circle',
opacity=0.7,
reversescale=True,
color='white',
size=10,
line=dict(color='black', width=8),
line_width=2))
node_trace_attn = go.Scatter(
x=attention_node_x,
y=attention_node_y,
mode='text',
text=attention_node_n,
textfont=dict(size=20),
# textposition='bottom center',
hoverinfo='text',
showlegend=False,
marker=dict(showscale=False,
symbol='circle',
opacity=1,
reversescale=True,
color='white',
size=24,
line=dict(color='black', width=8),
line_width=2))
dis_scaler = 1
base = 0.0
emb_legend = '$\\large \mathcal{I}(\mathbf{x}_i, \pi_{baseline,i})$' if not plot_attention_connection else '$\\large \mathcal{I}(\mathbf{x}_i, \pi^e_i)$'
dist_traces = []
dist_traces.append(
go.Bar(
y=node_y[:self.num_word],
x=np.flip(self.inf[ind].mean(0) * dis_scaler),
width=np.ones_like(x) * width_scaler,
base=base,
name="$\\large g(\mathbf{x}_i)$",
# name='g(x)',
orientation='h',
marker=dict(color=colors_bar[0], opacity=0.7),
showlegend=plot_attention_connection,
xaxis="x2",
yaxis="y2"))
dist_traces.append(
go.Bar(y=node_y[:self.num_word],
x=np.flip(self.comb_greedy_val[ind].mean(0) * dis_scaler),
width=np.ones_like(x) * width_scaler,
base=base,
name=emb_legend,
marker=dict(color=colors_bar[1], opacity=0.7),
orientation='h',
xaxis="x2",
yaxis="y2"))
dist_traces.append(
go.Bar(y=node_y[:self.num_word],
x=np.flip(self.comb_greedy_attn[ind].mean(0) * dis_scaler),
width=np.ones_like(x) * width_scaler,
base=base,
name='$\\large \mathcal{I}(\mathbf{x}_i, \pi^a_i)$',
marker=dict(color=colors_bar[2], opacity=0.7),
orientation='h',
showlegend=plot_attention_connection,
xaxis="x2",
yaxis="y2"))
data = traces + [
node_trace_with_text, node_trace, node_trace_attn, node_trace_last,
node_trace_mask
] + dist_traces
if not plot_attention_connection:
tickvals = [0., 0.02]
else:
tickvals = np.arange(-2, 2, 1)
layout = go.Layout(
template="simple_white",
barmode='group',
bargroupgap=0.0,
bargap=0.4,
legend=dict(
yanchor="top",
y=0.99,
xanchor="right",
x=0.9,
font=dict(size=20, color="black"),
),
showlegend=show_legend,
margin=dict(b=40, l=5, r=5, t=10),
xaxis=dict(domain=[0.25, .9],
automargin=True,
ticks='',
showline=False,
showticklabels=False,
showgrid=False,
zeroline=False),
yaxis=dict(autorange='reversed',
automargin=True,
ticks='',
showline=False,
showticklabels=False,
showgrid=False,
zeroline=False),
xaxis2=dict(
domain=[0, 0.2],
automargin=False,
ticks='',
range=[-4, 4],
# range=[0, 1.5],
# tickvals=np.arange(0, 1.1, 0.5),
tickvals=tickvals,
# tickvals=[0, 0.5, 1, 1.5],
tickfont=dict(size=20),
showline=True,
showticklabels=True,
showgrid=False,
zeroline=True),
yaxis2=dict(anchor="x2",
showline=False,
showticklabels=False,
ticks='',
showgrid=False,
zeroline=False),
hovermode='closest',
width=750,
height=500)
#large (950,500)
#like (750,120)
fig = go.Figure(data=data, layout=layout)
return fig