Boxes, and what they forget
Interval arithmetic pushed through a ReLU network by hand: where it proves robustness, the two separate reasons it stops, the wrapping effect, and what splitting the input costs.
· 11 min read · Chapter 2, Verifying neural networks
Contents
The simplest set we could use for is a box. Give every variable a lower bound and an upper bound, and forget everything else. In two dimensions that's a rectangle with sides parallel to the axes. In dimensions it's a product of intervals.
Computing with intervals goes back to Ramon Moore's work in the 1960s, and it's still the first thing anyone tries. This chapter runs it on our network by hand and then takes apart the reasons it stops working. There are two of them, and they're worth keeping apart, because later chapters fix them in different ways.
Arithmetic on intervalsCopy link to section
For every operation in the network we need a rule that takes intervals for the inputs and returns an interval for the result, with the guarantee that the true result is always inside. Three rules are enough for a ReLU network.
Adding two intervals adds their ends: .
Multiplying by a number scales the ends, and swaps them when is negative: if , and if .
ReLU is increasing, so it can be applied to each end: .
For a whole linear layer these rules combine into one formula that is easier to remember. Describe the input box by its centre and its radius , so that ranges over . Then
where is with every entry replaced by its absolute value. The centre goes through the matrix, and the radius goes through its absolute value. For a single linear layer this is the best box there is: every bound is reached by some input in the box.
Each rule is sound on its own, and soundness survives composition. If the box going into a layer contains every value that can arrive there, the box coming out contains every value that can leave. By induction over the layers, the final box contains every possible output. Every method in this series is proved sound with this same induction, so it pays to notice it once.
Running the network on boxesCopy link to section
Take . The input box has centre and radius .
First layer. The centre gives and the radius gives , so and . Both intervals are positive, so the ReLU changes nothing: and .
Second layer. The centre gives , and the radius gives . So and .
Every possible is at least and every possible is at most , so on the whole square. The network is robust at , and the proof took four products of a matrix with a vector.
Now take . Same computation: , , the ReLU still changes nothing, and
The two intervals overlap on . As far as the boxes know, could be while is , and the answer would flip. The method can't conclude.
The same computation in codeCopy link to section
In code a box is a small class. It stores its two ends, and it knows how to go through a matrix and through a ReLU:
class Box:
__array_ufunc__ = None # makes NumPy hand W @ box over to Box
def __init__(self, lo, hi):
self.lo = np.asarray(lo, dtype=float)
self.hi = np.asarray(hi, dtype=float)
def __rmatmul__(self, W): # W @ box: the centre-radius rule
mid = (self.lo + self.hi) / 2
rad = (self.hi - self.lo) / 2
return Box(W @ mid - np.abs(W) @ rad, W @ mid + np.abs(W) @ rad)
def relu(self): # ReLU is increasing: apply it to both ends
return Box(np.maximum(0, self.lo), np.maximum(0, self.hi))
def __repr__(self):
return f"Box(lo={self.lo.round(3)}, hi={self.hi.round(3)})"
def square(eps):
return Box(c - eps, c + eps)
out = net(square(0.05))
print(out, out.lo[0] > out.hi[1]) # Box(lo=[1.5 1. ], hi=[2.1 1.4]) True
out = net(square(0.1))
print(out, out.lo[0] > out.hi[1]) # Box(lo=[1.2 0.8], hi=[2.4 1.6]) FalseThe function net is the one from chapter 1, unchanged. When it computes W1 @ v and v is a box, the first line of the class tells NumPy to step aside, so Python asks the box instead and the box applies the centre-radius rule. The relu from chapter 1 sees that the box has its own ReLU and calls it. The same line that computes the network on a point now computes a box containing every output, and that is the whole verifier for this chapter.
Where the precision goesCopy link to section
We know from chapter 1 that the network is robust at , with a margin of at least everywhere. Something was lost on the way, and the figure shows where.
- Boxes, layer by layer: o₁ ∈ [1.20, 2.40], o₂ ∈ [0.80, 1.60]. The ranges overlap: no conclusion.
- The tightest box around the true outputs: o₁ ≥ 1.40, o₂ ≤ 1.40. Would still not prove it.
- The true sets. Smallest o₁ − o₂: 0.20, so the label never changes.
In general the boxes give and , so they prove robustness when , which means . The truth is . Between the two, the boxes fail for two different reasons.
First loss: the box forgets that the hidden values are relatedCopy link to section
Look at the middle panel at . The true hidden values fill a diamond: the first layer turns the input square by 45° and stretches it. The box around the diamond has twice its area, and the extra corners are values no input produces.
Take the corner . It's the one that gives the smallest , since . To get we need , so both inputs at their minimum, . But then , not . The two hidden values come from the same two inputs and can't be extreme in those directions at the same time. The box doesn't know that, because it records each variable on its own. That's what people mean when they say boxes are non relational.
A one-line version of the same problem: if , interval arithmetic computes , because it treats the two copies of as unrelated. This is called the dependency problem, and our network has it in disguise, since the margin subtracts two quantities that share their inputs.
This loss doesn't come from a careless rule. After the first layer the box is the best possible box. The trouble is that the second layer starts from the box and not from the diamond.
Second loss: even a perfect box is not enoughCopy link to section
Suppose an oracle handed us the tightest box around the true outputs, drawn dotted in the right panel. Where both hidden neurons are on, the outputs are
At , ranges over and over . The smallest is , at the input . The largest is also , at any input with . These are different inputs, but a box only keeps the two numbers, and is false. In general the perfect box gives and , so it proves robustness for and no further.
So we have three numbers:
| Proves robustness for | |
|---|---|
| Boxes, computed layer by layer | |
| The tightest box around the true outputs | |
| The network itself |
The first gap is the price of computing one layer at a time. The second is the price of the shape. The property is a relation between two outputs, and a box can't express any relation between its coordinates. No single box gets past , however cleverly it is computed.
Chapter 3 makes this distinction precise. There is a best approximation of each step, and there is a best approximation of the whole computation, and in general they are not the same thing.
Deeper networks: the wrapping effectCopy link to section
The first loss gets worse with depth. Picture a layer that only rotates its input by 45°. The true set stays a square of the same size, turning. A box can't turn, so after each layer it wraps the rotated box in a new box with sides parallel to the axes, times wider.
After 1 rotation the box is 1.41 times wider than the square it is supposed to describe, and 2 times larger in area.
After four layers the box is 4 times wider and 16 times larger in area than the set it describes, even though nothing in the network made anything bigger. Moore called this the wrapping effect. The matrices of a real network are not rotations, but they mix coordinates in the same way, and over dozens of layers the boxes on a normally trained network usually grow so wide that they prove nothing at all.
There is a way around this from the other side: train the network so that boxes work on it. Interval bound propagation training, proposed by Gowal and coauthors in 2018, puts the box bounds into the loss function, so training pushes towards networks on which interval arithmetic stays tight. It works much better than you would expect from such a crude analysis, and variants of it are still standard for training networks that can be certified. There's something odd about it, though: instead of finding a better proof method, you change the network until the crude one works.
Cutting the square into piecesCopy link to section
One more trick before giving up on boxes. Smaller boxes lose less, so cut the input square into pieces, run interval arithmetic on each piece, and declare victory if every piece is proved. If the property holds on each piece, it holds on their union.
4 × 4 = 16 boxes: 14 proved, 2 inconclusive.
At , pieces are enough. At you need . At you need . At no number of pieces works, because the margin is exactly zero at one corner and the property itself fails there. As approaches the true limit, the number of pieces explodes.
The code is a loop over the pieces:
def proved(box):
out = net(box)
return out.lo[0] > out.hi[1]
def proved_by_splitting(eps, k):
edges = np.linspace(-eps, eps, k + 1)
return all(proved(Box(c + [a1, b1], c + [a2, b2]))
for a1, a2 in zip(edges, edges[1:])
for b1, b2 in zip(edges, edges[1:]))
print(proved_by_splitting(0.12, 4), proved_by_splitting(0.12, 8)) # False TrueAnd this is with two inputs. With inputs, pieces per coordinate means boxes. For a 28 × 28 image, cutting the range of each pixel in two gives boxes, a number with 237 digits. Splitting is useful in practice, but only on the few coordinates or neurons that matter most, chosen with care, and never on all of them.
What boxes are good forCopy link to section
They are cheap. A layer costs two matrix products, one with for the centre and one with for the radius, so a whole network costs about two forward passes, even with millions of neurons. That makes them a good first pass. In particular they tell you which ReLUs are stable, meaning that their input interval doesn't contain 0. In our network is stable for every in the figures, and becomes unstable once goes past . Every more expensive method in this series starts by asking that question, because a stable ReLU is just a linear function.
Boxes can't remember that two neurons share a cause. The next shape, the zonotope, is built for exactly that: it keeps every value as an affine function of the inputs, so and never forget where they came from. Before building it, though, chapter 3 steps back and asks what we have been doing all along. What does it mean for a shape to approximate a set, what makes a rule for ReLU sound, and is there always a best rule?