Improved Adversarial Training via Learned Optimizer

Adversarial attack has recently become a tremendous threat to deep learning models. To improve the robustness of machine learning models, adversarial training, formulated as a minimax optimization problem, has been recognized as one of the most effective defense mechanisms. However, the non-convex and non-concave property poses a great challenge to the minimax training. In this paper, we empirically demonstrate that the commonly used PGD attack may not be optimal for inner maximization, and improved inner optimizer can lead to a more robust model. Then we leverage a learning-to-learn (L2L) framework to train an optimizer with recurrent neural networks, providing update directions and steps adaptively for the inner problem. By co-training optimizer's parameters and model's weights, the proposed framework consistently improves the model robustness over PGD-based adversarial training and TRADES.


Introduction
It has been widely acknowledged that deep neural networks (DNN) have made tremendous breakthroughs benefiting both academia and industry. Despite being effective, many DNN models trained with benign inputs are vulnerable to small and undetectable perturbation added to original data and tend to make wrong predictions under such threats. Those perturbed examples, also known as adversarial examples, can be easily constructed by algorithms such as Deep-Fool [23], Fast Gradient Sign Method (FGSM) [11], and Carlini-Wagner (C&W) attack [4]. Moreover, such adversarial attacks can also be conducted in the blackbox setting [3,5,6] and can appear naturally in the physical world [12,16]. This phenomenon can bring about serious consequences in domains such as face recognition and autonomous-driving. Therefore, how to train a model resistant to adversarial inputs has become an important topic.
A variety of defense methods have been proposed to improve the performance of DNNs against adversarial attacks [17,27,31,32,36,39]. Among them, adversarial training [17] stands out for its effectiveness. Moreover, [21] shows that adversarial training can be formulated as a minimax optimization problem, resembling a game between the attacker and the defender. The formulation is so intuitive that the inner problem aims at generating adversarial examples arXiv:2004.12227v1 [cs.LG] 25 Apr 2020 by maximizing the training loss while the outer one guides the network in the direction that minimizes the loss to resist attacks. However, directly obtaining the optimal value of the inner maximization is infeasible, so one has to run an iterative optimization algorithm for a fixed number (often 10) iterations to get an approximate inner maximizer.
Existing adversarial training often uses hand-designed general purpose optimizers, such as PGD attack, to (approximately) solve the inner maximization. However, there is an essential property of adversarial training that is rarely explored: the maximization problems associated with each sample share very similar structure, and a good inner maximizer for adversarial training only needs to work well for this set of data-dependent problems. To be specific, there are a finite of n maximization problems need to be solved (where n is number of training samples), and those maximization problems share the same objective function along with identical network structure and weights, and the only difference is their input x. Based on this observation, can we have a better optimizer that in particular works well for these very similar and data-dependent problems?
Motivated by this idea, we propose a learned optimizer for improved adversarial training. Instead of using an existing optimizer with a fixed update rule (such as PGD), we aim at learning the inner maximizer that could be faster and more effective for this particular set of maximization problems. We have noticed that two works have already put forward algorithms to combine learning to learn with adversarial training [14,13]. Both of them adopt a convolutional neural network (CNN) generator to produce malicious perturbations whereas CNN structure might complicate the training process and cannot grasp the essence of the update rule in the long term. In contrast, we propose an L2L-based adversarial training method with recurrent neural networks (RNN). RNN is capable of capturing long-term dependencies and has shown great potentials in predicting update directions and steps adaptively [20]. Thus, following the framework in [1], we leverage RNN as the optimizer to generate perturbations in a coordinate-wise manner. Based on the properties of the inner problem, we tailor our RNN optimizer with removed bias and weighted loss for further elaborations to ameliorate issues like short-horizon in L2L [34].
Specifically, our main contributions in this paper are summarized as follows: -We first investigate and confirm the improvement in the model robustness from stronger attacks by searching a suitable step size for PGD. -In replacement of hand-designed algorithms like PGD, an RNN-based optimizer based on the properties of the inner problem is designed to learn a better update rule. In addition to standard adversarial training, the proposed algorithm can also be applied to any other minimax defense objectives such as TRADES [39]. -Comprehensive experimental results show that the proposed method can noticeably improve the robust accuracy of both adversarial training [21] and TRADES [39]. Furthermore, our RNN-based adversarial training significantly outperforms previous CNN-based L2L adversarial training and requires much less number of trainable parameters.

