Abstract interpretation: being wrong in the safe direction
What it means for a shape to approximate a set, the best rule for each layer, why the best rules don't compose into the best analysis, and a free fix for one of the two losses of chapter 2.
· 15 min read · Chapter 3, Verifying neural networks
Contents
Chapter 2 ended with three questions. What does it mean for a shape to approximate a set? What makes a rule for ReLU sound? Is there always a best rule?
Patrick and Radhia Cousot worked out the general answers in the late 1970s, for computer programs, and called the theory abstract interpretation. It is the theory behind static analyzers such as Astrée, which was used to prove that the flight control software of the Airbus A340 and A380 is free of run-time errors like overflows and divisions by zero. The theory has a reputation for being hard. As far as I can tell, most of that reputation comes from loops. A loop can run any number of times, so an analyzer has to find a description that survives one more iteration, and computing those fixpoints is where the heavy lattice theory comes in. A feedforward network has no loops. Each layer runs once, and what's left of the theory is short. You already used most of it in chapter 2 without the names.
The names are worth learning anyway. With them, the two losses of chapter 2 become two precise statements, and one of them turns out to cost nothing to fix.
Two worldsCopy link to section
Every method in this series works in two worlds at once.
The concrete world is made of sets of points: the inputs within of , and everything the network makes of them, layer after layer. These sets are what we want to know about. They are exact, but we can't compute with them directly, because most of them are infinite and their shapes get complicated after a layer or two.
The abstract world is made of descriptions we can compute with. For now that means boxes. A box in is stored as numbers, and chapter 2 gave a rule to push one through each layer.
Two maps go between the worlds. The first one says what a box means:
The second one finds the best description of a set:
Its interval in coordinate runs from the smallest to the largest value of over the points of . The letters are traditional: for abstraction, for concretization.
A box is a sound description of when . That inclusion is what the title means by being wrong in the safe direction. The box is allowed to contain points that the network never produces. It must never miss one.
Precision is an orderCopy link to section
Boxes can be compared. Write when sits inside , one interval inside the other in every coordinate. If two boxes both describe the same set soundly, the smaller one rules out more outputs, so it can prove more. Inclusion is an order of precision, and lower is better.
Two boxes don't always compare, since one can be wider and the other taller. But any two boxes have a smallest box above both, their join , and a largest box below both, their meet , which is their intersection. An order in which every pair has a join and a meet is called a lattice. Sets of points form a lattice too, ordered by inclusion, with union as the join and intersection as the meet.
The two joins differ, and the difference matters. The union of two boxes is rarely a box, so the join of two boxes has to add points that are in neither. A program analyzer pays this every time the two branches of an if meet again. A network pays it if you treat each ReLU as a branch, negative input on one side and positive input on the other, analyse both and merge the results. Chapters 7 and 8 are about not merging.
The Galois connectionCopy link to section
Here is the definition the whole theory rests on. The pair is a Galois connection when, for every set and every box ,
Read it as: a box contains exactly when it contains the best box of . For boxes this is almost obvious, because a box contains exactly when each of its intervals covers the range of the matching coordinate over , and those ranges are the intervals of . You can still try to break it. In the figure, is the set of hidden values at , the diamond from chapter 2.
Short as it is, the equivalence carries a lot. Put : the left side holds trivially, so the right side holds too, and . The best box is sound. Put : now the right side holds trivially, so , and describing a box by a box gives nothing worse back (for boxes, you get itself). Finally, every box that contains contains , so the best box is the most precise sound description, not merely a good one.
There is also a formula for hiding in the definition. The boxes that contain are exactly the boxes above , so is their intersection:
This only works because an intersection of boxes, even of infinitely many of them, is again a box. Keep that in mind for the end of the chapter.
The general theory replaces boxes with any lattice of descriptions, called an abstract domain, and asks for the same equivalence. Boxes are known as the interval domain.
The best rule for one layerCopy link to section
Take one step of the network, a function : a linear layer, or ReLU. It sends a set to the set of all the with . We want a rule on boxes that is sound, which means
The Galois connection gives the best such rule directly: take the points of the box, apply to all of them, and take the best box of the result.
It is sound, because contains for every set , in particular for . And no sound rule does better. If is another sound rule, then , and the Galois connection turns that into , which says . Two lines, and they hold for any at all.
The formula says what the best rule is, not how to compute it. For the two kinds of layers in our network, computing it is easy.
For a linear layer , take one output coordinate, . Over a box it is smallest when each sits at its lower end if and at its upper end if , and largest at the opposite corner. Doing this for each gives the centre and radius rule of chapter 2, so that rule was already the best one. The corner depends on , though. For our , the smallest comes from the bottom left corner of the input square, and the smallest from the top left corner. That's why a linear map turns a box into a slanted box (a parallelogram, in the plane), and why the best box around the result is bigger than the result.
For ReLU there's even less to do. It acts on each coordinate separately and it's increasing, so it sends the interval onto , and a box onto a box. The rule from chapter 2 is the best one again, and it's better than best: it's exact. describes without a single extra point.
The dashed box has 2.00 times the area of the shape inside it.
The shape is still a box, so the dashed box is the shape itself.
I find this asymmetry the most useful thing in the chapter. With boxes, ReLU costs nothing. The cost is in mixing: a linear layer that combines coordinates turns a box into a slanted shape, and the box around that shape contains points that no input produces.
You can see those points in code. Take the hidden box at and ask, for each corner, which input would produce it. Both hidden neurons are on there, so the answer is the solution of :
H = relu(W1 @ square(0.1))
print(H) # Box(lo=[0.8 0. ], hi=[1.2 0.4])
for h in [(0.8, 0.0), (1.2, 0.0), (0.8, 0.4), (1.2, 0.4)]:
x = np.linalg.solve(W1, h) # the input that would give h
print(h, x, bool(np.all(np.abs(x - c) <= 0.1)))
# (0.8, 0.0) [0.4 0.4] False
# (1.2, 0.0) [0.6 0.6] False
# (0.8, 0.4) [0.6 0.2] False
# (1.2, 0.4) [0.8 0.4] FalseNone of the four corners comes from the input square. The second layer will treat them as possible hidden values anyway.
Best layers, worse networkCopy link to section
We have the best rule for every layer. Is the composition of those rules the best rule for the network?
Our network is . Interval arithmetic computes
while the best rule for the whole network is
The only difference is the between consecutive layers. Each one replaces a set by all the points of its best box, which is a bigger set, and every step after it is monotone. So the composition is still sound, but it can only lose: for every box .
To see where it actually loses, draw both computations, with sets of points on top and boxes underneath. stands for sets of points in the plane and for boxes.
Each square compares two routes from a set in its top left corner to a box in its bottom right corner: apply the layer and then take the best box, or take the best box first and then apply the box rule. The symbol in the middle says how they compare. In general , because the second route went through , which contains . For ReLU the two routes always agree, by the previous section.
Now follow the input square through the diagram. In the first square both routes agree, because the input square is already a box. In the ReLU square they agree because they always do. In the last square they split: receives the box around the hidden values, phantom corners included, instead of the hidden values themselves. That is the first loss of chapter 2, now with an exact address. It happens at the junction between the ReLU and , where a layer that mixes coordinates is fed a box. A deeper network has one such junction per layer, and the losses compound into the wrapping effect.
This picture, with squares that only commute up to , comes back in chapter 14, where category theory has a name for it.
Asking about the marginCopy link to section
Now the second loss. Chapter 2 showed that even the best box around the true outputs stops proving robustness at , and I wrote that no box gets past , however cleverly it is computed. That's true for boxes on and . But the property doesn't care about and separately. It's about the margin , which is a single number, and in one dimension a box is an interval, which describes the range of a number with nothing to spare.
So add one more linear layer at the end of the network that computes , and ask whether . Since
the last two linear maps merge into a single row, . Interval arithmetic on this network uses the same hidden box as before and gives
so it proves robustness up to , from the same hidden box that gave only before. In code the change is one matrix product:
spec = np.array([[1.0, -1.0]]) # the property: o1 - o2 > 0
W_margin = spec @ W2 # [[ 1. -2.]]
def margin_net(v):
return W_margin @ relu(W1 @ v)
out = net(square(0.08))
print(round(out.lo[0] - out.hi[1], 3)) # -0.2: boxes on o1 and o2 can't tell
print(margin_net(square(0.08)).lo) # [0.12]: the margin is positive, provedThis is the dependency problem of chapter 2 again. Computing and as two separate intervals and then subtracting treats the inside and the inside as unrelated. Merging the layers first lets them cancel before any interval is taken. Verification tools do this routinely, and from now on every method in the series gets the margin as its output.
The best interval for , the exact range of the margin over the input square, starts at , as chapter 1 computed. It's positive exactly when , the true limit. The four numbers fit in a table.
| Best rule for each layer, composed | Best rule for the whole network | |
|---|---|---|
| Boxes on and | ||
| An interval on the margin |
Chapter 2 went along the top row and then down the right column, and called the two steps the price of composing and the price of the shape. You can also go down first. Going down costs nothing, one extra row in the last matrix. Going right is the expensive direction. The bottom right cell is the exact answer, and that's no accident: for a property about a single number, the best interval for the whole network is the verification problem itself, which chapter 1 called NP-complete.
So the right column is out of reach, and the left column is what we can compute. The rest of the series is about closing the gap between them with shapes that lose less at each junction.
Testing a verifierCopy link to section
One practical point before moving on. A verifier is itself a program, and programs have bugs. A wrong rule for ReLU would quietly turn every "proved" into a lie. Testing can't show that a rule is sound, but it can catch rules that aren't, exactly like attacks in chapter 1: push random points through the real network and check that every output lands inside the computed box.
rng = np.random.default_rng(0)
def looks_sound(eps, n=10_000):
box = net(square(eps))
xs = c + rng.uniform(-eps, eps, size=(n, 2))
outs = np.array([net(x) for x in xs])
return bool(np.all(outs >= box.lo) and np.all(outs <= box.hi))
print([looks_sound(e) for e in (0.05, 0.1, 0.2)]) # [True, True, True]A False here would be a bug in the verifier, never in the network. It's the cheapest test there is, and every new kind of value in this series gets it.
When there is no bestCopy link to section
All of this leaned on , the best box of a set, which always exists. For other shapes it may not.
Try describing a disk with polygons (in more dimensions, convex polyhedra). The best box around a disk is the square that touches it on four sides. A polygon can do better: cut off a corner of the square with a line tangent to the disk, and you get a smaller polygon that still contains the disk. Then cut a corner of that one.
- The disk.
- Its best box, the square that touches it on four sides.
- A polygon around the disk with 5 sides, 22% larger than the disk.
The cutting never stops, so no polygon around the disk is the smallest, and doesn't exist. The formula from earlier fails at its last step: intersect all the polygons that contain the disk and you get the disk itself, which is not a polygon. Boxes have a best description of every set precisely because intersections of boxes are boxes. Polygons are more expressive than boxes, and the price is that nothing is ever the best. Zonotopes, the shapes of the next chapter, are in the same position.
The theory survives without . We keep , which still says what each shape means, and ask every rule to be sound, , and nothing more. Soundness still composes by the induction of chapter 2, so analysing the whole network is still a proof. We lose the notion of a best rule. A layer can have many sound rules, none of them better than all the others, and choosing one becomes a design decision that trades cost against precision. Chapters 4 and 5 are mostly about such decisions for ReLU.
NextCopy link to section
We now know what to look for: shapes that lose less at the junctions between layers. With boxes, ReLU was free and mixing was expensive. Zonotopes swap the two. A linear layer maps a zonotope to a zonotope exactly, so mixing costs nothing, and all the loss moves into the ReLUs that can switch. Our network has only one of those, and chapter 4 shows that zonotopes reach the true limit, , with the same net function and a new kind of value.