Docsity

Adversarial Defenses and Certification: Exercise Solution - ETH Zurich, Lecture notes of Artificial Intelligence

Toggle sidebar
bg1
Exercise 04 - Solution
Adversarial Defenses and Certification
Reliable and Interpretable Artificial Intelligence
ETH Zurich
Problem 1 (Coding).In this task, you are going to implement adversarial training with
PGD (originally introduced in [1]) and an alternative defense.
1. Complete the provided code skeleton in train.py to train the network model
with PGD defense. That is, for data distribution D(the MNIST dataset in our
case) and network parameters θ, optimize the following objective:
min
θ
E(x,y)∼Dmax
x0∈B(x)L(θ, x0, y).(1)
Here, B(x) := {x0| kx−x0k∞≤}denotes the -sized `∞-ball around x.Lis the
usual classification loss L(θ, x0, y) := H(y , fθ(x0)), where fθ= (model ◦softmax)
denotes the output distribution of the neural network, and Hthe cross entropy1
between distributions (being a discrete value, we treat yas a one-hot distribution).
In PyTorch, you can use nn.CrossEntropyLoss2to implement L.
Use PGD to solve the inner optimization problem with = 0.1, k= 7 steps, and
step = 2.5
k. You can reuse your implementation of untargeted PGD from the
previous exercise, or create a more efficient (batched) version better suited for
training.
Compare the accuracy results with and without PGD training.
2. The TRADES [2] algorithm minimizes the following objective (see [2] for details):
1https://en.wikipedia.org/wiki/Cross_entropy
2https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html
1
pf3
pf4
pf5

Partial preview of the text

Download Adversarial Defenses and Certification: Exercise Solution - ETH Zurich and more Lecture notes Artificial Intelligence in PDF only on Docsity!

Exercise 04 - Solution

Adversarial Defenses and Certification

Reliable and Interpretable Artificial Intelligence

ETH Zurich

Problem 1 (Coding). In this task, you are going to implement adversarial training with PGD (originally introduced in [1]) and an alternative defense.

  1. Complete the provided code skeleton in train.py to train the network model with PGD defense. That is, for data distribution D (the MNIST dataset in our case) and network parameters θ, optimize the following objective:

min θ E(x,y)∼D

[

max x′∈B(x)

L(θ, x′, y)

]

Here, B(x) := {x′^ | ‖x − x′‖∞ ≤ } denotes the -sized `∞-ball around x. L is the usual classification loss L(θ, x′, y) := H(y, fθ(x′)), where fθ = (model ◦ softmax) denotes the output distribution of the neural network, and H the cross entropy^1 between distributions (being a discrete value, we treat y as a one-hot distribution). In PyTorch, you can use nn.CrossEntropyLoss^2 to implement L.

Use PGD to solve the inner optimization problem with  = 0.1, k = 7 steps, and step = 2. 5 k. You can reuse your implementation of untargeted PGD from the previous exercise, or create a more efficient (batched) version better suited for training.

Compare the accuracy results with and without PGD training.

  1. The TRADES [2] algorithm minimizes the following objective (see [2] for details):

(^1) https://en.wikipedia.org/wiki/Cross_entropy (^2) https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html

min θ E(x,y)∼D

[

L(θ, x, y) ︸ ︷︷ ︸ for accuracy

  • λ max x′∈B(x)

L(θ, x′, fθ(x)) ︸ ︷︷ ︸ regularization for robustness

]

Extend your implementation in train.py to the TRADES defense. Again, the inner optimization problem is solved using PGD. Use λ = 1.0 and the same pa- rameters as in the previous task. Compare your results with the previous task.

Note: Here, L(θ, x′, fθ(x)) still denotes the cross entropy loss. However, PyTorch’s nn.CrossEntropyLoss cannot be used as it expects one of the distributions to be a one-hot encoding of the label. Instead, you will need to manually implement L. To this end, some existing functions in PyTorch3 4^ may be useful.

Solution 1. See train.py.

Defense Clean Accuracy Adversarial Accuracy None 0.98 0. PGD 0.98 0. TRADES 0.98 0.

Table 1: Test set accuracy.

The accuracy obtained with the default parameters and 50 (instead of 10) epochs is shown in table 1. Without any defense, the adversarial accuracy drops significantly, while it remains high with a defense. Note that the PGD defense outperformed TRADES here (higher adversarial accuracy at the same clean accuracy)—MNIST is a very “easy” task where adversarial training does not impact the clean accuracy too much. Note that the model and hyper-parameters used here are far from optimal.

