COMP90073 Trustworthy Machine Learning · Workshop Week 10

Adversarial Examples, Automatic Differentiation and Robust Generalisation

Three questions: an iterative FGSM attack by hand, forward versus reverse automatic differentiation on a small network, and why adversarially trained models generalise worse. Each prerequisite is explained right before it is needed, with a figure you can play with.

class 1, clean input class 2 perturbation \(\delta\) gradients
iterative FGSM with projection
Question 1 · Lecture 16

Iterative FGSM under an \(\ell_\infty\) constraint

Question 1

Suppose \(f\) is a binary linear classifier

$$f(\mathbf x; W, b) = W\mathbf x + b, \qquad W = [2,\,-1], \quad b = 0.5, \quad \mathbf x = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}.$$

A point is classified into Class 1 if \(f(\mathbf x) > 0\), and into Class 2 if \(f(\mathbf x) \le 0\).

Generate an adversarial sample for the point \((1, 3)\) using the iterative fast gradient sign method in its standard form (for an \(\ell_\infty\) constraint). Use a step size of 1, and after each step apply a projection \(\mathbb P\) such that

$$\lVert \mathbf x_i - \mathbf x_0\rVert_\infty \le 3.$$

Three ideas make this question easy:

  1. a linear classifier is just a line with an arrow,
  2. an \(\ell_\infty\) budget is a square around the point,
  3. the attack steps along the sign of the gradient, then clips back into the square.

Each gets a short primer below. Then we solve the question live.

Prerequisite 1.1

1.1Geometry of a linear binary classifier

The classifier's score is

$$f(\mathbf x) = 2x_1 - x_2 + 0.5.$$

It is zero on a line, the decision boundary:

$$x_2 = 2x_1 + 0.5.$$

Points on one side have \(f > 0\) and are Class 1. Points on the other side are Class 2.

The gradient of a linear function is the same everywhere:

$$\nabla_{\mathbf x} f = W^\top = \begin{bmatrix} 2 \\ -1 \end{bmatrix}.$$

This arrow is perpendicular to the boundary and points into Class 1. So "increase \(f\)" always means "move along \(W\)", wherever you start. That is why one step of the attack will be enough.

How far is the boundary? It depends on how you measure distance (lecture 16, slides 6–8):

$$\text{in } \ell_2: \ \ \frac{|f(\mathbf x_0)|}{\lVert W\rVert_2} = \frac{|f(\mathbf x_0)|}{\sqrt5}, \qquad\qquad \text{in } \ell_\infty: \ \ \frac{|f(\mathbf x_0)|}{\lVert W\rVert_1} = \frac{|f(\mathbf x_0)|}{3}.$$

The \(\ell_2\) path goes straight along \(W\). The \(\ell_\infty\) path moves every coordinate by the same amount. Section 1.2 explains why.

Derivation: why these distance formulas hold

One idea does all the work. The score is linear, so a move \(\delta\) changes it by exactly

$$f(\mathbf x_0 + \delta) - f(\mathbf x_0) = W\delta = W_1\delta_1 + W_2\delta_2.$$

To reach the boundary, the score must change by \(|f(\mathbf x_0)|\). So the distance is

$$\text{distance} = \frac{|f(\mathbf x_0)|}{\text{largest change in } f \text{ per unit of budget}}.$$

All that differs between norms is what "one unit of budget" allows.

\(\ell_\infty\): every coordinate may move by up to \(\epsilon\)

Each term \(W_i\delta_i\) is at most \(|W_i|\,\epsilon\), so

$$W\delta \;\le\; |W_1|\,\epsilon + |W_2|\,\epsilon \;=\; \lVert W\rVert_1\,\epsilon.$$

Equality holds when every coordinate moves the full \(\epsilon\) in the direction of its weight's sign:

$$\delta = \epsilon\,\operatorname{sign}(W).$$

So the best rate is \(\lVert W\rVert_1\), and the distance is \(|f(\mathbf x_0)|/\lVert W\rVert_1\). This is exactly the FGSM step.

With the question's numbers. \(W = [2, -1]\), so the best move is \(\delta = \epsilon\,(1, -1)\), which raises the score by

$$2\epsilon + 1\cdot\epsilon = 3\epsilon.$$

The score must rise by 0.5, so we need \(3\epsilon \ge 0.5\), i.e. \(\epsilon \ge 1/6\).

\(\ell_2\): the move may have length up to \(\epsilon\)

