Skip to content
← Notes

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 YY 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 nn dimensions it's a product of nn 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: [a,b]+[c,d]=[a+c, b+d][a, b] + [c, d] = [a + c,\ b + d].

Multiplying by a number ww scales the ends, and swaps them when ww is negative: w [a,b]=[wa, wb]w\,[a, b] = [wa,\ wb] if w≥0w \geq 0, and [wb, wa][wb,\ wa] if w<0w \lt 0.

ReLU is increasing, so it can be applied to each end: ReLU⁡[a,b]=[max⁡(0,a), max⁡(0,b)]\operatorname{ReLU}[a, b] = [\max(0, a),\ \max(0, b)].

For a whole linear layer z=Wx+bz = Wx + b these rules combine into one formula that is easier to remember. Describe the input box by its centre mm and its radius rr, so that xjx_j ranges over [mj−rj, mj+rj][m_j - r_j,\ m_j + r_j]. Then

z∈[ Wm+b−∣W∣ r,  Wm+b+∣W∣ r ],z \in \big[\, Wm + b - |W|\,r,\ \ Wm + b + |W|\,r \,\big],

where ∣W∣|W| is WW 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 ε=0.05\varepsilon = 0.05. The input box has centre c=(0.6, 0.4)c = (0.6,\ 0.4) and radius (0.05, 0.05)(0.05,\ 0.05).

First layer. The centre gives W1c=(1, 0.2)W_1 c = (1,\ 0.2) and the radius gives ∣W1∣ r=(0.1, 0.1)|W_1|\,r = (0.1,\ 0.1), so z1∈[0.9, 1.1]z_1 \in [0.9,\ 1.1] and z2∈[0.1, 0.3]z_2 \in [0.1,\ 0.3]. Both intervals are positive, so the ReLU changes nothing: h1∈[0.9, 1.1]h_1 \in [0.9,\ 1.1] and h2∈[0.1, 0.3]h_2 \in [0.1,\ 0.3].

Second layer. The centre gives W2(1, 0.2)=(1.8, 1.2)W_2 (1,\ 0.2) = (1.8,\ 1.2), and the radius gives ∣W2∣ (0.1, 0.1)=(0.3, 0.2)|W_2|\,(0.1,\ 0.1) = (0.3,\ 0.2). So o1∈[1.5, 2.1]o_1 \in [1.5,\ 2.1] and o2∈[1.0, 1.4]o_2 \in [1.0,\ 1.4].

Every possible o1o_1 is at least 1.51.5 and every possible o2o_2 is at most 1.41.4, so o1>o2o_1 \gt o_2 on the whole square. The network is robust at ε=0.05\varepsilon = 0.05, and the proof took four products of a matrix with a vector.

Now take ε=0.1\varepsilon = 0.1. Same computation: z1∈[0.8, 1.2]z_1 \in [0.8,\ 1.2], z2∈[0, 0.4]z_2 \in [0,\ 0.4], the ReLU still changes nothing, and

o1∈[1.2, 2.4],o2∈[0.8, 1.6].o_1 \in [1.2,\ 2.4], \qquad o_2 \in [0.8,\ 1.6].

The two intervals overlap on [1.2, 1.6][1.2,\ 1.6]. As far as the boxes know, o1o_1 could be 1.21.2 while o2o_2 is 1.61.6, 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]) False

The 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 ε=0.1\varepsilon = 0.1, with a margin of at least 0.20.2 everywhere. Something was lost on the way, and the figure shows where.

1. Inputs0.40.60.80.20.40.6x₁x₂2. After the ReLU layer0.511.5-0.300.30.6h₁h₂3. Outputs11.522.50.511.522.5o₁o₂
  • 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.
The same input square pushed through the network twice: exactly (filled) and with interval arithmetic (dashed). Hatched regions are the inputs, hidden values and outputs that end in a flipped label. In the middle panel, the dotted outline is the part of the set that the ReLU flattens onto h₂ = 0.

In general the boxes give o1≥1.8−6εo_1 \geq 1.8 - 6\varepsilon and o2≤1.2+4εo_2 \leq 1.2 + 4\varepsilon, so they prove robustness when 1.8−6ε>1.2+4ε1.8 - 6\varepsilon \gt 1.2 + 4\varepsilon, which means ε<0.06\varepsilon \lt 0.06. The truth is ε<0.15\varepsilon \lt 0.15. Between the two, the boxes fail for two different reasons.