Adversarial Attack and Defense
Model robustness has recently become a great concern for deploying deep learning models in real-world applications. Goodfellow et al. [11] succeeded in fooling the model to make wrong predictions by Fast Gradient Sign Method (FGSM). Subsequently, to produce adversarial examples, IFGSM and Projected Gradient Descent (PGD) [11,21] accumulate attack strength through running FGSM iteratively, and Carlini-Wagner (C&W) attack [4] designs a specific objective function to increase classification errors. Besides these conventional optimization-based methods, there are several algorithms [25,35] focusing on generating malicious perturbations via neural networks. For instance, Xiao et al. [35] exploit GAN, which is originally designed for crafting deceptive images, to output corresponding noises added to benign iuput data. The appearance of various attacks has pushed forward the development of effective defense algorithms to train neural networks that are resistant to adversarial examples. The seminal work of adversarial training has significantly improved adversarial robustness [21]. It has inspired the emergence of various advanced defense algorithms: TRADES [39] is designed to minimize a theoretically-driven upper bound and GAT [19] takes generator-based outputs to train the robust classifier. All these methods can be formulated as a minimax problem [21], where the defender makes efforts to mitigate negative effects (outer minimization) brought by adversarial examples from the attacker (inner maximization). Whereas, performance of such an adversarial game is usually constrained by the quality of solutions to the inner problem [13,14]. Intuitively, searching a better maxima for the inner problem can improve the solution of minimax training, leading to improved defensive models.

Learning to Learn
Recently, learning to learn emerges as a novel technique to efficiently address a variety of problems such as automatic optimization [1], few-shot learning [10], and neural architecture search [8]. In this paper, we emphasize on the subarea of L2L: how to learn an optimizer for better performance. Rather than using human-defined update rules, learning to learn makes use of neural networks for designing optimization algorithms automatically. It is developed originally from [7] and [37], in which early attempts are made to model adaptive algorithms on simple convex problems. More recently, [1] proposes an LSTM optimizer for some complex optimization problems, such as training a convolutional neural network classifier. Based on this work, elaborations in [20] and [33] further improve the generalization and scalability for learned optimizers. Moreover, [26] demonstrates that a zeroth order optimizer can also be learned using L2L. Potentials of learning-to-learn motivates a line of L2L-based defense which replaces hand-designed methods for solving the inner problem with neural network optimizers. [14] uses a CNN generator mapping clean images into corresponding perturbations. Since it only makes one-step and deterministic attack like FGSM, [13] modifies the algorithm and produces stronger and more diverse attacks iteratively. Unfortunately, due to the large number of parameters and the lack of ability to capture the long-term dependencies, the CNN generator adds too much difficulty in the optimization, especially for the minimax problem in adversarial training. Therefore, we adopt an RNN optimizer in our method for a more stable training process as well as a better grasp of the update rule.

Notations
We use bold lower-case letters x and y to represent clean images and their corresponding labels. An image classification task is considered in this paper with the classifier f parameterized by θ. sign(·) is an elementwise operation to output the sign of a given input with sign(0) = 1. B(x, ) denotes the neighborhood of x as well as the set of admissible perturbed images: where the infinity norm is adopted as the distance metric. We denote by Π the projection operator that maps perturbed data to the feasible set. Specifically, , which is an elementwise operator. L(·, ·) is a multi-class loss like cross-entropy.

