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.
Iterative FGSM under an \(\ell_\infty\) constraint
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:
- a linear classifier is just a line with an arrow,
- an \(\ell_\infty\) budget is a square around the point,
- 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.
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
| budget | best rate | best move | distance 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\).
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.
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)\).
1.4Warm-up: a single FGSM step on a simpler linear classifier
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)\).
So \(\mathbf x_0\) is Class 2. The gradient is \(\nabla f = w = [1, 1]\), so \(\operatorname{sign}(\nabla f) = [1, 1]\).
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.$$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.
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.
1.5Worked solution
-
Step 1 · Classify the starting point
When \(\mathbf x = (1,3)\):
$$f(1,3) = 2 - 3 + 0.5 = -0.5 < 0,$$so the prediction is Class 2.
-
Step 2 · Compute the gradient$$\nabla_{\mathbf x} f = W = \begin{bmatrix}2\\-1\end{bmatrix}, \qquad \operatorname{sign}(\nabla_{\mathbf x} f) = \begin{bmatrix}1\\-1\end{bmatrix}.$$
-
Step 3 · Take one sign-gradient step$$\begin{bmatrix} x_1^{(1)} \\ x_2^{(1)} \end{bmatrix} = \mathbb P\left(\begin{bmatrix}1\\3\end{bmatrix} + 1\cdot\operatorname{sign}\begin{bmatrix}2\\-1\end{bmatrix}\right) = \mathbb P\begin{bmatrix}2\\2\end{bmatrix}.$$
-
Step 4 · Apply the projection
\((2,2)\) is within an \(\ell_\infty\) distance of 3 from \((1,3)\):
$$\lVert (2,2) - (1,3)\rVert_\infty = \max(1,\ 1) = 1 \le 3,$$so the projection leaves it unchanged:
$$\mathbf x^{(1)} = \begin{bmatrix}2\\2\end{bmatrix}.$$ -
Step 5 · Check whether the label flipped$$f(2,2) = 4 - 2 + 0.5 = 2.5 > 0,$$
a prediction of Class 1. So \(\mathbf x^{(1)} = (2,2)\) is an adversarial example.
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.
Forward- and reverse-mode automatic differentiation
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}$$- How many multiplications does each mode need? What else contributes to the cost, and why do we mainly care about multiplications?
- 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?
- 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.
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}$$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.
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}.$$2.3Computational cost of forward and reverse mode
2.4Warm-up: forward and backward pass through a single neuron
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
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.$$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}.$$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.
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)\).
-
Step 1 · Write down the layer Jacobians
With \(h(\mathbf b) = [b_0 - b_1]\):
$$f_{\mathbf x} = \begin{bmatrix} e^{x_1} & 0 \\ 1/x_2 & -x_1/x_2^2 \\ 0 & 2 \end{bmatrix}$$$$g_{\mathbf a} = \begin{bmatrix} -\sin(a_1) & 0 & 0 \\ 0 & \cos(a_2+a_3) & \cos(a_2+a_3)\end{bmatrix}$$$$h_{\mathbf b} = \begin{bmatrix} 1 & -1 \end{bmatrix}$$ -
Step 2 · Forward mode: the first product$$g_{\mathbf a}\times f_{\mathbf x} = \begin{bmatrix} -\sin(a_1)\,e^{x_1} & 0 \\ \cos(a_2+a_3)/x_2 & \cos(a_2+a_3)\big(-x_1/x_2^2 + 2\big)\end{bmatrix}$$
Shapes \((2\times3)(3\times2)\): \(2\times3\times2 = 12\) multiplications.
-
Step 3 · Forward mode: the second product$$h_{\mathbf b}\,(g_{\mathbf a}\times f_{\mathbf x}) = \Big[\,-\sin(e^{x_1})e^{x_1} - \tfrac{\cos(x_1/x_2 + 2x_2)}{x_2} \qquad -\cos(x_1/x_2 + 2x_2)\big(-x_1/x_2^2 + 2\big)\,\Big]$$
\(1\times2\times2 = 4\) multiplications. Forward total: 16.
-
Step 4 · Reverse mode: the first product$$h_{\mathbf b}\times g_{\mathbf a} = \big[\,-\sin(a_1) \quad -\cos(a_2+a_3) \quad -\cos(a_2+a_3)\,\big]$$
Shapes \((1\times2)(2\times3)\): \(1\times2\times3 = 6\) multiplications.
-
Step 5 · Reverse mode: the second product$$(h_{\mathbf b}\times g_{\mathbf a})\times f_{\mathbf x} = \Big[\,-\sin(e^{x_1})e^{x_1} - \tfrac{\cos(x_1/x_2 + 2x_2)}{x_2} \qquad -\cos(x_1/x_2 + 2x_2)\big(-x_1/x_2^2 + 2\big)\,\Big]$$
\(1\times3\times2 = 6\) multiplications. Reverse total: 12.
-
Step 6 · Evaluate at \(\mathbf x = (2,4)\) not in the official sheet$$\frac{\partial y}{\partial x_1} \approx -0.8939 \times 7.389 - \frac{-0.6020}{4} \approx -6.4542$$$$\frac{\partial y}{\partial x_2} \approx -(-0.6020)\big(2 - \tfrac{2}{16}\big) \approx 1.1288$$$$\nabla_{\mathbf x} y(2,4) \approx [-6.4542,\ \ 1.1288]$$
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.
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.
The generalisation gap under adversarial training
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.
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.
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.
3.3Warm-up: clean and robust accuracy of a one-dimensional classifier
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}.$$| set | Class 1 points | Class 2 points |
|---|---|---|
| train | \(1.0,\ 0.9\) | \(0.1,\ 0.0\) |
| test | \(0.65,\ 1.2\) | \(0.3,\ 0.45\) |
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.
3.4Empirical study: standard versus adversarial training
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.
3.6Answer and discussion
-
Step 1 · Define the generalisation gap
The generalisation gap is the difference between train and test accuracy of a model. It represents the ability of a model to generalise its predictions to a holdout set.
-
Step 2 · Question the i.i.d. assumption
For standard machine learning, the holdout test set has the same distribution as the training set, but this may not be true for real-world data.
-
Step 3 · State the effect of adversarial training
Adversarial training, which is a form of robust training, typically shows a larger generalisation gap.
-
Step 4 · Interpret it
This suggests that, at training time, models are overfitting to the adversarially perturbed variants of the training data.
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 of key results
| Idea | Formula / fact | Where |
|---|---|---|
| Linear boundary | \(W\mathbf x + b = 0\), gradient \(\nabla_{\mathbf x} f = W^\top\) everywhere | 1.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 1 | 1.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\) multiplications | 2.1, 2.3 |
| Q2 counts | forward 16, reverse 12; merged partition: reverse 9 | 2.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 gap | train minus test accuracy; larger under adversarial training | 3.1, 3.4 |
References
- I. J. Goodfellow, J. Shlens, C. Szegedy. Explaining and harnessing adversarial examples. ICLR 2015. arxiv.org/abs/1412.6572
- A. Kurakin, I. J. Goodfellow, S. Bengio. Adversarial examples in the physical world. ICLR Workshop 2017. arxiv.org/abs/1607.02533
- 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
- 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
- L. Schmidt, S. Santurkar, D. Tsipras, K. Talwar, A. Madry. Adversarially robust generalization requires more data. NeurIPS 2018. arxiv.org/abs/1804.11285
- L. Rice, E. Wong, J. Z. Kolter. Overfitting in adversarially robust deep learning. ICML 2020. arxiv.org/abs/2002.11569
- D. Tsipras, S. Santurkar, L. Engstrom, A. Turner, A. Madry. Robustness may be at odds with accuracy. ICLR 2019. arxiv.org/abs/1805.12152
- 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
- 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
- A. Griewank, A. Walther. Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation. 2nd ed., SIAM, 2008. doi.org/10.1137/1.9780898717761
- T. Tanay, L. D. Griffin. A boundary tilting perspective on the phenomenon of adversarial examples. arXiv 2016. arxiv.org/abs/1608.07690
- 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
- 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.