Look at the middle panel at ε=0.1\varepsilon = 0.1. 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 (h1,h2)=(0.8, 0.4)(h_1, h_2) = (0.8,\ 0.4). It's the one that gives the smallest o1o_1, since 2×0.8−0.4=1.22 \times 0.8 - 0.4 = 1.2. To get h1=0.8h_1 = 0.8 we need x1+x2=0.8x_1 + x_2 = 0.8, so both inputs at their minimum, x=(0.5, 0.3)x = (0.5,\ 0.3). But then h2=x1−x2=0.2h_2 = x_1 - x_2 = 0.2, not 0.40.4. 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 x∈[0,1]x \in [0, 1], interval arithmetic computes x−x∈[−1,1]x - x \in [-1, 1], because it treats the two copies of xx as unrelated. This is called the dependency problem, and our network has it in disguise, since the margin h1−2h2h_1 - 2h_2 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

o1=2(x1+x2)−(x1−x2)=x1+3x2,o2=(x1+x2)+(x1−x2)=2x1.o_1 = 2(x_1 + x_2) - (x_1 - x_2) = x_1 + 3x_2, \qquad o_2 = (x_1 + x_2) + (x_1 - x_2) = 2x_1 .

At ε=0.1\varepsilon = 0.1, o1o_1 ranges over [1.4, 2.2][1.4,\ 2.2] and o2o_2 over [1.0, 1.4][1.0,\ 1.4]. The smallest o1o_1 is 1.41.4, at the input (0.5, 0.3)(0.5,\ 0.3). The largest o2o_2 is also 1.41.4, at any input with x1=0.7x_1 = 0.7. These are different inputs, but a box only keeps the two numbers, and 1.4>1.41.4 \gt 1.4 is false. In general the perfect box gives o1≥1.8−4εo_1 \geq 1.8 - 4\varepsilon and o2≤1.2+2εo_2 \leq 1.2 + 2\varepsilon, so it proves robustness for ε<0.1\varepsilon \lt 0.1 and no further.

So we have three numbers:

Proves robustness for
Boxes, computed layer by layerε<0.06\varepsilon \lt 0.06
The tightest box around the true outputsε<0.10\varepsilon \lt 0.10
The network itselfε<0.15\varepsilon \lt 0.15

The first gap is the price of computing one layer at a time. The second is the price of the shape. The property o1>o2o_1 \gt o_2 is a relation between two outputs, and a box can't express any relation between its coordinates. No single box gets past 0.10.1, 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, 2\sqrt 2 times wider.

-4-2024-4-2024first coordinatesecond coordinate

After 1 rotation the box is 1.41 times wider than the square it is supposed to describe, and 2 times larger in area.

Each layer turns its input by 45°. Filled: where the points really are. Dashed: the box we keep. Dotted: the previous box after rotation, which is what the next box has to wrap.

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.

0.50.60.70.30.40.5x₁x₂
ε
pieces per side

4 × 4 = 16 boxes: 14 proved, 2 inconclusive.

Each piece goes through interval arithmetic on its own. Blue pieces are proved, grey ones are not. The small hatched corner at the bottom right is where the label really flips; at ε = 0.15 the square would touch it.

At ε=0.10\varepsilon = 0.10, 4×4=164 \times 4 = 16 pieces are enough. At ε=0.12\varepsilon = 0.12 you need 8×8=648 \times 8 = 64. At ε=0.14\varepsilon = 0.14 you need 32×32=1,02432 \times 32 = 1{,}024. At ε=0.15\varepsilon = 0.15 no number of pieces works, because the margin is exactly zero at one corner and the property itself fails there. As ε\varepsilon 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 True

And this is with two inputs. With dd inputs, kk pieces per coordinate means kdk^d boxes. For a 28 × 28 image, cutting the range of each pixel in two gives 27842^{784} 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 WW for the centre and one with ∣W∣|W| 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 h1h_1 is stable for every ε\varepsilon in the figures, and h2h_2 becomes unstable once ε\varepsilon goes past 0.10.1. 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 h1h_1 and h2h_2 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?