The shortest way from a point to a line is the perpendicular, and \(W\) is perpendicular to the boundary. Moving a length \(t\) along \(W\) changes the score by

$$W\Big(t\,\frac{W^\top}{\lVert W\rVert_2}\Big) = t\,\lVert W\rVert_2.$$

So the best rate is \(\lVert W\rVert_2 = \sqrt5\), and the distance is \(|f(\mathbf x_0)|/\lVert W\rVert_2\). This is lecture 16's one-projection attack (slides 6–8). Formally, the bound is the Cauchy–Schwarz inequality, \(|W\delta| \le \lVert W\rVert_2\lVert\delta\rVert_2\).

\(\ell_1\): the total movement \(|\delta_1| + |\delta_2|\) is limited

Every unit of movement is worth at most the largest weight, so spend it all on that coordinate:

$$W\delta \;\le\; \max_i |W_i|\ \big(|\delta_1| + |\delta_2|\big).$$

The best rate is \(\max_i|W_i| = 2\), and the distance is \(|f(\mathbf x_0)|/2\).

Summary

budgetbest ratebest movedistance at \((1,3)\)
\(\ell_\infty\)\(\lVert W\rVert_1 = 3\)every coordinate by \(\pm\epsilon\), signs of \(W\)\(0.5/3 = 1/6\)
\(\ell_2\)\(\lVert W\rVert_2 = \sqrt5\)straight along \(W\)\(0.5/\sqrt5 \approx 0.224\)
\(\ell_1\)\(\lVert W\rVert_\infty = 2\)only the largest-weight coordinate\(0.5/2 = 0.25\)

Footnote, not needed for the course. The "best rate" for a general \(\ell_p\) budget is called the dual norm \(\lVert W\rVert_q\), with \(1/p + 1/q = 1\), and the bound is Hölder's inequality. The three rows above are the cases \(p = \infty, 2, 1\).

InteractiveDecision boundary, gradient and minimal perturbations
gradient \(W^\top\)shortest \(\ell_2\) pathshortest \(\ell_\infty\) path
Wherever you drag the point, the gradient arrow stays the same. The \(\ell_\infty\) path is diagonal because it moves both coordinates equally.
Prerequisite 1.2

1.2\(\ell_p\) norm balls and the optimal \(\ell_\infty\) perturbation

An attack may move the input by a perturbation \(\delta\), but only within a budget \(\epsilon\) (lecture 16, slides 10–11):

$$\mathbf x_{\text{adv}} = \mathbf x_0 + \delta, \qquad \lVert\delta\rVert_p \le \epsilon.$$

Three common ways to measure the size of \(\delta\):

$$\lVert\delta\rVert_1 = \sum_i |\delta_i| \qquad\quad \lVert\delta\rVert_2 = \sqrt{\textstyle\sum_i \delta_i^2} \qquad\quad \lVert\delta\rVert_\infty = \max_i |\delta_i|$$

The \(\ell_\infty\) ball is a square. Every coordinate may change by up to \(\epsilon\), independently. For images: no pixel changes by more than \(\epsilon\).

Which \(\delta\) pushes a linear score up the most? Inside the square, the answer is always a corner:

$$\max_{\lVert\delta\rVert_\infty\le\epsilon} \; w^\top\delta \;=\; \epsilon\,\lVert w\rVert_1, \qquad \text{reached at} \qquad \delta^\star = \epsilon\,\operatorname{sign}(w).$$

That corner is exactly the FGSM step.

InteractiveMaximisers of \(w^\top\delta\) over the \(\ell_1\), \(\ell_2\) and \(\ell_\infty\) balls
\(\ell_\infty\): a corner\(\ell_2\): along \(w\)\(\ell_1\): on an axis
Drag the arrow tip, or use the slider. The default direction is the question's \(W\). The \(\ell_\infty\) winner only jumps to another corner when a component of \(w\) changes sign.
Prerequisite 1.3

1.3FGSM, iterative FGSM and \(\ell_\infty\) projection

FGSM takes one big step of size \(\epsilon\) (lecture 16, slide 15):

$$\mathbf x_{\text{adv}} = \mathbf x_0 + \epsilon\,\operatorname{sign}\big(\nabla_{\mathbf x}\mathcal L\big).$$

Iterative FGSM takes several smaller steps of size \(\alpha\). After each step it projects back into the budget (slide 17):

