A question about infinitely many inputs
The toy network used throughout the series, the robustness property we want to prove about it, why random tests and attacks can't prove it, and the trade every method makes instead.
· 10 min read · Chapter 1, Verifying neural networks
Contents
Before looking at any method, we need a network to try it on and a precise statement of what we want to prove. For this one network we can even work out the exact answer. For networks in general we can't, and the end of the chapter explains why.
The networkCopy link to section
It has two inputs, and . The hidden layer computes
and the outputs are
The network answers A when , and B otherwise. In matrix form, with
There are no biases, to keep the arithmetic light. Nobody trained this network. I chose the weights so that the numbers come out round and so that every method in the series has something to get wrong.
Take the input . Then , , and the answer is A. The gap will come up constantly, so it deserves a name: the margin. A positive margin means A.
Here is the same network in Python. Every chapter adds a few lines to this code, and together they make a small verifier.
import numpy as np
W1 = np.array([[1.0, 1.0],
[1.0, -1.0]])
W2 = np.array([[2.0, -1.0],
[1.0, 1.0]])
c = np.array([0.6, 0.4])
def relu(v):
return v.relu() if hasattr(v, "relu") else np.maximum(0, v)
def net(v):
return W2 @ relu(W1 @ v)
def margin(x):
o = net(x)
return o[0] - o[1]
print(net(c), round(margin(c), 3)) # [1.8 1.2] 0.6The odd-looking relu is there for chapter 2, where v will be something other than an array. For now it is just np.maximum(0, v).
The questionCopy link to section
Suppose the two inputs are measurements, and measurements are noisy: each one might be off by up to some . Does the network still answer A? Written out, we want
The allowed inputs form a square of side centred at . With more inputs it becomes a cube, and for images it is the usual setting where every pixel may change by at most . This property is called local robustness. There are other ways to measure how far an input moved, but the square is the one the first methods handle best, so we'll keep it.
People in verification like to write such a property as a triple, with an assumption on the left, the program in the middle and a promise on the right:
Here is the larger of and . The left part is the precondition, the right part the postcondition, and the triple says that every input satisfying the first gives an output satisfying the second. The rest of the series is about proving triples like this one.
Writing the right triple is harder than it looks. A natural first try at robustness drops the reference point: "any two inputs closer than get the same label". It sounds reasonable and it's a disaster. Chain enough small steps and you can walk from any input to any other, so the only networks that satisfy it give every input the same label. Anchoring the property at one input avoids this, at the price of proving it one input at a time.
The question looks innocent. The difficulty is the "for all": the square contains infinitely many inputs.
First attempt: try inputsCopy link to section
The obvious move is to test a lot of inputs. You can do it yourself here. The slider sets , each button tests random inputs from the square, and inputs where the answer stopped being A are circled.
No inputs tried yet.
At the network is not robust. There is a thin triangle in the bottom right corner of the square where the answer flips, and it covers 0.26% of the square. A batch of 100 random tests misses it about three times out of four. Even 1,000 tests miss it 7% of the time. In code:
rng = np.random.default_rng(0)
eps = 0.16
xs = c + rng.uniform(-eps, eps, size=(100, 2))
print(round(min(margin(x) for x in xs), 3)) # 0.022: still positive, nothing foundIn two dimensions you could cover the square with a fine grid and be done. That stops working fast. A grid with ten points per coordinate has points in dimension , and a small 28 × 28 greyscale image already has . There is a second problem. When a property only just fails, it tends to fail near a corner of the cube, because a linear function takes its smallest value on a cube at a corner. In high dimensions, random samples almost never land near a corner.
Second attempt: search on purposeCopy link to section
Random tests ignore what they learn. An attack uses it. The margin is a function of the input, so it has a gradient, and walking downhill along the gradient leads to the worst input nearby. The panda in the preface became a gibbon this way. For our network it takes a few lines:
def margin_grad(x):
on = W1 @ x > 0 # which hidden neurons are active
return (W2[0] - W2[1]) @ (on[:, None] * W1)
x = c.copy()
for step in range(30):
x = x - 0.01 * np.sign(margin_grad(x)) # a small step downhill
x = np.clip(x, c - eps, c + eps) # without leaving the square
print(x, round(margin(x), 3)) # [0.76 0.24] -0.04Taking the sign of the gradient moves every coordinate by the same small step, which suits a square, since its corners are where linear functions are smallest. The attack walks straight to the bad corner in 16 steps, where 100 random tests usually see nothing. The attacks used on real networks, such as projected gradient descent (PGD), are this loop with more care about step sizes, plus random restarts.
So attacks are much better than random tests at finding problems. They can't show that there are none, though. Run the same loop at and it stops at with margin . Is the smallest margin in the square, or did the attack miss a deeper valley? On this network it is the smallest, but nothing in the attack tells you so.
It helps to think of the smallest margin over the square as an unknown number . Every input you try, by luck or by gradient, gives an upper bound: is at most the margin you just saw. A proof gives a lower bound. If the lower bound is positive, the network is robust. If some input has a negative margin, it is not. In between, all you can say is that you don't know yet.
Lower bound -0.20, best attack 0.28. Unknown: the lower bound is negative, and the attack found no input that flips the label.
Verification is the business of pushing that lower bound up. That needs a different way of looking at the network.
Third attempt: think in setsCopy link to section
Instead of pushing points through the network, push the whole square. Call the square and the network , and write
for the set of every output the network can produce on it. The answer is A exactly on the safe set
and the property becomes a statement about two sets:
That inclusion is what we'll be trying to prove for the rest of the series.
For our network we can compute exactly. The two ReLUs cut the input plane into regions where the network is linear. Where both hidden neurons are on, the margin is
Where is off, that is when , the margin is just , which is positive for every input we look at. So the worst input is the corner where is largest and smallest, , and there the margin is
The network is robust exactly when . Every method in the series will be chasing that number.
This is itself. It's a polygon, and the slider shows it touching the diagonal at .
- What the network can output. Always label A: o₁ − o₂ is at least 0.12.
- Where o₂ ≥ o₁, so the label is no longer A.
Why this doesn't scaleCopy link to section
Our network has two ReLUs, so the input plane splits into at most four linear regions, and we can handle each one by hand. A network with ReLUs has up to on/off patterns, and every pattern that actually occurs gives a region where the network is linear. Each ACAS Xu network has 300 ReLUs, and is larger than the number of atoms in the observable universe. Most of those patterns never occur, but nobody knows in advance which ones do.
Katz and his coauthors showed in 2017 that checking properties like ours on ReLU networks is NP-complete. So some instances will be expensive whatever we do. The methods in chapters 2 to 6 deal with this by changing the question slightly.
The trade: a bigger setCopy link to section
Instead of , compute a set that contains it, is easy to compute, and is easy to compare with . Then
If the big set is safe, so is the true one. If the big set is not inside , we learn nothing: maybe the network is unsafe, maybe was too fat.
A method that works like this is sound: when it says "proved", it is right. It is not complete, because it sometimes says "I don't know" about a network that is fine. The middle panel is the price we pay for soundness. Most of this series is about making that case rarer without paying too much for it.
The other two outcomes need other tools. Real counterexamples come from attacks, and from the exact solvers of chapter 7. Chapter 11 turns the picture inside out and computes sets that are guaranteed to lie inside , which is how you prove that bad outputs really happen.
Beyond robustnessCopy link to section
Robustness is one case of a general pattern: when the input is in some set , the output should be in some set . For ACAS Xu, describes a geometry, say "the intruder is far away and much slower than us", and a constraint on the scores, say "the score of clear of conflict stays below a threshold". The question is always whether , and everything in the series applies to it.
When the network steers a car or a drone, the property is about time instead: the set of states goes through the network, then through the physics, then through the network again, over and over. That's chapter 12.
NextCopy link to section
The cheapest anyone has come up with is a box, one interval per variable. In chapter 2 boxes prove our network robust up to . The truth is , and the gap has two separate causes.