Problem 2 (Alternating optimization). We formalized adversarial defense as the opti- mization problem in (1). In general, finding the globally optimal parameters θ∗^ is hard due to the nesting of maximization and minimization. In the lecture, we considered a method that approximates θ∗^ by individually solving the outer and inner optimization problem in alternation. In this task, you will show that this approach can result in a local minimum that is not globally optimal.

Consider the basic case in one dimension where the data is of the form (x, y) with discretized x ∈ Z and label y ∈ { 0 , 1 }. Assume a very simple classifier ρ in one parameter

(^3) https://pytorch.org/docs/stable/nn.html#loss-functions (^4) https://pytorch.org/docs/stable/generated/torch.nn.LogSoftmax.html#torch. nn.LogSoftmax

0 01 1

0 1 2 3 4 5 6 7 8 ... ...

0 1 2 3 4 5 6 7 8 ... ...

0 1 2 3 4 5 6 7 8 ... ...

0 1 2 3 4 5 6 7 8 ... 0 1 01 0 1 ...

0 1 0 1

0 1 2 3 4 5 6 7 8 0 1 ...^0 ...

0 0 0 1 1 0 1 1

0 1 2 3 4 5 6 7 8 ... 0 ...

Figure 1: (a–e) Example stuck in a local optimum. Left: Optimizing the parameter θ for given data points. Right: Adversarial examples maximizing the loss for a given parameter θ. (f) Global optimum.

Problem 3 (Box Transformers). In the lecture, you have seen the dox domain for numerical analysis. For vectors a, b ∈ Rm^ with ∀i. ai ≤ bi, the box [a, b] is a hypercube in Rm. We can use abstract transformers to obtain an over-approximation of the behavior of a function. Given a function f : Rn^ → Rm^ and an input box [a, b] ⊂ Rn, a sound abstract transformer f ]^ finds [c, d] ⊂ Rm^ such that ∀x ∈ [a, b]. f (x) ∈ [c, d].

For example, let x ∈ [1, 3] and y ∈ [2, 4], and assume we want to approximate the result of 2x − y. Using the basic abstract transformers from the lecture, we can compute

2 ·]^ [1, 3] −]^ [2, 4] = [2, 6] +]^ [− 4 , −2] = [− 2 , 4]

and conclude that 2x − y ∈ [− 2 , 4].

  1. Show that the box transformers lose precision, by approximating the outcome of x − x for x ∈ [0, 1] using the transformers +]^ and −]^ from the lecture.
  2. Prove or disprove: The alternative transformer [a, b] +′^ [c, d] = [a + c, b + |d|] for addition is sound (i.e., the output box is an over-approximation of all possible result values).
  3. Prove or disprove: The alternative transformer [a, b] +′′^ [c, d] = [−∞, a + b + d] for addition is sound.
  4. Derive a sound abstract transformer f ]^ for the function f (x) := x^2. That is, derive expressions for g, h such that [g, h] = f ]([a, b]) for a, b ∈ R.
  5. Derive a sound abstract transformer ·]^ for multiplication. That is, derive expres- sions for g, h such that [g, h] = [a, b] ·]^ [c, d], where a, b, c, d ∈ R.

Solution 3.

  1. It is [0, 1] −]^ [0, 1] = [0, 1] +]^ [− 1 , 0] = [− 1 , 1]. However, the only possible result of x − x for x ∈ [0, 1] is 0, which could be represented exactly using the more precise interval [0, 0].
  2. This is true. Let x ∈ [a, b] and y ∈ [c, d]. Hence, a ≤ x ≤ b and c ≤ y ≤ d. Now consider the sum z := x + y. It is a + c ≤ z and z ≤ b + d ≤ b + |d|, because d ≤ |d|. Therefore, z ∈ [a + c, b + |d|].
  3. This is not true. Consider [− 1 , −1] +′′^ [0, 0] = [−∞, −2], which does not include value −1 produced by −1 + 0.
  4. Recall that a ≤ b. We have to distinguish the three general positions of the interval w.r.t. 0: below (b ≤ 0), above (a ≥ 0), or including 0 (otherwise).

[g, h] = f ]([a, b]) =

[a^2 , b^2 ] if a ≥ 0, [b^2 , a^2 ] else if b ≤ 0, [0, max(a^2 , b^2 )] otherwise.

[a, b] ·]^ [c, d] = [min(ac, ad, bc, bd), max(ac, ad, bc, bd)]

Note: To see this, perform a case distinction on the signs of a, b, c, and d. The naive solution attempt [ab, cd] is unsound (why?).

References

[1] Aleksander Madry et al. ”Towards deep learning models resistant to adversarial attacks”. ICLR (2018).

[2] Hongyang Zhang et al. ”Theoretically Principled Trade-off between Robustness and Accuracy”. ICML (2019).