$$\mathbf x_{k+1} = \mathbb P\Big(\mathbf x_k + \alpha\,\operatorname{sign}\big(\nabla_{\mathbf x} f(\mathbf x_k)\big)\Big).$$

For \(\ell_\infty\), the projection simply clips each coordinate back into its allowed interval:

$$\mathbb P(\mathbf z)_i = \operatorname{clip}\big(z_i,\ \ x_{0,i}-\epsilon,\ \ x_{0,i}+\epsilon\big).$$

Small steps matter when the boundary is curved, where one big step can overshoot (slide 16). The animation at the top of the page shows that case.

Which direction? At the starting point,

$$f(1, 3) = 2 - 3 + 0.5 = -0.5 \le 0 \quad\Rightarrow\quad \text{Class 2}.$$

To reach Class 1 we must increase \(f\), so we step along \(+\operatorname{sign}(\nabla f)\).

Warm-up exercise

1.4Warm-up: a single FGSM step on a simpler linear classifier

Warm-up 1 · based on lecture 16, slides 6–8 and 15

Consider the classifier

$$f(\mathbf x) = x_1 + x_2 - 3, \qquad w = [1,\ 1], \quad b = -3.$$

As in Q1, \(f > 0\) means Class 1 and \(f \le 0\) means Class 2. Start from \(\mathbf x_0 = (1, 1)\).

1. The score at the start, \(f(\mathbf x_0)\):

So \(\mathbf x_0\) is Class 2. The gradient is \(\nabla f = w = [1, 1]\), so \(\operatorname{sign}(\nabla f) = [1, 1]\).

