-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtutorial_helpers.py
More file actions
465 lines (391 loc) · 15.5 KB
/
tutorial_helpers.py
File metadata and controls
465 lines (391 loc) · 15.5 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
# %matplotlib inline
import ipywidgets
import warnings; warnings.filterwarnings("ignore")
from contextlib import contextmanager,redirect_stderr,redirect_stdout
from os import devnull
@contextmanager
def suppress_stdout_stderr():
"""A context manager that redirects stdout and stderr to devnull"""
with open(devnull, 'w') as fnull:
with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
yield (err, out)
import sytorch as st
import torch
import numpy as np
torch.set_grad_enabled(False)
import matplotlib as mpl
from matplotlib import pyplot as plt
def get_dnn():
with st.no_grad():
dnn = st.nn.Sequential(
st.nn.Linear(1, 3),
st.nn.ReLU(),
st.nn.Linear(3, 1)
)
dnn[0].weight[:] = torch.as_tensor([[-1., 1., 0.5]]).T
dnn[0].bias[:] = torch.as_tensor([0., -2., 0.])
dnn[2].weight[:] = torch.as_tensor([[0.5, -0.5, 1.]])
dnn[2].bias[:] = torch.as_tensor([-0.5])
return dnn
xmin, xmax = -2., 4.
ymin, ymax = -0.7, 1.
def find_linear_regions(dnn, xlim, num=10000, step=1e-2):
# X = torch.linspace(*xlim, num)[...,None]
X = torch.arange(*xlim, step=step)[...,None]
AP = dnn.activation_pattern(X)[1]
bounderies = list(X[:-1][(AP[1:] != AP[:-1]).any(-1)].flatten())
if len(bounderies) == 0:
return (xlim, )
X = X.flatten().tolist()
if bounderies[0] != X[0]:
bounderies.insert(0, X[0])
if bounderies[-1] == X[-2]:
bounderies[-1] = X[-1]
else:
bounderies.append(X[-1])
return list(zip(bounderies[:-1], bounderies[1:]))
def plot(dnn, xlim=(-2., 4.), ylim=(), ax=None, **kwargs):
X = torch.linspace(*xlim, 100)[...,None]
# X = torch.arange(*xlim, step)[...,None]
Y = dnn(X)
if ax is None: ax = plt.gca()
ax.plot(X, Y, **kwargs)
def plot_regions(dnn, xlim=(-2., 4.), ylim=(), ax=None, **kwargs):
regions = find_linear_regions(dnn, xlim)
for xlim in regions:
plot(dnn, xlim=xlim, ax=ax)
def plot_polytope(dnn, x1, x2, **kwargs):
if x1 > x2: x1, x2 = x2, x1
regions = find_linear_regions(dnn, (x1, x2))
for x1, x2 in regions:
points = torch.Tensor([x1, x2])[...,None]
plot_segment(dnn, points, **kwargs)
def plot_points(N, points, lb, ub, ax=None, alpha=1., label='endpoints', **kwargs):
if ax is None: ax=plt.gca()
with torch.no_grad(), st.no_symbolic():
outputs = N(points)
sat_mask = (lb - 1e-4 <= outputs) * (outputs <= ub + 1e-4)
unsat_mask = ~sat_mask
# print(outputs[sat_mask])
# print(outputs[unsat_mask])
ax.scatter(points[ sat_mask], outputs[ sat_mask], color='b', label=f"sat. {label}", **kwargs)
ax.scatter(points[unsat_mask], outputs[unsat_mask], color='r', label=f"unsat. {label}", **kwargs)
def plot_segment(N, points, lb, ub, ax=None, alpha=1., **kwargs):
if ax is None: ax=plt.gca()
with torch.no_grad(), st.no_symbolic():
x1, x2 = points.reshape(-1).tolist()
X = torch.arange(x1, x2, 1e-2)[...,None]
Y = N(X)
sat_mask = (lb - 1e-4 <= Y) * (Y <= ub + 1e-4)
unsat_mask = ~sat_mask
if not sat_mask.any():
ax.plot(points, N(points), 'r', linewidth=3)
return
z1, z2 = X[sat_mask][[0, -1]]
ax.plot(points, N(points), 'b', linewidth=3)
if x1 <= z1:
pts = torch.Tensor([x1, z1])[...,None]
ax.plot(pts, N(pts), 'r', linewidth=3)
if z2 <= x2:
pts = torch.Tensor([z2, x2])[...,None]
ax.plot(pts, N(pts), 'r', linewidth=3)
def draw_neural_net(ax, N, N0=None, ap=None, left=0.1, right=0.9, bottom=0., top=1.):
if N0 is None:
N0 = N
layer_sizes = [N[0].weight.shape[1]] + [l.bias.shape[0] for l in N if hasattr(l, 'bias')]
n_layers = len(layer_sizes)
v_spacing = (top - bottom)/float(max(layer_sizes))
h_spacing = (right - left)/float(len(layer_sizes) - 1)
# Nodes
for n, layer_size in enumerate(layer_sizes):
layer_top = v_spacing*(layer_size - 1)/2. + (top + bottom)/2.
for m in range(layer_size):
circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/4.,
color='w', ec='k', zorder=4)
ax.add_artist(circle)
x, y = circle.get_center()
if ap is not None and n % 2 == 1:
node_ap = ap[n//2]
ax.text(
x+0.03, y-0.07, 'On' if node_ap[m] else 'Off',
ha='left',
zorder=10,
bbox=dict(
facecolor='white',
edgecolor='red' if node_ap[m] else 'blue',
alpha=1.,
pad=2.0),)
if n > 0:
b0 = N0[n*2-2].bias[m]
b1 = N[n*2-2].bias[m]
if b0 == b1:
ax.text(x, y+.1, rf"{b1:.1f}",
c='k',
ha='center',
fontfamily='monospace',
fontweight=1000)
else:
ax.text(x, y+.1, rf"{b1:.1f}",
bbox=dict(facecolor='lime', edgecolor='lime', alpha=.5, pad=2.0),
c='k',
ha='center',
fontfamily='monospace',
fontweight=1000)
# Edges
for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
for m in range(layer_size_a):
for o in range(layer_size_b):
line = plt.Line2D([n*h_spacing + left, (n + 1)*h_spacing + left],
[layer_top_a - m*v_spacing, layer_top_b - o*v_spacing], c='k')
ax.add_artist(line)
x, y = line.get_xydata().mean(0)
w0 = N0[n*2].weight[o,m]
w1 = N[n*2].weight[o,m]
if w0 == w1:
ax.text(
x, y+.03,
rf"{w1:.1f}",
c='k',
ha='center',
fontfamily='monospace',
fontweight=1000)
else:
ax.text(
x, y+.03,
rf"{w1:.1f}",
bbox=dict(facecolor='lime', edgecolor='lime', alpha=.5, pad=2.0),
c='k',
ha='center',
fontfamily='monospace',
fontweight=1000)
def interactive_pointwise_repair(
pointwise_repair,
x1=-1.5,
lb=-0.1, ub=0.1,
ref=-1.,
r0=True, r1=True, r2=True,
ap_mode='ref',
run=True):
r0 = r0 == 'On'
r1 = r1 == 'On'
r2 = r2 == 'On'
fig, ((ax0n, ax0), (ax1n, ax1)) = plt.subplots(2, 2, figsize=(9, 8))
N0 = get_dnn()
N = get_dnn()
points = torch.tensor([x1])[:,None]
ref_points = torch.tensor([ref])[:,None]
ax0.set_xlim(xmin, xmax)
ax0.set_ylim(ymin, ymax)
plot_regions(N, ax=ax0)
plot_points(N, points, lb=lb, ub=ub, ax=ax0, label='points')
if ap_mode != 'Manual':
ref_x = ref
ref_y = N(ref_points[0]).item()
ax0.scatter(ref_x, ref_y, marker='o', facecolors='none', edgecolors='k', label="ref point")
ref_ap = N.activation_pattern(ref_points[0:1])[1]
ax0.annotate(
f"Activation pattern\nfrom ref. point\n{tuple('On' if b else 'Off' for b in ref_ap[0])}",
xy=(ref_x, ref_y),
xytext=(ref_x+.8, ref_y+.4),
ha='center',
arrowprops=dict(arrowstyle="->,head_length=0.8,head_width=0.4"))
ax0.hlines([lb, ub], xmin=xmin-1., xmax=xmax+1., alpha=.6, color='k', linestyles='dashed', label="bounds")
# ax0.legend(loc='upper right', bbox_to_anchor=(3.1, 1.))
ax0.legend(loc='upper right', bbox_to_anchor=(1.8, 1.))
ax0t = ax0.twinx()
ax0t.set_ylim(ymin, ymax)
ax0t.set_yticks([lb, ub])
ax0n.axis('off')
draw_neural_net(ax0n, N, N0, ap=ref_ap if ap_mode != 'Manual' else N.activation_pattern(points)[1])
if not run:
return
with suppress_stdout_stderr():
if ap_mode == 'Manual':
ap = [[], np.asanyarray([[r0, r1, r2]]), []]
else:
ap = N.activation_pattern(ref_points)
N = pointwise_repair(N=N,
x1=x1,
lb=lb, ub=ub,
ap=ap)
# solver = st.GurobiSolver().verbose_(False)
# N.to(solver).requires_symbolic_weight_and_bias().repair()
# if ap_mode == 'Manual':
# ap = [[], np.asanyarray([[r0, r1, r2], [r0, r1, r2]]), []]
# else:
# ap = N.activation_pattern(ref_points)
# sy = N(points, pattern=ap)
# param_deltas = N.parameter_deltas()
# succeed = solver.solve(
# lb <= sy, sy <= ub,
# minimize = param_deltas.norm_ub('linf+l1_normalized')
# )
if N is None:
print("Infeasible!")
return
# plot_regions(N)
ax1.set_xlim(xmin, xmax)
ax1.set_ylim(ymin, ymax)
plot_regions(N, ax=ax1)
plot_points(N, points, lb=lb, ub=ub, ax=ax1, label='points')
ax1.hlines([lb, ub], xmin=xmin-1., xmax=xmax+1., alpha=.6, color='k', linestyles='dashed', label="bounds")
ax1t = ax1.twinx()
ax1t.set_ylim(ymin, ymax)
ax1t.set_yticks([lb, ub])
plt.subplots_adjust(wspace=.35)
fig.suptitle(rf"$\mathcal{{N}}({x1:.1f}) \in [{lb:.1f},{ub:.1f}]$",
size=20, weight=1000)
ax1n.axis('off')
draw_neural_net(ax1n, N, N0, ap=ap[1])
def interactive_polytope_repair(
polytope_repair,
x1=-1.5, x2=-0.5,
lb=-0.1, ub=0.1,
ref=-1.,
r0=True, r1=True, r2=True,
ap_mode='ref',
run=True):
r0 = r0 == 'On'
r1 = r1 == 'On'
r2 = r2 == 'On'
fig, ((ax0n, ax0), (ax1n, ax1)) = plt.subplots(2, 2, figsize=(9, 8))
N0 = get_dnn()
N = get_dnn()
points = torch.tensor([x1, x2])[:,None]
ref_points = torch.tensor([ref, ref])[:,None]
ax0.set_xlim(xmin, xmax)
ax0.set_ylim(ymin, ymax)
plot_regions(N, ax=ax0)
plot_points(N, points, lb=lb, ub=ub, ax=ax0)
plot_polytope(N, x1, x2, lb=lb, ub=ub, ax=ax0)
ax0.plot(points-10, N(points), 'r', linewidth=3, label='unsat. segment')
ax0.plot(points-10, N(points), 'b', linewidth=3, label='sat. segment')
if ap_mode != 'Manual':
ref_x = ref
ref_y = N(ref_points[0]).item()
ax0.scatter(ref_x, ref_y, marker='o', facecolors='none', edgecolors='k', label="ref point")
ref_ap = N.activation_pattern(ref_points[0:1])[1]
ax0.annotate(
f"Activation pattern\nfrom ref. point\n{tuple('On' if b else 'Off' for b in ref_ap[0])}",
xy=(ref_x, ref_y),
xytext=(ref_x+.8, ref_y+.4),
ha='center',
arrowprops=dict(arrowstyle="->,head_length=0.8,head_width=0.4"))
ax0.hlines([lb, ub], xmin=xmin-1., xmax=xmax+1., alpha=.6, color='k', linestyles='dashed', label="bounds")
# ax0.legend(loc='upper right', bbox_to_anchor=(3.1, 1.))
ax0.legend(loc='upper right', bbox_to_anchor=(1.8, 1.))
ax0t = ax0.twinx()
ax0t.set_ylim(ymin, ymax)
ax0t.set_yticks([lb, ub])
ax0n.axis('off')
draw_neural_net(ax0n, N, N0, ap=ref_ap if ap_mode != 'Manual' else None)
if not run:
return
with suppress_stdout_stderr():
if ap_mode == 'Manual':
ap = [[], np.asanyarray([[r0, r1, r2], [r0, r1, r2]]), []]
else:
ap = N.activation_pattern(ref_points)
N = polytope_repair(N=N,
x1=x1, x2=x2,
lb=lb, ub=ub,
ap=ap)
# solver = st.GurobiSolver().verbose_(False)
# N.to(solver).requires_symbolic_weight_and_bias().repair()
# if ap_mode == 'Manual':
# ap = [[], np.asanyarray([[r0, r1, r2], [r0, r1, r2]]), []]
# else:
# ap = N.activation_pattern(ref_points)
# sy = N(points, pattern=ap)
# param_deltas = N.parameter_deltas()
# succeed = solver.solve(
# lb <= sy, sy <= ub,
# minimize = param_deltas.norm_ub('linf+l1_normalized')
# )
if N is None:
print("Infeasible!")
return
# plot_regions(N)
ax1.set_xlim(xmin, xmax)
ax1.set_ylim(ymin, ymax)
plot_regions(N, ax=ax1)
plot_points(N, points, lb=lb, ub=ub, ax=ax1)
plot_segment(N, points, lb=lb, ub=ub, ax=ax1)
ax1.hlines([lb, ub], xmin=xmin-1., xmax=xmax+1., alpha=.6, color='k', linestyles='dashed', label="bounds")
ax1t = ax1.twinx()
ax1t.set_ylim(ymin, ymax)
ax1t.set_yticks([lb, ub])
plt.subplots_adjust(wspace=.35)
fig.suptitle(rf"$\forall x\in[{x1:.1f},{x2:.1f}].\mathcal{{N}}(x) \in [{lb:.1f},{ub:.1f}]$",
size=20, weight=1000)
ax1n.axis('off')
draw_neural_net(ax1n, N, N0, ap=ap[1])
x1 = ipywidgets.FloatSlider(value=-1.5, min=xmin, max=xmax, description='x1')
x2 = ipywidgets.FloatSlider(value=-0.5, min=xmin, max=xmax, description='x2')
ref= ipywidgets.FloatSlider(value= -1., min=xmin, max=xmax, description='Ref. Point')
ap_mode = ipywidgets.ToggleButtons(
options=['Manual', 'Reference Point'],
description='Use activation pattern from',
disabled=False,
button_style='info',
)
r0 = ipywidgets.ToggleButtons(
options=['On', 'Off'],
description='Manual activation pattern:',
disabled=False,
button_style='',
)
r1 = ipywidgets.ToggleButtons(
options=['On', 'Off'],
disabled=False,
button_style='',
)
r2 = ipywidgets.ToggleButtons(
options=['On', 'Off'],
disabled=False,
button_style='',
)
lb = ipywidgets.FloatSlider(value=-0.1, min=ymin, max=ymax, description='lb')
ub = ipywidgets.FloatSlider(value= 0.1, min=ymin, max=ymax, description='ub')
def interact_polytope_repair_with(polytope_repair):
r0.value='On'
r1.value='Off'
r2.value='Off'
ui = ipywidgets.HBox([
ipywidgets.VBox([x1, x2, ub, lb]),
ipywidgets.VBox([ap_mode, ref]),
ipywidgets.VBox([r0, r1, r2]),
], )
def f(**kwargs):
return interactive_polytope_repair(polytope_repair, **kwargs)
out = ipywidgets.interactive_output(
f, {
'x1': x1, 'x2': x2, 'ref': ref,
'lb': lb, 'ub': ub,
'r0': r0, 'r1': r1, 'r2': r2,
'ap_mode': ap_mode,
})
out.layout = ipywidgets.Layout(height='800px')
display(ui, out)
def interact_pointwise_repair_with(pointwise_repair):
r0.value='On'
r1.value='Off'
r2.value='Off'
ui = ipywidgets.HBox([
ipywidgets.VBox([x1, ub, lb]),
ipywidgets.VBox([ap_mode, ref]),
ipywidgets.VBox([r0, r1, r2]),
], )
def f(**kwargs):
return interactive_pointwise_repair(pointwise_repair, **kwargs)
out = ipywidgets.interactive_output(
f, {
'x1': x1, 'ref': ref,
'lb': lb, 'ub': ub,
'r0': r0, 'r1': r1, 'r2': r2,
'ap_mode': ap_mode,
})
out.layout = ipywidgets.Layout(height='800px')
display(ui, out)