Zonotopes: remembering where every value came from
Noise symbols and affine forms, why linear layers stop losing anything, the parallelogram that stands in for ReLU and why it is twice too big, and a zonotope verifier in thirty lines that reaches the true limit of our network.
· 13 min read · Chapter 4, Verifying neural networks
Contents
Chapter 3 found where boxes lose precision. Every time a layer mixes coordinates, the next layer receives a box instead of the true set, and the corners of that box are values that no input produces. The fix is to stop forgetting. Instead of storing each value as an interval, store a formula that says how it depends on the inputs, and let the formulas travel through the network.
The idea is older than deep learning. Comba and Stolfi proposed it in 1993 under the name affine arithmetic, as a cure for the dependency problem of interval arithmetic. Eric Goubault and Sylvie Putot built their analyzer Fluctuat on it, to track rounding errors in floating-point programs. In 2018 two analyzers from Martin Vechev's group at ETH Zürich, AI2 and then DeepZ, brought it to neural networks. The sets it computes are called zonotopes.
Noise symbolsCopy link to section
Write each input as its centre plus a deviation:
The 's are called noise symbols, and they are the only unknowns. Every value the network computes is a function of and , and as long as only linear layers are involved, it is an affine function:
An expression like this is an affine form: a centre, plus one coefficient per noise symbol. Its range is easy to read off. Push each symbol to whichever end makes the form smallest, then largest, and you find that ranges over , the same interval as with boxes. So far nothing has changed. The difference shows up in the next layer.
At both hidden neurons are on, so , and the margin is
The that comes from and the that comes from partly cancel, because they are the same symbol. The smallest value of is at , and it is , the exact answer from chapter 1. Boxes on and gave because they kept the two 's apart. The affine forms know they are the same one.
The dependency problem from chapter 2 goes away for the same reason. If , then exactly, because both copies carry the same , where interval arithmetic said .
It helps to see what the forms mean. A choice of noise values is one point in the square on the left of the next figure. It fixes the input, which fixes the hidden values and the outputs, so one point of the noise square gives one point in every layer.
η = (0.50, -0.50) gives x = (0.650, 0.350), then h = (1.000, 0.300) and o = (1.700, 1.300).
Margin: 0.6 − 0.1·η₁ + 0.3·η₂ = 0.6 − 0.050 − 0.150 = 0.400
What a zonotope isCopy link to section
Stack the affine forms of a layer into a vector and you get , where holds the centres and has one column per noise symbol. The columns are called generators. The set of all values of , as ranges over the cube , is a zonotope. Put differently, a zonotope is the image of a cube by an affine map.
In two dimensions a zonotope is a polygon whose opposite sides are parallel, symmetric about its centre. Each generator contributes one pair of parallel sides: one generator gives a segment, two give a parallelogram, three a hexagon.
3 noise symbols: 3 pairs of parallel sides, and the shape is symmetric around its centre.
Boxes lacked one property that zonotopes have. A linear layer sends to . Multiply the centre and the generators by and you have the image of the zonotope, with nothing added. The junctions between layers, where chapter 3 found all the losses of boxes, cost nothing anymore.
ReLU, where the loss moves toCopy link to section
A ReLU whose input range doesn't contain zero is easy. If the neuron is always on, and ReLU changes nothing. If it is always off, and the output is the constant 0. Both cases are affine, so both are exact.
The trouble is an unstable neuron, with . Its output is not an affine function of the noise symbols, so it has to be covered by one, and the cover must contain the whole graph of ReLU between and . In our network this happens to as soon as , since ranges over .
The crude cover forgets the input and says that is somewhere in . That's the box of chapter 2. A zonotope can do better, because it can tilt. Take two parallel lines with slope , one under the graph and one over it, and keep everything between them:
Any slope between 0 and 1 keeps the lower line under the graph. The upper line starts at and has to pass over , which needs . The area between the lines grows with , so the best choice is the smallest slope allowed,
for which the upper line goes through both ends of the graph. As an affine form, the parallelogram is
where is a fresh noise symbol that accounts for the thickness of the parallelogram, . Every unstable neuron adds one noise symbol, so the zonotope gains one pair of sides per unstable neuron.
z₂ ranges over [-0.20, 0.60], slope λ = 0.750, half height μ = 0.075.
- Box: area 0.480
- Zonotope parallelogram: area 0.120
- Triangle, the smallest convex cover: area 0.060
- Part of the parallelogram below zero, where no ReLU output can be
The three covers in the figure have areas that follow a simple pattern. The box has area . The parallelogram has area . The smallest convex set that contains the graph, the triangle with corners , and , has area . Whatever and are, the parallelogram is exactly twice the triangle. It's one of my favourite small facts in this series.
So why not use the triangle? Because its sides don't come in parallel pairs, and the sides of a zonotope always do. A zonotope is symmetric about its centre, the graph of ReLU isn't, and the parallelogram is the price of that symmetry. It also explains the hatched corner: to stay symmetric, the lower side has to dip below zero, so the zonotope allows ReLU outputs that are negative. A box never does that.
Running it on our networkCopy link to section
Take . Then ranges over , so is unstable, with and . A new symbol appears:
The neuron is still always on, so , and the margin is
Its smallest value is , which is , the exact answer again. The same computation works for every : zonotopes prove robustness all the way up to , the true limit, where boxes stopped at , or at with the margin trick of chapter 3.
That looks too good, given how loose the parallelogram is. Here is why the looseness doesn't matter. The margin subtracts , so its smallest value needs the largest , which comes from the upper side of the parallelogram. That side touches the graph at . And the worst input, , is exactly where . The cover is loose in the middle and tight at the one end that this property cares about.
- What the network can output. Always label A: o₁ − o₂ is at least 0.04.
- Zonotope (chapter 4): stays out of the hatched region, so it proves the label.
- Box (chapter 2): touches the hatched region, so it can't conclude.
- Where o₂ ≥ o₁, so the label is no longer A.
In codeCopy link to section
A zonotope is a centre and a generator matrix, and the class follows the box of chapter 2 almost line for line:
class Zonotope:
__array_ufunc__ = None
def __init__(self, center, gens):
self.c = np.asarray(center, dtype=float) # the centres, one per neuron
self.G = np.asarray(gens, dtype=float) # one column per noise symbol
def __rmatmul__(self, W): # linear layers are exact
return Zonotope(W @ self.c, W @ self.G)
def bounds(self):
r = np.abs(self.G).sum(axis=1)
return self.c - r, self.c + r
def relu(self):
lo, hi = self.bounds()
c, G, fresh = self.c.copy(), self.G.copy(), []
for i in range(len(c)):
if lo[i] >= 0: # always on: nothing to do
continue
if hi[i] <= 0: # always off: the output is 0
c[i], G[i] = 0.0, 0.0
continue
lam = hi[i] / (hi[i] - lo[i]) # slope of the parallelogram
mu = -lam * lo[i] / 2 # half of its height
c[i], G[i] = lam * c[i] + mu, lam * G[i]
new = np.zeros(len(c))
new[i] = mu # a new noise symbol
fresh.append(new)
return Zonotope(c, np.column_stack([G] + fresh))
def __repr__(self):
lo, hi = self.bounds()
return f"Zonotope(lo={lo.round(3)}, hi={hi.round(3)}, symbols={self.G.shape[1]})"
def zsquare(eps):
return Zonotope(c, eps * np.eye(2)) # x = c + eps * eta
print(margin_net(zsquare(0.12))) # Zonotope(lo=[0.12], hi=[1.073], symbols=3)
for eps in (0.14, 0.149, 0.151):
print(eps, margin_net(zsquare(eps)).bounds()[0].round(3))
# 0.14 [0.04]
# 0.149 [0.004]
# 0.151 [-0.004]The margin_net function is the one from chapter 3, and net is the one from chapter 1. Neither changed. The verifier is still the same line of code, running on a new kind of value. Two more checks take a few lines each: the dependency problem is gone, and the soundness test from chapter 3 still passes.
x = Zonotope([0.5], [[0.5]]) # x in [0, 1]
print(np.array([[1.0, -1.0]]) @ (np.array([[1.0], [1.0]]) @ x))
# Zonotope(lo=[0.], hi=[0.], symbols=1)
def zonotope_looks_sound(eps, n=10_000):
lo, hi = margin_net(zsquare(eps)).bounds()
xs = c + rng.uniform(-eps, eps, size=(n, 2))
ms = np.array([margin(x) for x in xs])
return bool(np.all(ms >= lo) and np.all(ms <= hi))
print([zonotope_looks_sound(e) for e in (0.05, 0.12, 0.2)]) # [True, True, True]Where zonotopes loseCopy link to section
Robustness came out exact, but that was the property being kind to us. Look at the hidden values themselves at :
print(relu(W1 @ zsquare(0.12))) # Zonotope(lo=[ 0.76 -0.037], hi=[1.24 0.44], symbols=3)
print(relu(W1 @ square(0.12))) # Box(lo=[0.76 0. ], hi=[1.24 0.44])The zonotope says that might be . That's the hatched corner of the parallelogram: a value no ReLU can output, and one the box rules out. Neither shape wins here. The zonotope knows how and move together, and the box knows that . One cheap fix is to run both analyses and keep, for each neuron, the tighter of the two bounds. Combining domains this way is called a reduced product.
The dip matters as soon as a property needs a lower bound. Suppose that, besides the label, we want to know how low the score can go at . Since , the true minimum is , reached at .
o2_row = W2[1:] # the row of W2 that computes o2
print(o2_row @ relu(W1 @ square(0.2))) # Box(lo=[0.6], hi=[2.])
print(o2_row @ relu(W1 @ zsquare(0.2))) # Zonotope(lo=[0.75], hi=[1.7], symbols=3)The zonotope gets closer than the box, but it stops at . The minimum of needs the smallest , which comes from the lower side of the parallelogram, and that side runs under the graph of ReLU. At the input , for example, , so the true is , but the parallelogram allows anything down to .
The triangle wouldn't lose anything there. ReLU is a convex function, so the lower edge of the triangle is the graph of ReLU itself: from below, the best convex cover is exact. A zonotope can't have that edge, because its sides must come in parallel pairs.
Adding constraintsCopy link to section
There is a way to get the triangle while keeping most of the machinery: allow linear constraints on the noise symbols. A constrained zonotope is a set of the form
For an unstable ReLU, keep the parallelogram and add the two constraints it is missing, and , written in terms of the 's. That cuts the parallelogram down to the triangle. Linear layers are still exact, since they only touch and , and with enough constraints a constrained zonotope can describe any bounded polyhedron.
The catch is in the bounds. The range of a coordinate is no longer a sum of absolute values. It's the solution of a linear program, one for each bound. For our two neurons that costs nothing. For a network with thousands of unstable neurons it means thousands of linear programs per layer.
A word about roundingCopy link to section
Every number in this code is a floating-point number, and almost every operation rounds. Print the lower bound of that boxes computed at with all its digits, and you get , not . Here the rounding went the safe way, slightly down for a lower bound. Nothing guarantees that it always will, and on a property that holds by a margin of , rounding in the wrong direction would turn "I don't know" into "proved".
This is not paranoia. In 2021 Jia and Rinard showed that floating-point error can make a verifier certify a network as robust when an adversarial input exists inside the certified region. A careful verifier rounds every lower bound down and every upper bound up. Interval libraries do this with the rounding modes of the processor (IntervalArithmetic.jl, in Julia, is a good example). On our network the margins are far too large for any of this to matter, but every bound a real tool reports should come with outward rounding.
NextCopy link to section
Chapter 5 looks for a middle ground between the zonotope and the triangle. DeepPoly and CROWN keep, for every neuron, one linear function below it and one above it, written in terms of the neurons of the previous layer. That is enough to use the triangle's lower edge, or , without solving any linear program, and it finds exactly on the bound where zonotopes stopped at . The new difficulty is the direction: to get good bounds, those linear functions have to be carried back through the layers until they reach the inputs.