2. Take one FGSM step with \(\epsilon = 0.75\). The new score \(f(\mathbf x')\):

How small can the budget be? Step 2 flipped the label with \(\epsilon = 0.75\). Now find the smallest budget that can work. Moving every coordinate by \(\epsilon\) in the direction \(\operatorname{sign}(w)\) is the best \(\ell_\infty\) move (1.2), so the new score is a straight line in \(\epsilon\):

$$f\big(\mathbf x_0 + \epsilon\,\operatorname{sign}(w)\big) = f(\mathbf x_0) + (\text{rise per unit of } \epsilon)\cdot\epsilon.$$
3a. How much does \(f\) rise per unit of \(\epsilon\)?
3b. So how large must \(\epsilon\) be for \(f\) to reach the boundary \(f = 0\)?

This is the \(\ell_\infty\) distance formula from 1.1: the rise per unit of \(\epsilon\) is \(\lVert w\rVert_1 = 2\), so \(\epsilon^\star = |f(\mathbf x_0)|/\lVert w\rVert_1 = 1/2\). Any budget below \(\epsilon^\star\) cannot flip the label. The "Derivation" dropdown in 1.1 explains why.

4. Project \(\mathbf z = (1.75,\ 0.2)\) back into the box \(\lVert\mathbf z - \mathbf x_0\rVert_\infty \le 0.5\). Enter both coordinates:
Show solution

The starting score is negative, so the point is Class 2:

$$f(1,1) = 1 + 1 - 3 = -1.$$

One FGSM step with \(\epsilon = 0.75\):

$$\mathbf x' = (1,1) + 0.75\cdot(1,1) = (1.75,\ 1.75), \qquad f(\mathbf x') = 0.5 > 0.$$

The label flips to Class 1. Moving both coordinates by \(\epsilon\) raises the score by \(1\cdot\epsilon + 1\cdot\epsilon = 2\epsilon\). It reaches the boundary when

$$-1 + 2\epsilon = 0 \quad\Rightarrow\quad \epsilon = \tfrac12.$$

In formula form, the minimal budget is

$$\epsilon^\star = \frac{|f(\mathbf x_0)|}{\lVert w\rVert_1} = \frac{1}{2}.$$

At exactly \(\epsilon = 0.5\) the score is 0, which still counts as Class 2, so any \(\epsilon > 0.5\) works. The projection clips each coordinate into \([0.5,\ 1.5]\):

$$\mathbb P(1.75,\ 0.2) = (1.5,\ 0.5).$$

Bridge to Q1. Q1 is the same computation with \(W = [2, -1]\), \(b = 0.5\), a step of \(\alpha = 1\) and a budget of \(\epsilon = 3\). The only new ingredient is that the step is repeated, with a projection after each step.

Solution

1.5Worked solution

InteractiveIterative FGSM with \(\ell_\infty\) projection from \(\mathbf x_0 = (1, 3)\)
Drag the start point to try others.
The dashed square is the budget around the start point. When a step would leave it, a faded point marks the raw step and the solid point the clipped one. Try \(\epsilon = 0.1\): no attack exists, because the boundary is \(1/6\) away in \(\ell_\infty\).
Official answer, step by step
  1. Step 1 · Classify the starting point
  2. Step 2 · Compute the gradient
  3. Step 3 · Take one sign-gradient step
  4. Step 4 · Apply the projection
  5. Step 5 · Check whether the label flipped

Worth saying in class. A step of 1 overshoots a lot: the smallest \(\ell_\infty\) change that flips the label is only \(1/6\). The projection never activates, because \(\alpha = 1\) is smaller than \(\epsilon = 3\). If you keep iterating, the point walks to the box corner \((4, 0)\) and stays there.


Question 2 · Lecture 17

Forward- and reverse-mode automatic differentiation

Question 2

Consider the function

$$y = \cos\!\left(e^{x_1}\right) - \sin\!\left(\frac{x_1}{x_2} + 2x_2\right).$$

Evaluate \(\nabla_{\mathbf x} y\) at the point \(\mathbf x = (2, 4)\): directly, and by following forward and reverse mode autodifferentiation. To help, think of the function as a network with the layers

$$\begin{aligned} \mathbf a &= f(\mathbf x) = \Big[\,e^{x_1},\ \ \tfrac{x_1}{x_2},\ \ 2x_2\,\Big] \\ \mathbf b &= g(\mathbf a) = \big[\,\cos(a_1),\ \ \sin(a_2 + a_3)\,\big] \\ y &= h(\mathbf b) = [\,???\,] \end{aligned}$$
  1. How many multiplications does each mode need? What else contributes to the cost, and why do we mainly care about multiplications?
  2. Autodiff reduces operations to elementary functions, then looks up their derivatives in a table. What difficulty might this partitioning face on a computer? How would you fix it?
  3. Ignoring (b), describe a different partitioning of \(f\), \(g\) and \(h\) that needs fewer operations, without making the derivatives harder.

The missing layer simply subtracts:

$$h(\mathbf b) = b_1 - b_2.$$

The official answer writes \(b_0 - b_1\), counting from zero.

Prerequisite 2.1

2.1Jacobians and the chain rule as a matrix product

A function with \(n\) inputs and \(m\) outputs has an \(m \times n\) matrix of derivatives, its Jacobian (lecture 17, slide 21):

$$J_{ij} = \frac{\partial\, \text{output}_i}{\partial\, \text{input}_j}.$$

For a single scalar output, the Jacobian is one row: the gradient.

For a chain of layers, the chain rule multiplies the layer Jacobians:

$$\underbrace{\frac{\partial y}{\partial\mathbf x}}_{1\times 2} \;=\; \underbrace{h_{\mathbf b}}_{1\times 2}\ \underbrace{g_{\mathbf a}}_{2\times 3}\ \underbrace{f_{\mathbf x}}_{3\times 2}$$

Cost of one product. An \((m\times n)\) matrix times an \((n\times p)\) matrix has \(m p\) entries, each a sum of \(n\) products:

$$\text{cost} = m \cdot n \cdot p \ \text{ multiplications}.$$

The key point. Matrix products are associative, so we may bracket the chain either way. That choice is the difference between the two modes:

$$\text{forward:} \ \ h_{\mathbf b}\,\big(g_{\mathbf a}\, f_{\mathbf x}\big) \qquad\qquad \text{reverse:} \ \ \big(h_{\mathbf b}\, g_{\mathbf a}\big)\, f_{\mathbf x}$$
Prerequisite 2.2

2.2Computational graph evaluated at \(\mathbf x = (2, 4)\)

Each node stores its value from the forward pass. Each edge carries a local derivative. Lecture 17's rule (slide 18):

$$\text{downstream gradient} = \text{upstream gradient} \times \text{local derivative}.$$
  • Forward mode pushes derivatives with respect to one input from left to right. One sweep per input.
  • Reverse mode pulls the derivative of the output from right to left. One sweep per output.
InteractivePrimal values, forward-mode tangents and reverse-mode adjoints
Forward mode needs two sweeps here (one per input), and each gives one entry of the gradient. Reverse mode gets the whole gradient in one sweep. With a scalar loss and millions of inputs, that is why networks are trained with reverse mode, i.e. backpropagation.

The two update rules, written out. A tangent \(\dot v\) is a derivative with respect to the seeded input, and flows forward:

$$\dot v = \sum_{\text{parents } u} \frac{\partial v}{\partial u}\,\dot u.$$

An adjoint \(\bar v\) is the derivative of \(y\) with respect to \(v\), and flows backward:

$$\bar u = \sum_{\text{children } v} \bar v\,\frac{\partial v}{\partial u}.$$
Prerequisite 2.3

2.3Computational cost of forward and reverse mode

InteractiveOrder of Jacobian multiplication and operation count
Each square is one matrix entry. Reverse mode starts from the one-row end, so every intermediate result stays one row tall. That is why it is cheaper here.
InteractiveScaling of cost with input and output dimension
A 4-layer chain with hidden width 100. Forward cost grows with the number of inputs, reverse cost with the number of outputs. A loss is one number, while a network has millions of weights, so reverse mode wins by orders of magnitude. Log scale.
Warm-up exercise

2.4Warm-up: forward and backward pass through a single neuron

Warm-up 2 · the example from lecture 17, slide 24

A logistic neuron with binary cross-entropy loss:

$$z = w x + b, \qquad \hat y = \sigma(z) = \frac{1}{1 + e^{-z}}, \qquad \mathcal L = -y\log\hat y - (1-y)\log(1-\hat y).$$

Use \(x = 2\), \(w = 0.5\), \(b = 0\) and \(y = 1\).

Forward pass

1. The prediction \(\hat y\):
2. The loss \(\mathcal L\):

Backward pass

For a sigmoid followed by cross-entropy, the local derivatives collapse to one simple term (lecture 16, slide 34):

$$\frac{\partial\mathcal L}{\partial z} = \hat y - y.$$
3. The weight gradient \(\partial\mathcal L/\partial w\), used for training:
4. The input gradient \(\partial\mathcal L/\partial x\), used for an attack:

Check it numerically

A finite difference estimates the same slope without any calculus (lecture 18, slide 5):

$$\frac{\partial\mathcal L}{\partial x} \approx \frac{\mathcal L(x+h) - \mathcal L(x-h)}{2h}.$$

Count the multiplications

Now a chain with one input and one output, where the Jacobians have shapes

$$J = \underbrace{h_{\mathbf b}}_{1\times2}\ \underbrace{g_{\mathbf a}}_{2\times3}\ \underbrace{f_x}_{3\times1}.$$
5. Multiplications in forward mode:
6. Multiplications in reverse mode:
Show solution

Forward.

$$z = 0.5 \cdot 2 + 0 = 1, \qquad \hat y = \sigma(1) \approx 0.7311, \qquad \mathcal L = -\log 0.7311 \approx 0.3133.$$

Backward.

$$\frac{\partial\mathcal L}{\partial z} = 0.7311 - 1 = -0.2689$$ $$\frac{\partial\mathcal L}{\partial w} = -0.2689 \cdot x \approx -0.538, \qquad \frac{\partial\mathcal L}{\partial b} = -0.2689, \qquad \frac{\partial\mathcal L}{\partial x} = -0.2689 \cdot w \approx -0.134.$$

Training follows \(-\partial\mathcal L/\partial w\). An attack follows \(+\operatorname{sign}(\partial\mathcal L/\partial x)\). It is the same graph with a different leaf (lecture 17, slide 17).

Counting. Forward mode costs \(6 + 2 = 8\). Reverse mode costs \(6 + 3 = 9\). With one input, forward mode is slightly cheaper.

Bridge to Q2. Q2 has the opposite shape: two inputs and one output. So expect reverse mode to win there.

Solution

2.5Worked solution: layer Jacobians and both modes

Directly. Differentiating by hand gives two partial derivatives:

$$\frac{\partial y}{\partial x_1} = -\sin\!\big(e^{x_1}\big)\,e^{x_1} \;-\; \frac{1}{x_2}\cos\!\Big(\frac{x_1}{x_2} + 2x_2\Big)$$ $$\frac{\partial y}{\partial x_2} = -\cos\!\Big(\frac{x_1}{x_2} + 2x_2\Big)\Big(2 - \frac{x_1}{x_2^2}\Big)$$

Layer by layer. First the three Jacobians, then the two ways of multiplying them. Switch to numbers to see the values at \((2, 4)\).

LiveLayer Jacobians
Forward\(h_{\mathbf b}\,(g_{\mathbf a} f_{\mathbf x})\)
Reverse\((h_{\mathbf b}\,g_{\mathbf a})\, f_{\mathbf x}\)
Official answer, step by step
  1. Step 1 · Write down the layer Jacobians
  2. Step 2 · Forward mode: the first product
  3. Step 3 · Forward mode: the second product
  4. Step 4 · Reverse mode: the first product
  5. Step 5 · Reverse mode: the second product
  6. Step 6 · Evaluate at \(\mathbf x = (2,4)\) not in the official sheet

The official sheet labels the last reverse factor \(f_{\mathbf a}\); it means \(f_{\mathbf x}\). It stops at the symbolic form, so step 6 is added here and checked against finite differences in the live panel above.

Solution

2.6Parts (a)–(c): cost analysis and partitioning

(a) Operation count and other computational costs

Forward mode needs 16 multiplications, reverse mode 12.

Other costs:

  • Memory. Reverse mode must keep every layer's values from the forward pass until the backward pass reaches them (the "tape"). Forward mode can discard each Jacobian once it has been used.
  • Additions inside each dot product.
  • Evaluating each elementary derivative.

The last item is cheap, because autodiff looks up derivatives of elementary functions (the derivative of \(\cos\) is \(-\sin\), and so on). Once derivatives are lookups, the remaining work is combining them, and that is dominated by the multiplications.

(b) Non-elementary functions in the partition

The table knows the derivative of \(\cos(u)\) for a single input \(u\). It does not know the derivative of \(\cos(a_2 + a_3)\) as a function of two inputs.

The fix: add a layer that forms the sum first,

$$\mathbf s = s(\mathbf a) = [\,a_1,\ \ a_2 + a_3\,], \qquad s_{\mathbf a} = \begin{bmatrix}1&0&0\\0&1&1\end{bmatrix}.$$

The next layer then applies \(\cos\) and \(\sin\) to one input each.

(c) An operation-reducing partition

Fold the subtraction into \(g\):

$$g(\mathbf a) = \big[\cos(a_1) - \sin(a_2 + a_3)\big], \qquad h(\mathbf b) = [\,b_1\,].$$

Now \(g_{\mathbf a}\) is \(1\times3\), and reverse mode costs

$$1\times1\times3 \;+\; 1\times3\times2 \;=\; 9.$$

Compare all four partitions below.

InteractiveOperation counts for alternative partitions
Costs are counted as in the official answer. Partition (b) follows the elementary-function rule but costs the most. Partition (c) gives the official 9 for reverse mode; forward mode happens to cost 8 there.

Question 3 · supplementary to Lectures 16–18

The generalisation gap under adversarial training

Question 3

With regards to adversarial training, what is the generalisation gap? How is it influenced by adversarial training? What can this tell us about how machine learning models learn, and the decision boundaries that form around data manifolds?

This question ties the first two together:

  • adversarial training runs the attack from Q1 inside every training step,
  • and computes all its gradients with the reverse mode from Q2.
Prerequisite 3.1

3.1Clean and robust generalisation gaps

The generalisation gap compares performance on the training data with performance on unseen test data:

$$\text{gap} = \text{Acc}_{\text{train}} - \text{Acc}_{\text{test}}.$$

The robust accuracy counts a point as correct only if every perturbation inside its budget is still classified correctly. In practice we check this with a strong attack such as PGD:

$$\text{RobAcc} = \Pr\Big[\,f(\mathbf x + \delta) = y \ \ \text{for all} \ \lVert\delta\rVert_\infty \le \epsilon\,\Big].$$

That gives a second gap:

$$\text{robust gap} = \text{RobAcc}_{\text{train}} - \text{RobAcc}_{\text{test}}.$$

A standard train/test split assumes both sets come from the same distribution. Attackers, and real deployments, break that assumption.

Prerequisite 3.2

3.2Adversarial training as a min–max optimisation

Instead of minimising the loss at each training point, minimise it at the worst point in that point's \(\epsilon\)-box4:

$$\min_\theta\ \ \mathbb E_{(\mathbf x,y)}\Big[\ \max_{\lVert\delta\rVert_\infty\le\epsilon}\ \mathcal L\big(f_\theta(\mathbf x+\delta),\ y\big)\Big]$$
  • The inner max is approximated by PGD: the loop from Q1, with a random start.
  • The outer min is ordinary gradient descent on the weights \(\theta\), using reverse mode from Q2.

Same graph, two targets: the attack differentiates with respect to the input, training with respect to the weights (lecture 17, slide 17).

The catch. Each training point now asks for a whole box to be classified correctly, not just one point. A flexible model can fit those boxes exactly. But fitting boxes around 30 points says little about the boxes around new points.

Warm-up exercise

3.3Warm-up: clean and robust accuracy of a one-dimensional classifier

Warm-up 3

A threshold classifier on the real line:

$$f(x) = x - 0.5, \qquad \text{Class 1 if } f(x) > 0.$$

In one dimension the \(\ell_\infty\) budget is an interval: the attacker may move each point by up to \(\epsilon = 0.3\) to the left or right. A point is robustly correct if its whole interval stays on the correct side:

$$|x - 0.5| > \epsilon \quad \text{and the clean prediction is correct}.$$
setClass 1 pointsClass 2 points
train\(1.0,\ 0.9\)\(0.1,\ 0.0\)
test\(0.65,\ 1.2\)\(0.3,\ 0.45\)
Drag the boundary to move the threshold.
1. The clean generalisation gap, in percentage points:
2. The robust test accuracy, in %:
3. The robust generalisation gap, in percentage points:
Show solution

Clean: every point is on its correct side, so train and test accuracy are both 100% and the clean gap is 0.

Robust: the training points lie at distance 0.5, 0.4, 0.4 and 0.5 from the threshold, all beyond \(\epsilon = 0.3\), so robust train accuracy is 100%. The test points lie at 0.15, 0.7, 0.2 and 0.05, and only one is beyond 0.3:

$$\text{RobAcc}_{\text{test}} = \tfrac14 = 25\%, \qquad \text{robust gap} = 100\% - 25\% = 75 \text{ points}.$$

The same classifier has no clean gap but a huge robust gap. Robustness asks for a margin around every point, and test points are not guaranteed to be as far from the boundary as the training points were.

Bridge to Q3. Adversarial training makes the model create that margin around its training points. The next experiment asks whether the margin carries over to test points.

Empirical study

3.4Empirical study: standard versus adversarial training

LiveStandard and PGD adversarial training on the two-moons dataset
Class 1 trainClass 2 train\(\epsilon\)-boxboundary
robust trainrobust testclean trainclean test
A small 2–32–32–2 network trained in your browser with Adam, on noisy two-moons data. Robust accuracy uses a 10-step PGD attack. The shaded band on the right is the robust gap. Try this: run both with 30 points, then again with 120 points, and watch the robust gap shrink.
Interpretation

3.5Boundary tilting and the data manifold

Put the data on a thin manifold, here a horizontal line. The label changes at the middle.

Any boundary through the middle that is not horizontal gets 100% accuracy. But as the boundary tilts towards the data line, every point gets closer to it in directions off the manifold.

Standard training has no reason to prefer an upright boundary. This is the boundary-tilting picture of adversarial examples11.

InteractiveBoundary orientation versus clean and robust accuracy
Clean accuracy stays at 100% for every tilt. Robust accuracy falls as the boundary leans onto the data and the boxes start to cross it. Adversarial training pushes the boundary out of the boxes it sees, but elsewhere it is still free to lean in.
Answer

3.6Answer and discussion

Official answer, step by step
  1. Step 1 · Define the generalisation gap
  2. Step 2 · Question the i.i.d. assumption
  3. Step 3 · State the effect of adversarial training
  4. Step 4 · Interpret it

Implications for learning and decision boundaries

Models fit what they are shown. Adversarial training constrains the boundary only inside the boxes around training points. A flexible model can memorise those boxes, reaching near-100% robust training accuracy, without keeping a margin around new points. This robust overfitting grows with longer training, and early stopping helps6.

Robustness needs more data. Robust generalisation can need far more samples than standard generalisation5. The experiment shows this when you go from 30 to 120 training points.

Boundaries hug the manifold. Real data lie near a low-dimensional manifold. Standard training only cares that points on it land on the right side. In off-manifold directions the boundary can sit very close to the data (3.5), and adversarial examples exploit exactly those directions11,12,13.

Accuracy and robustness can pull apart. Forcing the boundary to keep its distance can cost clean accuracy7,8. Some useful features are real but brittle, and robust training makes the model give them up9.

Summary

Summary of key results

IdeaFormula / factWhere
Linear boundary\(W\mathbf x + b = 0\), gradient \(\nabla_{\mathbf x} f = W^\top\) everywhere1.1
Distance to boundary\(\ell_2\): \(|f|/\lVert W\rVert_2\); \(\ell_\infty\): \(|f|/\lVert W\rVert_1\)1.1, 1.2
Best \(\ell_\infty\) step\(\arg\max_{\lVert\delta\rVert_\infty\le\epsilon} w^\top\delta = \epsilon\operatorname{sign}(w)\)1.2
Iterative FGSM\(\mathbf x_{k+1} = \operatorname{clip}_{[\mathbf x_0-\epsilon,\ \mathbf x_0+\epsilon]}\big(\mathbf x_k + \alpha\operatorname{sign}\nabla\big)\)1.3, Q1
Q1 answer\((1,3)\to(2,2)\), \(f\) from \(-0.5\) to \(2.5\): Class 2 to Class 11.5
Chain rule\(\partial y/\partial\mathbf x = h_{\mathbf b}\,g_{\mathbf a}\,f_{\mathbf x}\)2.1
Product cost\((m\times n)(n\times p)\): \(m\,n\,p\) multiplications2.1, 2.3
Q2 countsforward 16, reverse 12; merged partition: reverse 92.5, 2.6
Q2 value\(\nabla y(2,4) \approx [-6.4542,\ 1.1288]\)2.5
Adversarial training\(\min_\theta \mathbb E\,\max_{\lVert\delta\rVert\le\epsilon}\mathcal L(f_\theta(\mathbf x+\delta),y)\)3.2
Generalisation gaptrain minus test accuracy; larger under adversarial training3.1, 3.4
Bibliography

References

  1. I. J. Goodfellow, J. Shlens, C. Szegedy. Explaining and harnessing adversarial examples. ICLR 2015. arxiv.org/abs/1412.6572
  2. A. Kurakin, I. J. Goodfellow, S. Bengio. Adversarial examples in the physical world. ICLR Workshop 2017. arxiv.org/abs/1607.02533
  3. A. G. Baydin, B. A. Pearlmutter, A. A. Radul, J. M. Siskind. Automatic differentiation in machine learning: a survey. JMLR 18(153):1–43, 2018. arxiv.org/abs/1502.05767
  4. A. Madry, A. Makelov, L. Schmidt, D. Tsipras, A. Vladu. Towards deep learning models resistant to adversarial attacks. ICLR 2018. arxiv.org/abs/1706.06083
  5. L. Schmidt, S. Santurkar, D. Tsipras, K. Talwar, A. Madry. Adversarially robust generalization requires more data. NeurIPS 2018. arxiv.org/abs/1804.11285
  6. L. Rice, E. Wong, J. Z. Kolter. Overfitting in adversarially robust deep learning. ICML 2020. arxiv.org/abs/2002.11569
  7. D. Tsipras, S. Santurkar, L. Engstrom, A. Turner, A. Madry. Robustness may be at odds with accuracy. ICLR 2019. arxiv.org/abs/1805.12152
  8. H. Zhang, Y. Yu, J. Jiao, E. P. Xing, L. El Ghaoui, M. I. Jordan. Theoretically principled trade-off between robustness and accuracy. ICML 2019 (TRADES). arxiv.org/abs/1901.08573
  9. A. Ilyas, S. Santurkar, D. Tsipras, L. Engstrom, B. Tran, A. Madry. Adversarial examples are not bugs, they are features. NeurIPS 2019. arxiv.org/abs/1905.02175
  10. A. Griewank, A. Walther. Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation. 2nd ed., SIAM, 2008. doi.org/10.1137/1.9780898717761
  11. T. Tanay, L. D. Griffin. A boundary tilting perspective on the phenomenon of adversarial examples. arXiv 2016. arxiv.org/abs/1608.07690
  12. J. Gilmer, L. Metz, F. Faghri, S. S. Schoenholz, M. Raghu, M. Wattenberg, I. J. Goodfellow. Adversarial spheres. ICLR Workshop 2018. arxiv.org/abs/1801.02774
  13. D. Stutz, M. Hein, B. Schiele. Disentangling adversarial robustness and generalization. CVPR 2019. arxiv.org/abs/1812.00740

Built for COMP90073 Workshop Week 10 from lectures 16–18 (Huang) and the Week 10 tutorial answers.