Adversarial Training
In this part, we present the formulation of adversarial training, together with some hand-designed optimizers to solve this problem. To obtain a robust classifier against adversarial attacks, an intuitive idea is to minimize the robust loss, defined as the worst-case loss within a small neighborhood B(x, ). Adversarial training, which aims to find the weights that minimize the robust loss, can be formulated as a minimax optimization problem in the following way [21]: where D is the empirical distribution of input data. However, (1) only focuses on accuracy over adversarial examples and might cause severe over-fitting issues on the training set. To address this problem, TRADES [39] investigates the tradeoff between natural and robust errors and theoretically puts forward a different objective function for adversarial training: Note that (1) and (2) are both defined as minimax optimization problems, and to solve such saddle point problems, a commonly used approach is to first get an approximate solution x of inner maximization based on the current θ, and then use x to conduct updates on model weights θ. The adversarial training procedure then iteratively runs this on each batch of samples until convergence. Clearly, the quality and efficiency of inner maximization is crucial to the performance of adversarial training. The most commonly used inner maximizer is the projected gradient descent algorithm, which conducts a fixed number of updates: Here L(x t ) represents the maximization term in (1) or (2) with abuse of notation.

Effects of Adaptive Step Sizes
We found that the performance of adversarial training crucially depends on the optimization algorithm used for inner maximization, and the current widely used PGD algorithm may not be the optimal choice. Here we demonstrate that even a small modification of PGD and without any change to the adversarial training objective can boost the performance of model robustness. We use the CNN structure in [39] to train a classifier on MNIST dataset. When 10-step PGD (denoted by PGD for simplicity) is used for the inner maximization, a constant step size is always adopted, which may not be suitable for the subsequent update. Therefore, we make use of backtracking line search (BLS) to select a step size adaptively for adversarial training (AdvTrain as abbreviation). Starting with a maximum candidate step size value α 0 , we iteratively decrease it by α t = ρα t−1 until the following condition is satisfied: where p = ∇ x L(x ) is a search direction. Based on a selected control parameter c ∈ (0, 1), the condition tests whether the update with step size α t leads to sufficient increase in the objective function, and it is guaranteed that a sufficiently small α will satisfy the condition so line search will always stop in finite steps. This is standard in gradient ascent (descent) optimization, and see more discussions in [24]. Following the convention, we set ρ = 0.5 and c = 10 −4 . As shown in Table 1 Classifier f Classifier f

