Skip to content
← Notes

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

111−12−111x₁0.6x₂0.4h₁1.0h₂0.2o₁1.8o₂1.2ReLU: keep positives, zero the restinputhiddenoutputlabel Aif o₁ > o₂
The network used in every chapter. Each arrow multiplies by its weight, each circle adds what arrives. In colour, the values at the input c = (0.6, 0.4).

It has two inputs, x1x_1 and x2x_2. The hidden layer computes

h1=max⁡(0, x1+x2),h2=max⁡(0, x1−x2),h_1 = \max(0,\ x_1 + x_2), \qquad h_2 = \max(0,\ x_1 - x_2),

and the outputs are

o1=2h1−h2,o2=h1+h2.o_1 = 2h_1 - h_2, \qquad o_2 = h_1 + h_2 .

The network answers A when o1>o2o_1 \gt o_2, and B otherwise. In matrix form, o=W2ReLU⁡(W1x)o = W_2 \operatorname{ReLU}(W_1 x) with

W1=(111−1),W2=(2−111).W_1 = \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}, \qquad W_2 = \begin{pmatrix} 2 & -1 \\ 1 & 1 \end{pmatrix}.

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 c=(0.6, 0.4)c = (0.6,\ 0.4). Then h=(1, 0.2)h = (1,\ 0.2), o=(1.8, 1.2)o = (1.8,\ 1.2), and the answer is A. The gap o1−o2=0.6o_1 - o_2 = 0.6 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.6

The 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 ε\varepsilon. Does the network still answer A? Written out, we want

for all x with ∣x1−0.6∣≤ε and ∣x2−0.4∣≤ε,o1(x)>o2(x).\text{for all } x \text{ with } |x_1 - 0.6| \leq \varepsilon \text{ and } |x_2 - 0.4| \leq \varepsilon, \qquad o_1(x) \gt o_2(x).

The allowed inputs form a square of side 2ε2\varepsilon centred at cc. With more inputs it becomes a cube, and for images it is the usual setting where every pixel may change by at most ε\varepsilon. 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:

{ ∥x−c∥∞≤ε }o←N(x){ o1>o2 }.\big\{\ \|x - c\|_\infty \leq \varepsilon\ \big\} \qquad o \leftarrow N(x) \qquad \big\{\ o_1 \gt o_2\ \big\} .

Here ∥x−c∥∞\|x - c\|_\infty is the larger of ∣x1−0.6∣|x_1 - 0.6| and ∣x2−0.4∣|x_2 - 0.4|. 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 ε\varepsilon 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 cc 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 ε\varepsilon, each button tests random inputs from the square, and inputs where the answer stopped being A are circled.

0.40.50.60.70.80.20.30.40.50.6x₁x₂c

No inputs tried yet.

Every input in the square is allowed. Grey dots are tests that kept label A, circled ones flipped it.

At ε=0.16\varepsilon = 0.16 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 found

In 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 10d10^d points in dimension dd, and a small 28 × 28 greyscale image already has d=784d = 784. 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.04

Taking 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 (0.76, 0.24)(0.76,\ 0.24) 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 ε=0.14\varepsilon = 0.14 and it stops at (0.74, 0.26)(0.74,\ 0.26) with margin 0.040.04. Is 0.040.04 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 m∗m^\ast. Every input you try, by luck or by gradient, gives an upper bound: m∗m^\ast 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.

-1.5-1-0.500.5lower boundattackmargin ≤ 0: label flipsmargin > 0: label A

Lower bound -0.20, best attack 0.28. Unknown: the lower bound is negative, and the attack found no input that flips the label.

The smallest margin over the square is somewhere in the shaded bracket. A verifier (here the interval arithmetic of chapter 2) pushes the left end up, an attack pushes the right end down. On this tiny network the attack happens to land on the true worst input. On real networks it usually doesn't, and the bracket is wider.

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 XX and the network NN, and write

N(X)={ N(x)∣x∈X }N(X) = \{\, N(x) \mid x \in X \,\}

for the set of every output the network can produce on it. The answer is A exactly on the safe set

S={ o∣o1>o2 },S = \{\, o \mid o_1 \gt o_2 \,\},

and the property becomes a statement about two sets:

N(X)⊆S.N(X) \subseteq S .

That inclusion is what we'll be trying to prove for the rest of the series.

For our network we can compute N(X)N(X) exactly. The two ReLUs cut the input plane into regions where the network is linear. Where both hidden neurons are on, the margin is

o1−o2=h1−2h2=(x1+x2)−2(x1−x2)=3x2−x1.o_1 - o_2 = h_1 - 2h_2 = (x_1 + x_2) - 2(x_1 - x_2) = 3x_2 - x_1 .

Where h2h_2 is off, that is when x1<x2x_1 \lt x_2, the margin is just h1=x1+x2h_1 = x_1 + x_2, which is positive for every input we look at. So the worst input is the corner where x1x_1 is largest and x2x_2 smallest, (0.6+ε, 0.4−ε)(0.6 + \varepsilon,\ 0.4 - \varepsilon), and there the margin is

3(0.4−ε)−(0.6+ε)=0.6−4ε.3(0.4 - \varepsilon) - (0.6 + \varepsilon) = 0.6 - 4\varepsilon .

The network is robust exactly when ε<0.15\varepsilon \lt 0.15. Every method in the series will be chasing that number.

This is N(X)N(X) itself. It's a polygon, and the slider shows it touching the diagonal at ε=0.15\varepsilon = 0.15.

11.522.50.511.522.5o₁o₂
  • 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 nn ReLUs has up to 2n2^n 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 23002^{300} 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 N(X)N(X), compute a set YY that contains it, is easy to compute, and is easy to compare with SS. Then

N(X)⊆Y  and  Y⊆S⟹N(X)⊆S.N(X) \subseteq Y \ \text{ and } \ Y \subseteq S \quad\Longrightarrow\quad N(X) \subseteq S .

If the big set is safe, so is the true one. If the big set is not inside SS, we learn nothing: maybe the network is unsafe, maybe YY was too fat.

ProvedInconclusiveReal counterexample
Filled: the true set of outputs. Dashed: the bigger, simpler set we actually compute. Hatched: unsafe outputs. Only the left case is a proof. In the middle one the method gives up although the network is fine.

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 N(X)N(X), 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 PP, the output should be in some set QQ. For ACAS Xu, PP describes a geometry, say "the intruder is far away and much slower than us", and QQ a constraint on the scores, say "the score of clear of conflict stays below a threshold". The question is always whether N(P)⊆QN(P) \subseteq Q, 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 YY anyone has come up with is a box, one interval per variable. In chapter 2 boxes prove our network robust up to ε=0.06\varepsilon = 0.06. The truth is 0.150.15, and the gap has two separate causes.