/
bound_layers.py
executable file
·1525 lines (1339 loc) · 75.2 KB
/
bound_layers.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import numpy as np
from torch.nn import DataParallel
from torch.nn import Sequential, Conv2d, Linear, ReLU, LeakyReLU
from model_defs import Flatten, model_mlp_any
import torch.nn.functional as F
from itertools import chain
import logging
from bound_param_ramp import BoundLeakyReLUStep
logging.basicConfig(level=logging.INFO)
# logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class BoundFlatten(torch.nn.Module):
# this layer unwrap the input x to a 2D tensor
def __init__(self, bound_opts=None):
super(BoundFlatten, self).__init__()
self.bound_opts = bound_opts
def forward(self, x):
# usually the input is of shape (batch,C,H,W)
# then the output is of shape (batch, C*H*W)
self.shape = x.size()[1:] # it's of shape (C,H,W)
return x.view(x.size(0), -1)
def interval_propagate(self, norm, h_U, h_L, eps):
return norm, h_U.view(h_U.size(0), -1), h_L.view(h_L.size(0), -1), 0, 0, 0, 0
def linear_propagate(self, last_uA, last_ub, last_lA, last_lb):
# assume input of this layer is a, of shape (N, C_in, H_in, W_in)
# output of this layer is z, of shape (N, C_in H_in W_in)
# this function reshape last_uA, last_ub, last_lA, last_lb to their corresponding shape
# x is of shape (batch, C, H, W),
# last_uA and last_lA are of shape (batch, CHW, C_in, H_in, W_in)
# last_ub, last_lb are of the same shape (batch, C_in, H_in, W_in)
batch = last_uA.shape[0]
CHW = last_uA.shape[1]
last_uA = last_uA.view(batch, CHW, -1) # shape (batch, CHW, C_in H_in W_in)
last_uA = torch.transpose(last_uA, 1,2) # shape (batch, C_in H_in W_in, CHW)
last_ub = last_ub.view(batch, -1) # shape (batch, C_in H_in W_in)
last_lA = last_lA.view(batch, CHW, -1) # shape (batch, CHW, C_in H_in W_in)
last_lA = torch.transpose(last_lA, 1,2) # shape (batch, C_in H_in W_in, CHW)
last_lb = last_lb.view(batch, -1) # shape (batch, C_in H_in W_in)
return last_uA, last_ub, last_lA, last_lb
def bound_backward(self, last_uA, last_lA):
# assume input of this layer is a, of shape (batch,C,H,W)
# output is z, of shape (batch, CHW)
# the objective we want to bound is obj, of shape (batch, obj_dim)
# last_uA and last_lA is of shape (batch, obj_dim, C*H*W)
# and they satisfy last_lA z + some bias <= obj <= last_uA z + some bias
# we want to get uA and lA of shape (batch, obj_dim, C,H,W)
# such that lA a + some bias <= obj <= uA a + some bias
# where lA, uA reshape to (batch, obj_dim, C*H*W)
# a reshape to (batch, C*H*W, 1)
# uA and lA can be obtained by reshape last_uA and last_lA
def _bound_oneside(A):
if A is None:
return None
return A.view(A.size(0), A.size(1), *self.shape)
if self.bound_opts.get("same-slope", False) and (last_uA is not None) and (last_lA is not None):
new_bound = _bound_oneside(last_uA)
return new_bound, 0, new_bound, 0
else:
return _bound_oneside(last_uA), 0, _bound_oneside(last_lA), 0
class BoundLinear(Linear):
def __init__(self, in_features, out_features, bias=True, bound_opts=None):
super(BoundLinear, self).__init__(in_features, out_features, bias)
self.bound_opts = bound_opts
@staticmethod
def convert(linear_layer, bound_opts=None):
# extract weight and other useful information from a linear layer
l = BoundLinear(linear_layer.in_features, linear_layer.out_features, linear_layer.bias is not None, bound_opts)
l.weight.data.copy_(linear_layer.weight.data)
l.bias.data.copy_(linear_layer.bias.data)
return l
def bound_backward(self, last_uA, last_lA):
# in this linear layer we assume the input is a and output is z: z = weight a + bias
# we already know the quantity in interest, obj, can be bounded by two linear functions of z
# last_uA * z + last_ub <= obj <= last_lA * z + last_lb
# this function finds two linear functions of a to bound obj
# this function returns uA, ubias, lA, lbias such that
# uA * a + ubias + last_ub <= obj <= lA * a + lbias + last_lb
# a is of shape (batch, in_dim)
# z is of shape (batch, out_dim)
# last_A is of shape (batch, obj_dim, out_dim)
# weight is of shape (out_dim, in_dim)
# bias is of shape (out_dim)
def _bound_oneside(last_A, compute_A=True):
if last_A is None:
return None, 0
logger.debug('last_A %s', last_A.size())
# propagate A to the next layer
if compute_A:
# last_A shape (batch, obj_dim, out_dim)
# weight is of shape (out_dim, in_dim)
next_A = last_A.matmul(self.weight) # (batch, obj_dim, in_dim)
logger.debug('next_A %s', next_A.size())
else:
next_A = None
# compute the bias of this layer
# sum_bias need to to added to the last bias to get the real bias
# last_A shape (batch, obj_dim, out_dim)
# self.bias of shape (out_dim)
sum_bias = last_A.matmul(self.bias) # shape (batch, obj_dim)
logger.debug('sum_bias %s', sum_bias.size())
return next_A, sum_bias
if self.bound_opts.get("same-slope", False) and (last_uA is not None) and (last_lA is not None):
uA, ubias = _bound_oneside(last_uA, True)
_, lbias = _bound_oneside(last_lA, False)
lA = uA
else:
uA, ubias = _bound_oneside(last_uA)
lA, lbias = _bound_oneside(last_lA)
return uA, ubias, lA, lbias
@staticmethod
def get_closed_form_bound(Au, bu, Al, bl, x0, p_norm, eps, x_U=None, x_L=None):
# find the maximum value of Au x + bu and minimum value of Al x + bl
# when x is within the l-p ball ||x-x0||_{p_norm} <= eps
# x is of shape (batch, x_shape), x could be 2D or 4D, namely x could be flattened or original shape
# x_U and x_L if exist, they are the same shape as x: (batch, x_shape)
# Au is of shape (batch, out_dim, x_shape)
# bu is of shape (batch, out_dim)
batch = x0.shape[0]
if (p_norm != np.inf) or (x_U is None):
# print('Use x0 and eps to compute closed form bound')
if p_norm == 1:
dual_norm = np.inf
elif p_norm == np.inf:
dual_norm = 1
else:
dual_norm = 1/(1-1/p_norm)
x0_temp = x0.view(batch,-1).unsqueeze(2) # (batch, x_shape, 1)
# this part may have problem, we should figure out
# whether eps is for the original data or normalized data
upper = Au.matmul(x0_temp).squeeze(2) + bu + eps*torch.norm(Au, p=dual_norm, dim=2)
lower = Al.matmul(x0_temp).squeeze(2) + bl - eps*torch.norm(Al, p=dual_norm, dim=2)
# upper and lower are of shape (batch, out_dim)
else: # if norm=np.inf and x_U, x_L are not None
# x_L, x_U maybe tighter than x0-eps, x0+eps
# because we need to clamp x0-eps, x0+eps to the range [0,1]
# before feed it to the network
# print('Use x_L and x_U to compute closed form bound')
x_U_temp = x_U.view(batch,-1).unsqueeze(2)
x_L_temp = x_L.view(batch,-1).unsqueeze(2)
# x_L <= x <= x_U
# Au x + bu <= relu(Au) x_U + neg(Bu) x_L + bu
Au_relu = torch.clamp(Au, min=0)
Au_neg = torch.clamp(Au, max=0)
upper = Au_relu.matmul(x_U_temp).squeeze(2) + Au_neg.matmul(x_L_temp).squeeze(2) + bu
# Al x + bl >= relu(Al) x_L + neg(Bl) x_U + bl
Al_relu = torch.clamp(Al, min=0)
Al_neg = torch.clamp(Al, max=0)
lower = Al_relu.matmul(x_L_temp).squeeze(2) + Al_neg.matmul(x_U_temp).squeeze(2) + bl
return upper, lower
def linear_propagate(self, last_uA, last_ub, last_lA, last_lb, x0, norm, eps,
C = None, x_U=None, x_L=None):
# in this linear layer we assume the input is a and output is z: z = weight a + bias
# we already know a can be bounded by two linear functions of x
# last_lA x + last_lb <= a <= last_uA x + last_ub
# this function finds two linear functions of x to bound z (Cz if C is not None)
# this function returns uA, ubias, lA, lbias such that
# uA x + ubias <= z <= lA x + lbias
# we also need to compute the closed form bounds of z
# a is of shape (batch, in_dim)
# z is of shape (batch, out_dim)
# x is of shape (batch, x_shape),
# note that we assume x_shape is 1D, namely, x is flattened
# last_uA and last_lA are of shape (batch, in_dim, x_shape)
# last_ub, last_lb, a are of the same shape (batch, in_dim)
# weight is of shape (out_dim, in_dim)
# bias is of shape (out_dim)
# define this_layer_dim = products of elements in this_layer_shape
# this_layer_shape may have multi dimensions
if C is not None:
# C is of shape (batch, obj_dim, out_dim)
weight = C.matmul(self.weight) # shape (batch, obj_dim, in_dim)
bias = C.matmul(self.bias) # shape (batch, obj_dim)
else:
# weight dimension (this_layer_shape, prev_layer_shape)
weight = self.weight.unsqueeze(0) # (1, out_dim, in_dim)
bias = self.bias.unsqueeze(0) # (1, out_dim)
relu_W = weight.clamp(min=0) # (1, out_dim, in_dim) or (batch, obj_dim, in_dim)
nega_W = weight.clamp(max=0) # (1, out_dim, in_dim) or (batch, obj_dim, in_dim)
# last_A (batch, in_dim, x_shape)
lA = relu_W.matmul(last_lA) + nega_W.matmul(last_uA)
# lA (batch, out_dim, x_shape) or (batch, obj_dim, x_shape)
# W shape (1, out_dim, in_dim) or (batch, obj_dim, in_dim)
# last_b.unsqueeze(2) shape (batch, in_dim, 1)
lbias = relu_W.matmul(last_lb.unsqueeze(2)).squeeze(2) + nega_W.matmul(last_ub.unsqueeze(2)).squeeze(2) + bias
# lbias shape (batch, out_dim) or (batch, obj_dim)
uA = relu_W.matmul(last_uA) + nega_W.matmul(last_lA)
ubias = relu_W.matmul(last_ub.unsqueeze(2)).squeeze(2) + nega_W.matmul(last_lb.unsqueeze(2)).squeeze(2) + bias
h_U, h_L = BoundLinear.get_closed_form_bound(uA, ubias, lA, lbias, x0, norm, eps,
x_U=x_U, x_L=x_L)
#h_L <= z <= h_U, are of shape (batch, out_dim) or (batch, obj_dim)
return uA, ubias, lA, lbias, h_U, h_L
def interval_propagate(self, norm, h_U, h_L, eps, C = None):
# h_U and h_L should be of shape (batch, prev_layer_shape)
# they are upper and lower bound of previous layer
# merge the specification
if C is not None:
# after multiplication with C, we have (batch, output_shape, prev_layer_shape)
# we have batch dimension here because of each example has different C
weight = C.matmul(self.weight)
bias = C.matmul(self.bias)
else:
# weight dimension (this_layer_shape, prev_layer_shape)
weight = self.weight
bias = self.bias
if norm == np.inf:
# Linf norm
mid = (h_U + h_L) / 2.0
diff = (h_U - h_L) / 2.0
weight_abs = weight.abs()
if C is not None:
center = weight.matmul(mid.unsqueeze(-1)) + bias.unsqueeze(-1)
deviation = weight_abs.matmul(diff.unsqueeze(-1))
# these have an extra (1,) dimension as the last dimension
center = center.squeeze(-1)
deviation = deviation.squeeze(-1)
else:
# fused multiply-add
center = torch.addmm(bias, mid, weight.t())
deviation = diff.matmul(weight_abs.t())
else:
# L2 norm
h = h_U # h_U = h_L, and eps is used
dual_norm = np.float64(1.0) / (1 - 1.0 / norm)
if C is not None:
center = weight.matmul(h.unsqueeze(-1)) + bias.unsqueeze(-1)
center = center.squeeze(-1)
else:
center = torch.addmm(bias, h, weight.t())
# center = bias + h * weight.t()
# .t() is transpose of the tensor
deviation = weight.norm(dual_norm, -1) * eps
upper = center + deviation
lower = center - deviation
return np.inf, upper, lower, 0, 0, 0, 0
class BoundConv2d(Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, bound_opts=None):
super(BoundConv2d, self).__init__(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
self.bound_opts = bound_opts
@staticmethod
def convert(l, bound_opts=None):
nl = BoundConv2d(l.in_channels, l.out_channels, l.kernel_size, l.stride, l.padding, l.dilation, l.groups, l.bias is not None, bound_opts)
nl.weight.data.copy_(l.weight.data)
nl.bias.data.copy_(l.bias.data)
logger.debug(nl.bias.size())
logger.debug(nl.weight.size())
return nl
def forward(self, input):
output = super(BoundConv2d, self).forward(input)
self.output_shape = output.size()[1:]
self.input_shape = input.size()[1:]
return output
def bound_backward(self, last_uA, last_lA):
# assume input is a, of shape (batch, C_in, H_in, W_in),
# output is z, of shape (batch, C_out, H_out, W_out),
# the objective we want to bound is obj, of shape (batch, obj_dim)
# then last_uA and last_lA are of shape (batch, obj_dim, C_out, H_out, W_out)
# and they satisfy last_lA z + some bias <= obj <= last_uA z + some bias
# where last_lA, last_uA reshape to (batch, obj_dim, C_out*H_out*W_out)
# z reshape to (batch, C_out*H_out*W_out, 1)
# we want to compute uA and lA such of shape (batch, obj_dim, C_in, H_in, W_in)
# such that lA a + some bias <= obj <= uA a + some bias
# where lA, uA reshape to (batch, obj_dim, C_in*H_in*W_in)
# a reshape to (batch, C_in*H_in*W_in, 1)
def _bound_oneside(last_A, compute_A=True):
if last_A is None:
return None, 0
logger.debug('last_A %s', last_A.size())
shape = last_A.size()
# propagate A to the next layer, with batch concatenated together
if compute_A:
output_padding0 = int(self.input_shape[1]) - (int(self.output_shape[1]) - 1) * self.stride[0] + 2 * self.padding[0] - int(self.weight.size()[2])
output_padding1 = int(self.input_shape[2]) - (int(self.output_shape[2]) - 1) * self.stride[1] + 2 * self.padding[1] - int(self.weight.size()[3])
next_A = F.conv_transpose2d(last_A.view(shape[0] * shape[1], *shape[2:]), self.weight, None, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups, output_padding=(output_padding0, output_padding1))
next_A = next_A.view(shape[0], shape[1], *next_A.shape[1:])
logger.debug('next_A %s', next_A.size())
else:
next_A = False
logger.debug('bias %s', self.bias.size())
# dot product, compute the bias of this layer, do a dot product
sum_bias = (last_A.sum((3,4)) * self.bias).sum(2)
logger.debug('sum_bias %s', sum_bias.size())
return next_A, sum_bias
# if the slope is the same (Fast-Lin) and both matrices are given, only need to compute one of them
if self.bound_opts.get("same-slope", False) and (last_uA is not None) and (last_lA is not None):
uA, ubias = _bound_oneside(last_uA, True)
_, lbias = _bound_oneside(last_lA, False)
lA = uA
else:
uA, ubias = _bound_oneside(last_uA)
lA, lbias = _bound_oneside(last_lA)
return uA, ubias, lA, lbias
def interval_propagate(self, norm, h_U, h_L, eps):
if norm == np.inf:
mid = (h_U + h_L) / 2.0
diff = (h_U - h_L) / 2.0
weight_abs = self.weight.abs()
deviation = F.conv2d(diff, weight_abs, None, self.stride, self.padding, self.dilation, self.groups)
else:
# L2 norm
mid = h_U
logger.debug('mid %s', mid.size())
# TODO: consider padding here?
deviation = torch.mul(self.weight, self.weight).sum((1,2,3)).sqrt() * eps
logger.debug('weight %s', self.weight.size())
logger.debug('deviation %s', deviation.size())
deviation = deviation.unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
logger.debug('unsqueezed deviation %s', deviation.size())
center = F.conv2d(mid, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
logger.debug('center %s', center.size())
upper = center + deviation
lower = center - deviation
return np.inf, upper, lower, 0, 0, 0, 0
def linear_propagate(self, last_uA, last_ub, last_lA, last_lb, x0, norm, eps,
C = None, x_U=None, x_L=None):
# in this linear layer we assume the input is a and output is z: z = conv(a) + bias
# we already know a can be bounded by two linear functions of x
# last_lA x + last_lb <= a <= last_uA x + last_ub
# this function finds two linear functions of x to bound z
# this function returns uA, ubias, lA, lbias such that
# uA x + ubias <= z <= lA x + lbias
# we also need to compute the closed form bounds of z
# C has no use in this function, C is only used for the last layer, while a conv layer will never be the last layer of a network
# a is of shape (batch, C_in, H_in, W_in)
# z is of shape (batch, C_out, H_out, W_out)
# x is of shape (batch, C, H, W),
# last_uA and last_lA are of shape (batch, CHW, C_in, H_in, W_in)
# last_ub, last_lb, a are of the same shape (batch, C_in, H_in, W_in)
# weight is of shape (C_out, C_in, kernel_size[0], kernel_size[1])
# bias is of shape (C_out)
# define this_layer_dim = products of elements in this_layer_shape
# this_layer_shape may have multi dimensions
weight = self.weight
bias = self.bias
relu_W = weight.clamp(min=0)
nega_W = weight.clamp(max=0)
batch = last_uA.shape[0]
CHW = last_uA.shape[1]
C_in = last_uA.shape[2]
H_in = last_uA.shape[3]
W_in = last_uA.shape[4]
lA = (F.conv2d(last_lA.reshape(-1,C_in,H_in,W_in), relu_W, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups)
+ F.conv2d(last_uA.reshape(-1,C_in,H_in,W_in), nega_W, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups))
# lA now is of shape (batch*CHW, C_out, H_out, W_out)
C_out = lA.shape[1]
H_out = lA.shape[2]
W_out = lA.shape[3]
lA = lA.view(batch, CHW, C_out, H_out, W_out)
lbias = (F.conv2d(last_lb, relu_W, bias=bias, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups)
+ F.conv2d(last_ub, nega_W, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups))
uA = (F.conv2d(last_uA.reshape(-1,C_in,H_in,W_in), relu_W, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups)
+ F.conv2d(last_lA.reshape(-1,C_in,H_in,W_in), nega_W, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups))
uA = uA.view(batch, CHW, C_out, H_out, W_out)
ubias = (F.conv2d(last_ub, relu_W, bias=bias, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups)
+ F.conv2d(last_lb, nega_W, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups))
h_U, h_L = BoundConv2d.get_closed_form_bound(uA, ubias, lA, lbias, x0, norm, eps,
x_U=x_U, x_L=x_L)
return uA, ubias, lA, lbias, h_U, h_L
@staticmethod
def get_closed_form_bound(Au, bu, Al, bl, x0, p_norm, eps, x_U=None, x_L=None):
# find the maximum value of Au x + bu and minimum value of Al x + bl
# when x is within the l-p ball ||x-x0||_{p_norm} <= eps
# x is of shape (batch, C,H,W)
# x_U and x_L if exist, they are the same shape as x
# Au,Al are of shape (batch, CHW, C_out, H_out, W_out)
# bu,bl are of shape (batch, C_out, H_out, W_out)
batch = Au.shape[0]
CHW = Au.shape[1]
C_out = Au.shape[2]
H_out = Au.shape[3]
W_out = Au.shape[4]
Au_temp = Au.view(batch, CHW, -1) # shape (batch, CHW, C_out H_out W_out)
Au_temp = torch.transpose(Au_temp, 1, 2) # shape (batch, C_out H_out W_out, CHW)
Al_temp = Al.view(batch, CHW, -1) # shape (batch, CHW, C_out H_out W_out)
Al_temp = torch.transpose(Al_temp, 1, 2) # shape (batch, C_out H_out W_out, CHW)
if (p_norm != np.inf) or (x_U is None):
# print('Use x0 and eps to compute closed form bound')
if p_norm == 1:
dual_norm = np.inf
elif p_norm == np.inf:
dual_norm = 1
else:
dual_norm = 1/(1-1/p_norm)
x0_temp = x0.view(batch, -1).unsqueeze(2) # (batch, CHW, 1)
# this part may have problem, we should figure out
# whether eps is for the original data or normalized data
upper = Au_temp.matmul(x0_temp).squeeze(2) + eps*torch.norm(Au_temp, p=dual_norm, dim=2) #shape (batch, C_out H_out W_out)
upper = upper.view(batch, C_out, H_out, W_out) + bu
lower = Al_temp.matmul(x0_temp).squeeze(2) - eps*torch.norm(Al_temp, p=dual_norm, dim=2) #shape (batch, C_out H_out W_out)
lower = lower.view(batch, C_out, H_out, W_out) + bl
# upper and lower are of shape (batch, C_out, H_out, W_out)
else: # if norm=np.inf and x_U, x_L are not None
# x_L, x_U maybe tighter than x0-eps, x0+eps
# because we need to clamp x0-eps, x0+eps to the range [0,1]
# before feed it to the network
# print('Use x_L and x_U to compute closed form bound')
x_U_temp = x_U.view(batch, -1).unsqueeze(2) # (batch, CHW, 1)
x_L_temp = x_L.view(batch, -1).unsqueeze(2) # (batch, CHW, 1)
# x_L <= x <= x_U
# Au x + bu <= relu(Au) x_U + neg(Bu) x_L + bu
Au_relu = torch.clamp(Au_temp, min=0) # shape (batch, C_out H_out W_out, CHW)
Au_neg = torch.clamp(Au_temp, max=0) # shape (batch, C_out H_out W_out, CHW)
upper = Au_relu.matmul(x_U_temp).squeeze(2) + Au_neg.matmul(x_L_temp).squeeze(2) # shape (batch, C_out H_out W_out)
upper = upper.view(batch, C_out, H_out, W_out) + bu # shape (batch, C_out, H_out, W_out)
# Al x + bl >= relu(Al) x_L + neg(Bl) x_U + bl
Al_relu = torch.clamp(Al_temp, min=0)
Al_neg = torch.clamp(Al_temp, max=0)
lower = Al_relu.matmul(x_L_temp).squeeze(2) + Al_neg.matmul(x_U_temp).squeeze(2) # shape (batch, C_out H_out W_out)
lower = lower.view(batch, C_out, H_out, W_out) + bl # shape (batch, C_out, H_out, W_out)
return upper, lower
def get_linear_bound_for_relu(l, u, bound_opts):
# This function finds bounding lines for ReLU activation in the interval [l ,u]
# bound_opts is a dictionary could contain keys use-constant, same-slope, zero-lb, one-lb,
# only one of the keys should have value True
# if use-constant, we choose both boundling lines with 0 slopes at any cases
# elif same-slope, we choose tight upper bounding line, lower bounding line with same slope when l<0 and u>0
# elif zero-lb, we choose tight upper bounding line, lower bounding line with 0 slope when l<0 and u>0
# elif one-lb, we choose tight upper bounding line, lower bounding line with 1 when l<0 and u>0
# else, we choose tight upper bounding line, lower bounding lines with adaptive slope
# except for use-constant, other choices don't affect how we choose bounding lines when l>=0 or u<=0
# in these cases, we always chooose tightest upper and lower bounding lines
device = l.device
# don't change how to initialize them here
ku = torch.zeros(u.shape, device = device)
bu = torch.zeros(u.shape, device = device)
kl = torch.zeros(l.shape, device = device)
bl = torch.zeros(l.shape, device = device)
if bound_opts.get('use-constant', False):
bu = torch.clamp(u, min=0)
bl = torch.clamp(l, min=0)
# print('has use constant')
return kl, bl, ku, bu
# case u<=0, the 0 initialization already satisfy this case
# case l>=0
idx = (l>=0)
kl[idx] = 1
ku[idx] = 1
# bl and kl is 0
# case l<0 and u>0
idx = (l<0) * (u>0)
k = (u / (u-l))[idx]
# k u + b = u -> b = (1-k) * u
b = (1-k) * u[idx]
ku[idx] = k
bu[idx] = b
# bl already 0
# kl should be between 0 and 1
if bound_opts.get('same-slope', False): # parallel to the upper line
kl[idx] = k
elif bound_opts.get('zero-lb', False): # always use 0 slope
pass
# kl[idx] = 0 # kl is initialized with 0, don't need to redo this
elif bound_opts.get('one-lb', False): # always use 1 slope
kl[idx] = 1
elif bound_opts.get('adaptive-lb', False): # use adaptive
u_geq_l = (u.abs()>=l.abs())
new_idx = idx * u_geq_l
kl[new_idx] = 1
# new_idx = idx * (1-u_geq_l)
# kl[new_idx] = 0 # kl is initialized with 0, don't need to redo this
else:
print('bound_opts:', bound_opts)
raise Exception('bound-opts not supported')
return kl, bl, ku, bu
class BoundReLU(ReLU):
def __init__(self, prev_layer, inplace=False, bound_opts=None):
super(BoundReLU, self).__init__(inplace)
# ReLU needs the previous layer's bounds
# self.prev_layer = prev_layer
self.bound_opts = bound_opts
self.upper_u = None # shape (batch, this_layer_shape)
self.lower_l = None
# the lower and upper bounds of the preactivation will be recorded
# as self.upper_u and self.lower_l if interval_propagate or linear_propagate is called
self.dead = None
self.alive = None
self.unstable = None
# assume input of this relu layer is z and output is a: a = relu(z)
# self.alpha_l = None
# self.beta_l = None
# self.alpha_u = None
# self.beta_u = None
# these quantities records the linear functions of x to bound z
# alpha_l * z + beta_l <= z <= alpha_u * z + beta_u
# For relu between linear layers
# z is of shape (batch, n, 1)
# x is of shape (batch, n0, 1)
# alpha is of shape (batch, n, n0n)
# beta is of shape (batch, n, 1)
# In reality, those dimensions of width 1 may be squeezed
## Convert a ReLU layer to BoundReLU layer
# @param act_layer ReLU layer object
# @param prev_layer Pre-activation layer, used for get preactivation bounds
def update_neuron_status(self):
self.dead = (self.upper_u<=0).float().mean()
self.alive = (self.lower_l>=0).float().mean()
self.unstable = ((self.lower_l<0) * (self.upper_u>0)).float().mean()
@staticmethod
def convert(act_layer, prev_layer, bound_opts=None):
l = BoundReLU(prev_layer, act_layer.inplace, bound_opts)
return l
def interval_propagate(self, norm, h_U, h_L, eps):
assert norm == np.inf
guard_eps = 1e-5
self.unstab = ((h_L < -guard_eps) & (h_U > guard_eps))
# self.unstab indicates that this neuron's activation is unsure
# stored upper and lower bounds will be used for backward bound propagation
# this is the upper and lower bounds of the input of this relu layer
self.upper_u = h_U
self.lower_l = h_L
self.update_neuron_status()
tightness_loss = self.unstab.sum()
# tightness_loss = torch.min(h_U_unstab * h_U_unstab, h_L_unstab * h_L_unstab).sum()
return norm, F.relu(h_U), F.relu(h_L), tightness_loss, tightness_loss, \
(h_U < 0).sum(), (h_L > 0).sum()
def bound_backward(self, last_uA, last_lA):
# in this relu layer we assume the input is z and output is a: a = relu(z)
# we already know the quantity in interest, obj, can be bounded by two linear functions of a
# last_uA a + last_ub <= obj <= last_lA a + last_lb
# this function finds two linear functions of z to bound obj
# this function returns uA, ubias, lA, lbias such that
# uA * z + ubias + last_ub <= obj <= lA * z + lbias + last_lb
# last_uA and last_lA are of shape (batch, obj_dim, this_layer_shape)
# define this_layer_dim = products of elements in this_layer_shape
# this_layer_shape may have multi dimensions
lb_r = self.lower_l.clamp(max=0) # shape (batch, this_layer_shape), same as a or z
ub_r = self.upper_u.clamp(min=0) # shape (batch, this_layer_shape), same as a or z
# this step guarantees upper_d = 0, upper_b=0 if upper_u <= 0
# upper_d = 1, upper_b=0 if lower_l >=0
# avoid division by 0 when both lb_r and ub_r are 0
ub_r = torch.max(ub_r, lb_r + 1e-8)
# CROWN upper and lower linear bounds
upper_d = ub_r / (ub_r - lb_r)
upper_b = - lb_r * upper_d
# note that there is no lower_b because the lower bounding line always passes the origin
upper_d = upper_d.unsqueeze(1) # shape (batch, 1, this_layer_shape)
if self.bound_opts.get("same-slope", False) or self.bound_opts.get("backward_same-slope", False):
# the same slope for upper and lower
lower_d = upper_d
elif self.bound_opts.get("zero-lb", False) or self.bound_opts.get("backward_zero-lb", False):
# Always use slope 0 as lower bound. Any value between 0 and 1 is a valid lower bound for CROWN
lower_d = (upper_d >= 1.0).float()
elif self.bound_opts.get("one-lb", False) or self.bound_opts.get("backward_one-lb", False):
# Always use slope 1 as lower bound
lower_d = (upper_d > 0.0).float()
elif self.bound_opts.get("adaptive-lb", False) or self.bound_opts.get("backward_adaptive-lb", False):
lower_d = (upper_d > 0.5).float()
else:
raise Exception('The bounding line choice is not supported or you have not specified a bounding line choice method')
uA = lA = None
ubias = lbias = 0
# Choose upper or lower bounds based on the sign of last_A
if last_uA is not None:
pos_uA = last_uA.clamp(min=0) # shape (batch, obj_dim, this_layer_shape)
if self.bound_opts.get("same-slope", False):
# same upper_d and lower_d, no need to check the sign
uA = upper_d * last_uA # shape (batch, obj_dim, this_layer_shape)
else:
neg_uA = last_uA.clamp(max=0)
uA = upper_d * pos_uA + lower_d * neg_uA # shape (batch, obj_dim, this_layer_shape)
mult_uA = pos_uA.view(last_uA.size(0), last_uA.size(1), -1) # shape (batch, obj_dim, this_layer_dim)
ubias = mult_uA.matmul(upper_b.view(upper_b.size(0), -1, 1)).squeeze(-1) # of shape (batch, obj_dim)
# upper_b.view(upper_b.size(0), -1, 1) is of shape (batch, this_layer_dim, 1)
# note that there is no lower_b because the lower bounding line always passes the origin
if last_lA is not None:
neg_lA = last_lA.clamp(max=0)
if self.bound_opts.get("same-slope", False) or self.bound_opts.get("backward_same-slope", False):
lA = uA if uA is not None else lower_d * last_lA
else:
pos_lA = last_lA.clamp(min=0)
lA = upper_d * neg_lA + lower_d * pos_lA
mult_lA = neg_lA.view(last_lA.size(0), last_lA.size(1), -1)
lbias = mult_lA.matmul(upper_b.view(upper_b.size(0), -1, 1)).squeeze(-1)
# note that there is no lower_b because the lower bounding line always passes the origin
# lbias and ubias need to to added to the last bias to get the real bias
return uA, ubias, lA, lbias
def linear_propagate(self, h_U, h_L, last_uA, last_ub, last_lA, last_lb):
# in this relu layer we assume the input is z and output is a: a = relu(z)
# hU and hL are the upper and lower bounds of z
# we already know z can be bounded by two linear functions of x
# last_lA x + last_lb <= z <= last_uA x + last_ub
# this function finds two linear functions of x to bound a
# this function returns uA, ubias, lA, lbias such that
# uA x + ubias <= a <= lA x + lbias
# we don't need to compute the closed form bounds of a
# the bounds of the next layer's preactivation should be computed in BoundLinear and BoundConv
# x is of shape (batch, x_shape)
# last_uA and last_lA are of shape (batch, this_layer_shape, x_shape)
# last_ub, last_lb, z, a are of the same shape (batch, this_layer_shape)
# define this_layer_dim = products of elements in this_layer_shape
# this_layer_shape may have multi dimensions
# this is the upper and lower bounds of the input of this relu layer
self.upper_u = h_U # shape (batch, this_layer_shape)
self.lower_l = h_L # shape (batch, this_layer_shape)
self.update_neuron_status()
if self.bound_opts.get("use-constant", False):
# avoid division by 0 when both lb_r and ub_r are 0
h_U_new = torch.max(h_U, h_L + 1e-8)
# CROWN upper and lower linear bounds
lower_d, lower_b, upper_d, upper_b = get_linear_bound_for_relu(
h_L, h_U_new, self.bound_opts)
else:
lb_r = self.lower_l.clamp(max=0) # shape (batch, this_layer_shape), same as a or z
ub_r = self.upper_u.clamp(min=0) # shape (batch, this_layer_shape), same as a or z
ub_r = torch.max(ub_r, lb_r + 1e-8)
upper_d = ub_r / (ub_r - lb_r)
upper_b = - lb_r * upper_d
lower_b = torch.Tensor([0]).to(upper_d.device)
if self.bound_opts.get("same-slope", False) or self.bound_opts.get("lbp_same-slope", False):
# the same slope for upper and lower
lower_d = upper_d
elif self.bound_opts.get("zero-lb", False) or self.bound_opts.get("lbp_zero-lb", False):
# Always use slope 0 as lower bound. Any value between 0 and 1 is a valid lower bound for CROWN
lower_d = (upper_d >= 1.0).float()
elif self.bound_opts.get("one-lb", False) or self.bound_opts.get("lbp_one-lb", False):
# Always use slope 1 as lower bound
lower_d = (upper_d > 0.0).float()
elif self.bound_opts.get("adaptive-lb", False) or self.bound_opts.get("lbp_adaptive-lb", False):
lower_d = (upper_d > 0.5).float()
else:
raise Exception('The bounding line choice is not supported or you have not specified a bounding line choice method')
# detach bounding line parameters
if self.bound_opts.get("detach", False):
upper_d = upper_d.detach()
upper_b = upper_b.detach()
lower_d = lower_d.detach()
lower_b = lower_b.detach()
uA = lA = None
ubias = lbias = 0
# Choose upper or lower bounds based on the sign of last_A
if last_uA is not None:
if len(last_uA.shape) == 3:
# if the layer before this layer is a linear layer
# upper_d shape (batch, this_layer_shape)
# last_uA shape (batch, this_layer_shape, x_shape)
uA = upper_d.unsqueeze(2) * last_uA # (batch, this_layer_shape, x_shape)
elif len(last_uA.shape) == 5:
# if the layer before this layer is a conv layer
# last_uA is of shape (batch, CHW, C_out, H_out, W_out)
# upper_d has the shape (batch, C_out, H_out, W_out)
uA = upper_d.unsqueeze(1) * last_uA # (batch, CHW, C_out, H_out, W_out)
else:
raise Exception('The shape of last_uA is %s, which is not correct.' % str(last_uA.shape))
# last_ub shape (batch, this_layer_shape)
# upper_b shape (batch, this_layer_shape)
ubias = upper_d * last_ub + upper_b # (batch, this_layer_shape)
if last_lA is not None:
if len(last_lA.shape) == 3:
# lower_d shape (batch, this_layer_shape)
# last_lA shape (batch, this_layer_shape, x_shape)
lA = lower_d.unsqueeze(2) * last_lA # (batch, this_layer_shape, x_shape)
elif len(last_lA.shape) == 5:
lA = lower_d.unsqueeze(1) * last_lA
else:
raise Exception('The shape of last_lA is %s, which is not correct.' % str(last_lA.shape))
# last_lb shape (batch, this_layer_shape)
lbias = lower_d * last_lb + lower_b # (batch, this_layer_shape)
return uA, ubias, lA, lbias
class BoundLeakyReLU(LeakyReLU):
def __init__(self, neg_slope=0.01, inplace=False, bound_opts=None, shape=None):
super(BoundLeakyReLU, self).__init__(neg_slope, inplace)
self.bound_opts = bound_opts
self.neg_slope = neg_slope
# need to set bound_opts['activation'] = 'hard_tanh' if want to use this activation
# also need to set the value for bound_opts['neg_slope']
self.upper_u = None # shape (batch, this_layer_shape)
self.lower_l = None
# the lower and upper bounds of the preactivation will be recorded
# as self.upper_u and self.lower_l if interval_propagate or linear_propagate is called
self.dead = None
self.alive = None
self.unstable = None
if self.bound_opts.get('param-all-lb', False) or self.bound_opts.get('param-unstable-lb', False) or self.bound_opts.get('param-all-lb-tight', False):
# shape is the shape of input of this layer, with out batch dimension
self.kl = torch.rand(shape)
self.kl = self.kl.clamp_(min=self.neg_slope)
self.kl = self.kl.unsqueeze(0)
self.kl = torch.nn.Parameter(self.kl)
def update_slope(self, slope):
self.neg_slope = slope
def forward(self, x):
out = F.leaky_relu(x, negative_slope=self.neg_slope)
return out
def update_neuron_status(self):
self.dead = (self.upper_u<=0).float().mean()
self.alive = (self.lower_l>=0).float().mean()
self.unstable = ((self.lower_l<0) * (self.upper_u>0)).float().mean()
@staticmethod
def get_line_params_from_two_points(x1, y1, x2, y2):
# compute the line slope and intercept that pass through the points (x1, y1), (x2, y2)
diff = x2-x1
small_values = diff.abs() < 1e-6
large_values = ~small_values
diff = large_values.float() * diff + small_values.float() * 1e-6
k = (y2-y1)/diff
# k x1 + b = y1
# b = y1-k*x1
# k x2 + b = y2
b = y2-k*x2
# if we assume x2>=x1, y2>=x1
# we can guarantee the line k x + b is always above the two input points even if diff.abs() < 1e-6
return k, b
def get_bound_lines(self, l, u):
u = torch.max(u, l + 1e-6)
yl = F.leaky_relu(l, negative_slope=self.neg_slope)
yu = F.leaky_relu(u, negative_slope=self.neg_slope)
ku, bu = self.get_line_params_from_two_points(l, yl, u, yu)
alive = (l>=0).float()
dead = (u<=0).float()
unstable = ((l<0) * (u>0)).float()
ku = self.neg_slope * dead + ku*unstable + alive
bu = bu*unstable # because when a neuron is dead or alive, bu=0
if self.bound_opts.get("same-slope", False):
# the same slope for upper and lower
# this formulation is valid in any case: the neuron is dead, unstable, or alive
kl = ku
# bl = torch.Tensor([0]).to(ku.device)
bl = torch.zeros(bu.shape).to(bu.device)
elif self.bound_opts.get("zero-lb", False):
# this is the tight strategy stated in the paper
# Use slope self.neg_slope as lower slope when a neuron is dead or unstable, use 1 when it's alive
kl = alive + (1-alive)*self.neg_slope
# bl = torch.Tensor([0]).to(ku.device)
bl = torch.zeros(bu.shape).to(bu.device)
elif self.bound_opts.get("always-neg-slope-lb", False):
# Always use slope self.neg_slope as lower slope, even when it's alive
# when it's dead or unstable, bl=0
# when it's alive, it passes (l,yl). yl = kl l + bl, bl = yl - kl l
kl = torch.ones(l.shape).to(l.device)*self.neg_slope
bl = torch.zeros(l.shape).to(l.device)
bl = bl + alive * (yl-kl*l)
elif self.bound_opts.get("always-zero-lb", False):
# Always use slope 0 as lower slope
kl = torch.zeros(l.shape).to(l.device)
bl = yl
elif self.bound_opts.get("one-lb", False):
# Use slope 1 as lower slope when a neuron is alive or unstable, use self.neg_slope when it's dead
kl = dead * self.neg_slope + (1-dead)
# bl = torch.Tensor([0]).to(ku.device)
bl = torch.zeros(bu.shape).to(bu.device)
elif self.bound_opts.get("use-constant", False):
kl = torch.zeros(u.shape).to(u.device)
ku = torch.zeros(u.shape).to(u.device)
bl = yl
bu = yu
elif self.bound_opts.get("adaptive-lb", False):
# adaptive lower slope as default
# this formulation is only valid when self.neg_slope is small
u_big = u>(-l)
l_big = ~u_big
kl = (dead + unstable*l_big.float()) * self.neg_slope + (alive+unstable*u_big.float())
# bl = torch.Tensor([0]).to(ku.device)
bl = torch.zeros(bu.shape).to(bu.device)
elif self.bound_opts.get('param-all-lb', False):
# always use self.kl as lower bounding line no matter what status the neuron is in
# and always use bl=0
self.kl.data.clamp_(min=self.neg_slope, max=1)
kl = self.kl
# bl = torch.Tensor([0]).to(ku.device)
bl = torch.zeros(bu.shape).to(bu.device)
elif self.bound_opts.get('param-unstable-lb', False):
# only use self.kl as lower bounding line for unstable neurons, use tight lower bounding line in other cases
self.kl.data.clamp_(min=self.neg_slope, max=1)
kl = self.kl*unstable + self.neg_slope*dead + 1*alive
# bl = torch.Tensor([0]).to(ku.device)
bl = torch.zeros(bu.shape).to(bu.device)
elif self.bound_opts.get('param-all-lb-tight', False):
# always use self.kl as lower bounding line no matter what status the neuron is in
# but use largest possible bl
self.kl.data.clamp_(min=self.neg_slope, max=1)
kl = self.kl
bl = torch.zeros(kl.shape).to(kl.device)
# kl x + bl pass u,yu when the neuron is dead, yu = kl u + bl
bl = bl + dead * (yu-kl*u)
# kl x + bl pass l,yl when the neuron is alive, yl = kl l + bl
bl = bl + alive * (yl-kl*l)
# bl = 0 when the neuron is unstable, nothing need to be done
elif 'bound_specification' in self.bound_opts.keys():
bound_spec = self.bound_opts['bound_specification']
# bound_spec has the form 'tt-tc-tt'
bound_s = bound_spec.split('-')
# dead neurons
if bound_s[0][0] == 't': # dead upper bounding line
ku_dead = self.neg_slope
bu_dead = 0
elif bound_s[0][0] == 'c':
ku_dead = 0
bu_dead = yu
else:
raise Exception('bound specification elements must be t or c, but got', bound_spec)
if bound_s[0][1] == 't': # dead lower bounding line
kl_dead = self.neg_slope
bl_dead = 0
elif bound_s[0][1] == 'c':
kl_dead = 0
bl_dead = yl
else:
raise Exception('bound specification elements must be t or c, but got', bound_spec)
# unstable neurons
if bound_s[1][0] == 't': # unstable upper bounding line
ku_unstable = ku
bu_unstable = bu
elif bound_s[1][0] == 'c':
ku_unstable = 0
bu_unstable = yu
else:
raise Exception('bound specification elements must be t or c, but got', bound_spec)
if bound_s[1][1] == 't': # unstable lower bounding line
kl_unstable = self.neg_slope
bl_unstable = 0
elif bound_s[1][1] == 'c':
kl_unstable = 0
bl_unstable = yl
else:
raise Exception('bound specification elements must be t or c, but got', bound_spec)
# alive neurons
if bound_s[1][0] == 't': # alive upper bounding line
ku_alive = 1
bu_alive = 0
elif bound_s[1][0] == 'c':
ku_alive = 0
bu_alive = yu
else:
raise Exception('bound specification elements must be t or c, but got', bound_spec)
if bound_s[1][1] == 't': # alive lower bounding line
kl_alive = 1
bl_alive = 0
elif bound_s[1][1] == 'c':
kl_alive = 0
bl_alive = yl
else:
raise Exception('bound specification elements must be t or c, but got', bound_spec)
ku = dead*ku_dead + unstable*ku_unstable + alive*ku_alive
bu = dead*bu_dead + unstable*bu_unstable + alive*bu_alive
kl = dead*kl_dead + unstable*kl_unstable + alive*kl_alive
bl = dead*bl_dead + unstable*bl_unstable + alive*bl_alive
else:
raise Exception('You have not specified a valid method to choose lower bounding line.\n bound_opts: %s' % bound_opts)
# if True in torch.isnan(ku) or True in torch.isnan(bu) or True in torch.isnan(kl) or True in torch.isnan(bl):
# pdb.set_trace()
return ku,bu,kl,bl
def interval_propagate(self, norm, h_U, h_L, eps):
assert norm == np.inf
guard_eps = 1e-5
self.unstab = ((h_L < -guard_eps) & (h_U > guard_eps))
# self.unstab indicates that this neuron's activation is unsure
# stored upper and lower bounds will be used for backward bound propagation
# this is the upper and lower bounds of the input of this relu layer
self.upper_u = h_U
self.lower_l = h_L
self.update_neuron_status()
tightness_loss = self.unstab.sum()
# tightness_loss = torch.min(h_U_unstab * h_U_unstab, h_L_unstab * h_L_unstab).sum()
out_U = F.leaky_relu(h_U, negative_slope=self.neg_slope)
out_L = F.leaky_relu(h_L, negative_slope=self.neg_slope)
return norm, out_U, out_L, tightness_loss, tightness_loss, \
(h_U < 0).sum(), (h_L > 0).sum()