Learning to Learn for Adversarial Training
As mentioned in the previous section, it can be clearly seen that the inner maximizer plays an important role in the performance of adversarial training. However, despite the effectiveness of BLS introduced in Section 3.3, it is impractical to combine it with adversarial training as multiple line searches together with loss calculation in this algorithms increase its computational burden significantly. Then a question arises naturally: is there any automatic way for determining a good step size for inner maximization without too much computation overhead? Moreover, apart from the step size, the question can be extended to whether such a maximizer can be learned for a particular dataset and model in replacement of a general optimizer like PGD. Recently, as a subarea of learning-to-learn, researchers have been investigating whether it is possible to use machine learning, especially neural networks, to learn improved optimizer to replace the handdesigned optimizer [1,20,33]. However, it is commonly believed that those MLlearned general-purpose optimizers are still not practically useful due to several unsolved issues. For instance, the exploded gradient [22] in unrolled optimization impedes generalization of these learned optimizers to longer steps and truncated optimization on the other hand induces short-horizon bias [34].
In this paper, we show it is possible and practical to learn an optimizer for inner maximization in adversarial training. Note that in adversarial training, the maximization problems share very similar form: max x ∈B(x, ) L(f (x ), y), where they all have the same loss function L and the same network (structure and weights) f , and the only difference is their input x and label y. Furthermore, we only need the maximizer to perform well on a fixed set of n optimization problems for adversarial training. These properties thus enable us to learn a better optimizer that outperforms PGD.
To allow a learned inner maximizer, we parameterize the learned optimizer by an RNN network. This is following the literature of learning-to-learn [1], but we propose several designs as shown below that works better for our inner maximization problem which is a constrained optimization problem instead of a standard unconstrained training task in [1]. We then jointly optimize the classifier parameters (θ) as well as the parameters of the inner maximizer (φ). The overall framework can be found in Figure 1.
Specifically, the inner problem is to maximize vanilla adversarial training loss in (1) or TRADES loss in (2), with a constraint that x ∈ B(x, ). We expand on adversarial training here and more details about TRADES can be found in Appendix A. With an RNN optimizer m parameterized by φ, we propose the following parameterized update rule to mimic the PGD update rule in (3): Here, g t is the gradient ∇ x L(f (x ), y) and h t is the hidden state representation. It has to be emphasized that our RNN optimizer generates perturbations coordinate-wisely, in contrast to other L2L based methods which take as input the entire image. This property reduces trainable parameters significantly, making it much easier and faster for training. In addition, note that the hidden state of our RNN optimizer plays an important role in the whole optimization. A separate hidden state for each coordinate guarantees the different update behavior.
And it contains richer information like the trajectory of loss gradients mentioned in [13] but can produce a recursive update with a simpler structure. For the RNN design, we mainly follow the structure in [1] but with some modifications to make it more suitable to adversarial training. We can expand the computation of perturbation for each step as: where h t ∈ R d , V ∈ R 1×d , U ∈ R d×1 , W ∈ R d×d , b 1 ∈ R and b 2 ∈ R d in the coordinate-wise update manner. As the optimization proceeds, the gradient will become much smaller when approaching the local maxima. At that time, a stable value of the perturbation is expected without much change between two consecutive iterations. However, from (6) and (7), we can clearly see that despite small g t , the update rule will still produce an update with magnitude proportional to tanh(b 1 ). Imagine the case where the exact optimal value is found with an all-zero hidden state (b 2 needs to be zero as well), δ t = tanh(b 1 ) with a non-zero bias will push the adversarial example away from the optimal one. Thus, two bias terms b 1 and b 2 are problematic for optimization close to the optimal solution. Due to the short horizon of the inner maximization in adversarial training, it is unlikely for the network to learn zero bias terms. Therefore, to ensure stable training, we remove the bias terms in the vanilla RNN in all implementations.
With an L2L framework, we simultaneously train the RNN optimizer parameters φ and the classifier weights θ together. The joint optimization problem can be formulated as follows: where x T (φ * ) is computed by running Eq.(5) T times iteratively. Since the learned optimizer aims at finding a better solution to the inner maximization term, the objective function for training it in the horizon T is defined as: Note that if we set w t = 0 for all t < T and w T = 1, then (10) implies that our learned maximizer m φ will maximize the loss after T iterations. However, in practice we found that considering intermediate iterations can further improve the performance since it will make the maximizer converges faster even after conducting one or few iterations. Therefore in the experiments we set an increased weights w t = t for t = 1, . . . , T . Note that [22] showed that this kind of unrolled optimization may lead to some issues such as exploded gradients which is still an unsolved problem in L2L. However, in adversarial training we only need to set a relative small T (e.g., T = 10) so we do not encounter that issue. While updating the learned optimizer, corresponding adversarial examples are produced together. We can then train the classifier by minimizing the loss accordingly. The whole algorithm is presented in Algorithm 1.

Advantages over Other L2L-based Methods
Previous methods have proposed to use a CNN generator [13,14] to produce perturbations in adversarial training. However, CNN-based generator has a larger number of trainable parameters, which makes it hard to train. In Table 2, the detailed properties including the number of parameter and training time per epoch are provided for different learning-to-learn based methods. We can observe that our proposed RNN approach stands out with the smallest parameters as well as efficiency in training. Specifically, our RNN optimizer only has 120 parameters, almost 5000 times fewer than L2LDA while the training time per epoch is 268.50s (RNN-TRADES only consumes 443.52s per training epoch) v.s. 1972.41s. Furthermore, our method also leads to better empirical performance, as shown in our main comparison in Table 3, 4 and 5. Comparison of our variants and original adversarial training methods can be found in Appendix B.

Experimental Results
In this section, we present experimental results of our proposed RNN-based adversarial training. We compare our method with various baselines against both white-box and black-box attack. In addition, different datasets and network architectures are also evaluated. Sample a mini-batch M from clean data. 6: for (x, y) in B do 7: Initialization: h0 ← 0, L θ ← 0, L φ ← 0 8: Gaussian augmentation: x 0 ← x + 0.001 · N (0, I) 9: for t = 0, . . . , T − 1 do 10: δt, ht+1 ← m φ (gt, ht), where coordinate-wise update is applied 12: x t+1 ← Π B(x, ) (x t + δt) 13: end for 15:  [4] is adopted as the classifier. For CIFAR-10, we use both the standard VGG-16 [28] and Wide ResNet [38], which has been used in most of the previous defense papers including adversarial training [21] and TRADES [39]. We also conduct an additional experiment on Restricted ImageNet [30] with ResNet-18 and results are presented in Appendix C. -Baselines for Comparison. Note that our method is an optimization framework which is irrelevant to what minimax objective function is used. Therefore we choose two most popular minimax formulations, AdvTrain 1 [21] and TRADES 2 [39], and substitute the proposed L2L-based optimization for their original PGD-based algorithm. Moreover, we also compare with a previous L2L defense mechanism L2LDA 3 [13] which outperforms other L2Lbased methods for thorough comparison. We use the source code provided by the authors on github with their recommended hyper-parameters for all these baseline methods. -Evaluation and implementation details. Defense algorithms are usually evaluated by classification accuracy under different attacks. Effective attack algorithms including PGD, C&W and the attacker of L2LDA are used for evaluating the model robustness, with the maximum ∞ perturbation strength = 0.3 for MNIST and = 8/255 for CIFAR-10. For PGD, we run 10 and 100 iterations (PGD-10 and -100) with the step size η = /4, as suggested in [13]. C&W is implemented with 100 iterations in the infinity norm. For L2LDA attacker, it is learned from L2LDA [13] under different settings with 10 attack steps. In addition, we also uses the learned optimizer of RNN-Adv to conduct 10-step attacks.
For our proposed RNN-based defense, we use a one-layer vanilla RNN with the hidden size of 10 as the optimizer for the inner maximization. Since we test our method under two different minimax losses, we name them as RNN-Adv and RNN-TRADES respectively. The classifier and the optimizer are updated alternately according to the Algorithm 1. All algorithms are implemented in PyTorch-1.1.0 with four NVIDIA 1080Ti GPUs. Note that all adversarial training methods adopt 10-step inner optimization for fair comparison. We run each defense method five times with different random seeds and report the lowest classification accuracy.

Performance on White-box Attacks
We demonstrate the robustness of models trained from different defense methods under the white-box setting in this part. Experimental results are shown in Table 3, 4 and 5. From these three tables, we can observe that our proposed L2L-based adversarial training with RNN always outperforms its counterparts. To be specific, our method achieves 95.80% robust accuracy among various attacks on MNIST dataset. On CIFAR-10, RNN-TRADES reaches 47.23% and 54.11 for VGG-16 and Wide ResNet with 1.28% and 1.43% gain over other baselines. It should be stressed that our method surpasses L2LDA (the previous CNN-based L2L method) noticeably. For conventional defense algorithms, our L2L-based variant improves the original method by 1% − 2% percents under different attacks from comparison of robust accuracy in AdvTrain and RNN-Adv. A similar phenomenon can also be observed in TRADES and RNN-TRADES. Since previous works of L2L-based defense only concentrate on PGD-based adversarial training, the substantial performance gain indicates that the learned optimizer   can contribute to the minimax problem in TRADES as well. Furthermore, apart from traditional attack algorithms, we leverage our RNN optimizer learned from adversarial training as the attacker (the column RNN-Adv). Results in three experiments show that compared with other general attackers when conducting 10 iterations such as PGD-10 and L2LDA, ours is capable of producing much stronger perturbations which lead to low robust accuracy.

Analysis
Learned Optimizer. As mentioned in Section 5.2, the optimizer learned from PGD-based adversarial training can be regarded as an special attacker. Thus, we primarily investigate the update trajectories of different attackers to obtain an in-depth understanding of our RNN optimizer. For VGG-16 models trained from four defense methods, three attacker are used to generate perturbations in 10 steps respectively and losses are recorded as shown in Figure 2.
We can see clearly from these four figures that the losses obtained from RNN-Adv are always larger than others within 10 iterations, reflecting stronger attacks produced by our proposed optimizer. Moreover, it should be noted that the loss gap between RNN-Adv and other attackers is much more prominent at some very beginning iterations. This in fact demonstrates an advantage of the learning-to-learn framework that the optimizer can converge faster than handdesigned algorithms. Generalization to more attack steps. Although our learned RNN optimizer is only trained under 10 steps, we show that it can generalize to more steps as an attacker. From Table 6, we can observe that the attacker is capable of producing much stronger adversarial examples by extending its attack steps to 40. Performance of our attacker is even comparable with that of PGD-100, which further demonstrates the superiority of our proposed method.

Performance on Black-box Transfer Attacks
We further test the robustness of the proposed defense method under transer attack. As suggested by [2], this can be served as a sanity check to see whether our defense leads to obfuscated gradients and gives a false sense of model robustness. Following procedures in [2], we first train a surrogate model with the same architecture of the target model using a different random seed, and then generate adversarial examples from the surrogate model to attack the target model.  Specifically, we choose VGG-16 models obtained from various defense algorithms as our target models. In the meanwhile, we train two surrogate models: one is Plain-Net with natural training and the other is PGD-Net with 10-step PGD-based adversarial training. Results are presented in Table 7. We can observe that our method outperforms all other baselines, with RNN-PGD and RNN-TRADES standing out in defending attacks from Plain-Net and PGD-Net respectively. It suggests great resistance of our L2L defense to transfer attacks.

Loss Landscape Exploration
To further verify the superior performance of the proposed algorithm, we visualize the loss landscapes of VGG-16 models trained under different defense strategies, as shown in Figure 3. According to the implementation in [9], we modify the input along a linear space defined by the sign of the gradient and a random Rademacher vector, where the x and y axes represent the magnitude of the perturbation added in each direction and the z axis represents the loss. It can be observed that loss surfaces of models trained from RNN-Adv and RNN-TRADES in Figure 3e and 3f are much smoother than those of their counterparts in Figure 3b and 3c. Besides, our method significantly reduces the loss value of perturbed data close to the original input. In particular, the maximum loss decreases roughly from 7.61 in adversarial training to 3.66 in RNN-Adv. Compared with L2LDA in Figure 3d, the proposed RNN optimizer can contribute to less bumpier loss landscapes with smaller variance, which further demonstrates the stability and superiority of our L2L-based adversarial training.

Conclusion
For defense mechanisms that can be formulated as a minimax optimization problem, we propose to replace the inner PGD-based maximizer with a automatically learned RNN maximizer, and show that jointly training the RNN maximizer and classifier can significantly improve the defense performance. Empirical results demonstrate that the proposed approach can be combined with several minimax defense objectives, including adversarial training and TRADES. For future work, it can be a worthwhile direction to address the inadequacy of L2L in dealing with a long-horizon problem. Then we can substitute the learned optimizer for hand-designed algorithms in both inner and outer problems, which enables an entirely automatic process for adversarial training.

A Algorithm for TRADES
As we have emphasized, our proposed method can be incorporated into any adversarial training which can be formulated as a minimax optimization problem. Here we provide the detailed algorithm of RNN-TRADES in Algorithm 2.

B.1 Time comparison
In this section, we compared training time of our proposed methods with the original adversarial training. We still conduct analysis of VGG-16 on CIFAR-10 dataset. From results in Table 8, it can be observed that our methods approximately double the overall training time per epoch, which is not a heavy burden with improved performance taken into account.

B.2 Trajectory
A similar trajectory can be observed in terms of classification accuracy as well when the model is attacked by different attackers. In Figure 4, the robust accuracy under RNN-Adv drops most rapidly and also achieves the lowest point after Table 8. Time comparison with original adversarial training. Here we report the ratio of our proposed method to its original counterpart, for example, T RNN-Adv /T AdvTrain . In the parentheses, we report the training time per epoch of our proposed method including RNN-Adv and RNN-TRADES