December 22, 2021
The fine-tuning of pre-trained language models has a great success in many NLP fields. Yet, it is strikingly vulnerable to adversarial examples, e.g., word substitution attacks using only synonyms can easily fool a BERT-based sentiment analysis model. In this paper, we demonstrate that adversarial training, the prevalent defense technique, does not directly fit a conventional fine-tuning scenario, because it suffers severely from catastrophic forgetting: failing to retain the generic and robust linguistic features that have already been captured by the pre-trained model. In this light, we propose Robust Informative Fine-Tuning (RIFT), a novel adversarial fine-tuning method from an information-theoretical perspective. In particular, RIFT encourages an objective model to retain the features learned from the pre-trained model throughout the entire fine-tuning process, whereas a conventional one only uses the pre-trained weights for initialization. Experimental results show that RIFT consistently outperforms the state-of-the-arts on two popular NLP tasks: sentiment analysis and natural language inference, under different attacks across various pre-trained language models. 1.
Deep models are well-known to be vulnerable to adversarial examples [1]–[4]. For instance, fine-tuned models pre-trained on very large corpora can be easily fooled by word substitution attacks using only synonyms [5]–[8]. This has raised grand security challenges to modern NLP systems, such as spam filtering and malware detection, where pre-trained language models like BERT [9] are widely deployed.
Attack algorithms [2], [5], [10]–[12] aim to maliciously generate adversarial examples to fool a victim model, while adversarial defense aims at building robustness against them. Among the defense methods, adversarial training [1], [2], [13], [14] is the most effective one [15]. It updates model parameters using perturbed adversarial samples generated on-the-fly and yields consistently robust performance even against the challenging adaptive attacks [15], [16].
However, despite its effectiveness in training from scratch, adversarial training may not directly fit the current NLP paradigm, the fine-tuning of pre-trained language models. First, fine-tuning per se suffers from catastrophic forgetting [17]–[19], i.e., the resultant model tends to over-fit to a small fine-tuning data set, which may deviate too far from the pre-trained knowledge [20], [21]. Second, adversarially fine-tuned models are more likely to forget: adversarial samples are usually out-of-distribution [22], [23], thus they are generally inconsistent with the pre-trained model. As a result, adversarial fine-tuning fails to memorize all the robust and generic linguistic features already learned during pre-training [24], [25], which are however very beneficial for a robust objective model.
Addressing forgetting is essential for achieving a more robust objective model. Conventional wisdom such as pre-trained weight decay [26], [27] and random mixout [28] mitigates forgetting by constraining \(l_p\) distance between the two models’ parameters. Though effective to some extent, however, it is limited because change in the model parameter space only serves as an imperfect proxy for that in the function space [29]. Besides, the extent to which an encoder fails to retain information also depends on the input distribution. Therefore, a better way to encourage memorization is favorable.
In this paper, we follow an information-theoretical lens to look into the forgetting problem: we employ mutual information to measure how well an objective model memorizes the useful features captured before. This motivates our novel adversarial fine-tuning method, Robust Informative Fine-Tuning (RIFT). In addition to fitting a down-stream task as conventions, RIFT maximizes the mutual information between the output of an objective model and that of the pre-trained model conditioned on the class label. It encourages an objective model to continuously retain useful information from the pre-trained one throughout the whole fine-tuning process, whereas the conventional one only makes use of the pre-trained weights for initialization. We illustrate the overall objective of RIFT in Figure 1 and summarize the major contributions of this paper as follows:
To the best of our knowledge, we are the first to investigate the intensified phenomenon of catastrophic forgetting in the adversarial fine-tuning of pre-trained language models.
To address the forgetting problem and achieve more robust models, we propose a novel approach named Robust Informative Fine-Tuning (RIFT) from an information-theoretical perspective. RIFT enables an objective model to retain robust and generic linguistic features throughout the whole fine-tuning process and thus enhance robustness against adversarial examples.
We empirically evaluate RIFT on two prevailing NLP tasks, sentiment analysis and natural language inference, where RIFT consistently outperforms the state-of-the-arts in terms of robustness, under different attacks across various pre-trained language models.
In this paper, we focus on the text classification task to introduce our method, while it can be easily extended to other NLP tasks. We suppose random variables \(X, Y\sim p_{\mathcal{D}}(x,y)\), where \(X\) represents the textual input, \(Y\) represents the class label, \(p_{\mathcal{D}}\) is the data distribution, and \(x,y\) are the observed values. Our goal is to build a classifier \(q(y|F_s(x))\), where \(F_s(\cdot)\), referred to as our objective model in the rest of this paper, is a feature extractor fine-tuned from the encoder of a pre-trained language model, e.g., Transformer [30], and \(q(\cdot|\cdot)\) is parameterized by using an MLP with softmaxed output. We favor a classifier that is robust against adversarial attacks [1], [5], i.e., maintaining high accuracy even given adversarial examples as inputs.
Adversarial training [1], [2], [14] is currently the most effective defense technique [15]. Its training objective can be generally formulated as: \[\begin{align} \min [\mathop{\mathbb{E}}\limits_{x,y \sim p_{\mathcal{D}}}[ \max\limits_{~\hat{x}\in \mathbb{B}(x)} \mathcal{L}(x, \hat{x}, y) ]],\label{eq:general32ad32train} \end{align}\tag{1}\] where \(\mathbb{B}(x)\) defines the allowed perturbation space around \(x\), and \(\mathcal{L}\) is a loss to encourage correct predictions both given vanilla samples and given perturbed examples. Such an objective function can also be applied to the fine-tuning of a pre-trained language model towards robustness, and we refer to it as adversarial fine-tuning. The loss function in Eq. 1 can be specified following [13], [31] in a semi-supervised fashion as: \[\begin{align} -\log q(y|F_s(x)) + \beta {\rm KL}\big(q(\cdot|F_s(x))||q(\cdot|F_s(\hat{x}))\big),\label{eq:klad} \end{align}\tag{2}\] where the Kullback–Leibler divergence encourages invariant predictions between \(x\) and \(\hat{x}\).
Figure 2: Relative \(L_2\) distance between the pre-trained model and the objective model in the parameter space, under different fine-tuning schemes on IMDB, with small learning rate 2e-5.. a — In
adversarial fine-tuning, the relative \(L_2\) dist-
ance continuously grows as the fine-tuning proceeds., b — Trading clean accuracy off against robustness (by increasing \(\beta\) in Eq. 2 ) induces increased relative \(L_2\) distance at the last epoch.
Despite its effectiveness in training from scratch, such an adversarial objective function may not directly fit the fine-tuning scenario. To begin with, fine-tuning itself suffers from catastrophic forgetting [17]–[19]. During fine-tuning, an objective model \(F_s(\cdot)\) tends to continuously deviate away from the pre-trained one to fit a down-stream task [20], [21], and thus the useful information already captured before is less utilized as the fine-tuning proceeds. Further, adversarial fine-tuning suffers even more from the forgetting problem, the reasons of which are given as what follows.
(i) Adversarial Fine-Tuning Tends to Forget: Adversarial fine-tuning targets at tackling adversarial examples, which are generally out of the manifold [22], [23] of the pre-training corpora. To additionally handle them, an objective model would be fine-tuned towards a solution that is far away from the optimization starting point, i.e., the pre-trained model. Figure 2 (b) empirically shows this point: by increasing \(\beta\) in Eq. 2 , we emphasize more on robustness instead of vanilla accuracy, and consequently, at the last epoch the distance between models also increases. Besides, adversarial fine-tuning often entails more iterations to converge (several times of normal fine-tuning), which further intensifies the forgetting problem, as the objective model is continuously deviating away as shown in Figure 2 (a).
(ii) Adversarial Fine-Tuning Needs to Memorize: Overfitting to training data is a dominant phenomenon throughout the adversarial training/fine-tuning process [32]. To mitigate overfitting and generalize better, adversarial fine-tuning should retain all the generalizable features already captured by the pre-trained model [9], [33]. In addition, adversarial fine-tuning favors an objective model that extracts features invariant to perturbations [34], [35] for robustness. As such, all those generalizable and robust linguistic information captured during pre-training [9], [24] are particularly beneficial and should be memorized. We empirically validates that encouraging memorization does improve both generalization and robustness in Sec. 3.5.
To address forgetting, previous methods such as pre-trained weight decay [26], [27] and random mixout [28] have shown their effectiveness in stabilizing fine-tuning [21]. However, they focus only on the parameter space, in which they encourage an object model to be similar to the pre-trained one. Distance in the parameter space can only approximately characterize the change in the function space, and fails to take the data distribution into consideration. A more natural way to capture the extent to which a model memorizes or forgets, should be using the mutual information between outputs of the two models, and we provide our solution as follows.
We first use an information-theoretical perspective to look into how a pre-trained model should be leveraged throughout the whole fine-tuning process.
We define random variable \(T=F_t(X)\) as the feature of \(X\) extracted by the pre-trained language model \(F_t(\cdot)\), and \(t\) as the observed value of \(T\). Similarly, we define random variable \(S=F_s(X)\) as the feature of \(X\) extracted by our objective model \(F_s(\cdot)\), and \(s\) as the observed value of \(S\). We formulate an overall fine-tuning objective as follows. The motivation is to train \(F_s(\cdot)\) such that \(S\) is capable of predicting \(Y\), as well as preserving the information from \(T\). \[\begin{align} \max {I}(S;~Y,T),\label{eq:mi} \end{align}\tag{3}\] i.e., maximizing the mutual information between (i) the feature extracted by the objective model and (ii) the class label plus the feature extracted by the pre-trained model. Since the pre-trained model \(F_t(\cdot)\) is a fixed deterministic function, the fine-tuning objective in Eq. 3 in essense encourages \(F_s(\cdot)\) to output features that contain as much information as possible for predicting \(Y\) and \(T\). This enables the objective model to learn from the pre-trained language model via \(T\) throughout the whole fine-tuning process, and thus helps address the forgetting problem.
However, the objective defined in Eq. 3 is generally hard to optimize directly. Therefore, we decompose Eq. 3 into two terms as follows: \[\begin{align} {I}(S;~Y, T) = {I}(S;Y) + {I}(S;T|~Y), \label{eq:decompose} \end{align}\tag{4}\] where \({I}(S;Y)\) measures how well the output of our objective model can predict the label, and \({I}(S;T|~Y)\) measures when conditioned on the class label, how well the output features of the two models can predict each other. Visualization of each component can be seen in Figure 1 for more intuitive understandings. We next introduce how each term in the right-hand side of Eq. 4 can be transformed into a tractable lower bound for optimization.
(i) Maximizing I(S;Y): We treat \(q(y|s)\), which is the classification layer that takes the features extracted by the objective model as input, as a variational distribution of \(p(y|s)\), and derive a variational lower bound on \({I}(S;Y)\) following variational inference [36] as follows: \[\begin{align} {I}(S;Y) &= {H}(Y) - {\mathbb{E}}_{x,y \sim p_{\mathcal{D}}} [-\log q(y|s)] + {\rm KL}\big(p(\cdot|s)||q(\cdot|s)\big)\\ &\geq {H}(Y) - {\mathbb{E}}_{x,y \sim p_{\mathcal{D}}} [-\log q(y|s)], \end{align}\] where \({H}(Y)\) is a constant measuring the shannon entropy of \(Y\), and \({\mathbb{E}}_{X,Y} [-\log q(y|s)]\) is essentially the cross-entropy loss using \(q(y|s)\) for classification. Then, the objective of maximizing \({I}(S;Y)\) can be achieved by minimizing \({\mathbb{E}}_{X,Y} [-\log q(y|s)]\) instead.
(ii) Maximizing I(S;T| Y): The definition of the conditional mutual information \({I}(S;T|~Y)\) is as follows: \[\begin{align} {I}(S;T|~Y)= \mathbb{E}_{y \sim p_{\mathcal{D}}(y)} [{I}(S;T)|~Y=y]= \mathbb{E}_{y \sim p_{\mathcal{D}}(y)}[~\mathbb{E}_{x \sim p_{\mathcal{D}}(x|y)} [\log \frac{p(s,t|y)}{p(s|y)p(t|y)} ]]. \label{eq:inside} \end{align}\tag{5}\] To achieve a tractable objective for maximizing Eq. 5 , we employ noise contrastive estimation [37], [38] and derive a lower bound \(-\mathcal{L}_{\text{info}}\) on the conditional mutual information. This can be summarized in the following Lemma (the proof of which can be found in Appendix 6.2):
Lemma 1. Given \(\{x_i,y\}_{i=1}^{N}\) that is sampled i.i.d. from \(p_{\mathcal{D}}(x|y)\), \(s_i=F_s(x_i)\), and \(t_i=F_t(x_i)\), \({I}(S;T|~Y)\) is lower bounded by \(-\mathcal{L}_{\text{info}}=\mathbb{E}_{y \sim p_{\mathcal{D}}(y)}\big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} \log \frac{e^{f_y(s_i,t_i)}}{\sum_{j=1}^{N} e^{f_y(s_i,t_j)}} +\log N ]\big]\), and \(f_y\) is a score function indexed by \(y\).
By leveraging Lemma 1, \(\mathcal{L}_{\text{info}}\) can be computed using a batch of samples and then minimized for maximizing \({I}(S;T|~Y)\). The score function \(f_y\) is defined as the inner product after non-linear projections into a space of hyper-sphere following [39] as \(f_y(a, b) = \frac{1}{\tau} \frac{\langle g_{y}^{1}(a), g_{y}^{2}(b)\rangle}{\|g_{y}^{1}(a)\|_2 \|g_{y}^{2}(b)\|_2}\), where \(g_y^1\) and \(g_y^2\) are parameterized by using MLPs.
Above we have introduced the training objective of informative fine-tuning and decomposed it into \(I(S;Y)\) and \(I(S;T|~Y)\) for optimization. However, one may wonder why not maximize \(I(S;T)\) directly instead of \(I(S;T|~Y)\). From an information-theoretical perspective, if we maximize \(I(S;Y)\) and \(I(S;T)\), the intersection of the three circles in Figure 1, i.e., the interaction information of them, is repeatedly optimized and might induce confliction. To further elaborate this point, we look into contrastive loss and give an explanation as follows.
We consider contrastive learning by decomposing it into the encouragement of alignment and uniformity following [41]. For example, maximizing \(I(S;T)=H(S)-H(S|T)\) is to decrease \(H(S|T)\) and increase \(H(S)\). Decreasing \(H(S|T)\) corresponds to encouraging alignment in contrastive learning, which aims to align \(s_i\) and \(t_i\) in the sense of inner product when they are projected into a hyper-sphere. In the meanwhile, increasing \(H(S)\) corresponds to uniformity in contrastive learning, which encourages \(s_i\) and \(s_j\), where \(i\neq j\), to diffuse over the whole sub-space as uniformly as possible. It can be seen in Figure 3 (c) that, in the space of \(\mathcal{S}^1\), alignment is achieved in that points from two different circles but with the same color are aligned, and uniformity is achieved in that all points diffuse over the whole \(\mathcal{S}^1\).
However, when maximizing \(I(S;T)\), the uniformity in the contrastive loss is encouraged over the whole data manifold. Diffusing uniformly over the whole data manifold can be against the objective of maximizing \(I(S;Y)\), which aims to separate the whole data manifold into class-specific parts as shown in Figure 3 (b). In contrast, maximizing \(I(S;T|~Y)\) only encourages uniformity inside each class-specific data manifold, which is complementary to maximizing \(I(S;Y)\), as show in Figure 3 (d). In the meanwhile, the alignment is still enforced. More empirical support can be found in Sec. 3.4.
In this section, we are going to put the objective function of informative fine-tuning into the adversarial context and introduce Robust Informative Fine-Tuning (RIFT).
To robustly train a model, the adversarial examples \(\hat{x}\) for training should be formulated and generated first. As our end goal is to enhance robustness in down-stream tasks, the generation process of adversarial examples should focus on preventing a model from predicting the ground truth label. However, using label for generating adversarial examples in training often induces label leaking [42], i.e., the generated adversarial examples contains label information which is used as a trivial short-cut by a classifier. As such, we follow [13], [31] to generate \(\hat{x}\) in a self-supervised fashion: \[\begin{align} \hat{x}=\underset{x'\in \mathbb{B}(x)}{\arg\max}~{\rm KL}\big(q(\cdot|F_s(x))||q(\cdot|F_s(x'))\big).\label{eq:adv32x} \end{align}\tag{6}\] By solving Eq. 6 , \(\hat{x}\) is found to induce the most different prediction from that of a vanilla sample \(x\) in terms of KL, inside the attack space \(\mathbb{B}(x)\).
Next, we introduce how to robustly optimize each objective in the informative fine-tuning by using \(\hat{x}\).
(i) Robustly Maximizing I(S;Y): As shown in Sec. 2.3, maximizing \({I}(S;Y)\) can be achieved by minimizing a cross-entropy loss instead. To encourage adversarial robustness, this cross-entropy loss can be upgraded to encourage both correct predictions on and invariant predictions between \(x\) and \(\hat{x}\) [13], [31]. We formulate such an objective function as follows: \[\begin{align} \min\limits_{\theta,\phi} \mathcal{L}_{\text{r-task}}, ~~~\mathcal{L}_{\text{r-task}} = \mathop{\mathbb{E}}\limits_{x,y \sim p_{\mathcal{D}}} \big[-\log q(y|F_s(x)) + \beta {\rm KL}\big(q(\cdot|F_s(x))||q(\cdot|F_s(\hat{x}))\big)\big],\label{eq:obj1} \end{align}\tag{7}\] where \(\theta\) denotes the parameters of \(F_s(\cdot)\) and \(\phi\) denotes the parameters of \(q(\cdot|\cdot)\). Minimizing \(\mathcal{L}_{\text{r-task}}\) in Eq. 7 corresponds to adversarilly maximizing \(I(S;Y)\) for robust performance in a down-stream task. By doing so, the adversarial example \(\hat{x}\) is generated by Eq. 6 first and then both \(x\) and \(\hat{x}\) are used to optimize the model parameters \(\theta\) and \(\phi\).
(ii) Robustly Maximizing I(S;T| Y): We aim to maximize the conditional mutual information \({I}(S;T|~Y)\), but under an adversarial distribution of input data. We formulate such a term as \({I}(\hat{S};T|~Y)\), where random variable \(\hat{S}=F_s(\hat{X})\), \(\hat{X}\sim p_{\text{adv}}(\hat{x}|x, \theta,\phi,\mathbb{B})\), and sampling from \(p_{\text{adv}}\), the adversarial distribution of input data, is to generate adversarial example \(\hat{x}\) by Eq. 6 .
To optimize \({I}(\hat{S};T|~Y)\), we propose \(-\mathcal{L}_{\text{r-info}}\) as a lower bound on it, and formulate the objective to minimize \(\mathcal{L}_{\text{r-info}}\) as follows (similar to \(-\mathcal{L}_{\text{info}}\) by using Lemma 1): \[\begin{align} \min\limits_{\theta,\varphi} \mathcal{L}_{\text{r-info}},~~~\mathcal{L}_{\text{r-info}} = \mathop{\mathbb{E}}\limits_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}\limits_{\{x_i,y\}_{i=1}^{N} \sim p_{\mathcal{D}}(x|y)} [ \frac{1}{N} \sum_{i=1}^{N} -\log \frac{e^{f_y(\hat{s}_i,t_i)}}{\sum_{j=1}^{N} e^{f_y(\hat{s}_i,t_j)}} -\log N] \big], \label{eq:rinfo} \end{align}\tag{8}\] where \(\hat{s}_i=F_s(\hat{x}_i)\), \(t_i=F_t(x_i)\), and \(\varphi\) denotes the parameters of all the score functions \(f_y\). By Eq. 8 , we are able to encourage \(F_s(\cdot)\) to retain information from \(F_t(\cdot)\) in a robust fasion.
Noted that, in \(\mathcal{L}_{\text{r-info}}\) we do not use \(\hat{x}_i\) to extract features from the pre-trained model. This follows the spirit of Knowledge Distillation [43] (though the setting is different in that a student is expected to perform identically to a teacher in knowledge distillation, while in fine-tuning it does not): the data used to extract features of a teacher should be inside the domain for which the teacher is trained. A down-stream NLP task, e.g., sentiment analysis, has a data domain that is generally a sub-domain of the pre-training corpora, while \(\hat{x}_i\) can be significantly off the pre-training data manifold.
Objective Function of Robust Informative Fine-Tuning (RIFT): The objective function of RIFT is a combination of \(\mathcal{L}_{\text{r-task}}\) and \(\mathcal{L}_{\text{r-info}}\), defined as follows: \[\begin{align} \min\limits_{\theta,\phi,\varphi} \mathcal{L}_{\text{r-task}} + \alpha\mathcal{L}_{\text{r-info}}, \label{eq:final} \end{align}\tag{9}\] where the hyper-parameter \(\alpha\) controls to what extent we encourage an objective model to absorb information from the pre-trained one (ablation on \(\alpha\) can be seen in Sec. 3.5). We summarize the whole training process in Algorithm 1 and include more implementation details in Appendix 6.1.
Tasks and Datasets: We evaluate the robust accuracy and compare our method with the state-of-the-arts on: (i) Sentiment analysis using the IMDB dataset [44]. (ii) Natural language inference using the SNLI dataset [45]. We mainly focus on robustness against adversarial word substitutions [5], [6], as such attacks preserve the syntactic and semantics very well [8], [46], [47], and are very hard to detect even by humans. Under this attack setting, any word in the input sequence can be substituted by a semantically similar word of it (often its synonym). We evaluate robustness on 1000 random examples from the testset of IMDB and SNLI respectively following [8], [46].
Model Architectures: We examine our methods and compare with state-of-the-arts on the following two prevailing pre-trained language models: (i) BERT-base-uncased [9]. (ii) RoBERTa-base [33].
Attack Algorithms: Two powerful attacks are employed: (i) Genetic [5] based on population algorithm. Aligned with [8], [46], the population size and iterations are set as \(60\) and \(40\) respectively. (ii) PWWS [6] based on word saliency. We only attack hypothesis on SNLI aligned with [8], [46].
Substitution Set: We follow [8], [46] to use the substitution set from [5], and the same language model constraint is applied to Genetic attacks and not to PWWS attacks.
.5
| Method | Model | Genetic | PWWS |
|---|---|---|---|
| Standard | BERT | 38.1\(\pm 2.5\) | 40.7\(\pm 1.1\) |
| Adv-Base | BERT | 74.8\(\pm 0.4\) | 68.3\(\pm 0.3\) |
| Adv-PTWD | BERT | 73.9\(\pm 0.4\) | 69.1\(\pm 0.7\) |
| Adv-Mixout | BERT | 75.4\(\pm 0.7\) | 68.8\(\pm 0.6\) |
| RIFT | BERT | 77.2\(\pm 0.8\) | 70.1\(\pm 0.5\) |
.5
| Method | Model | Genetic | PWWS |
|---|---|---|---|
| Standard | RoBERTa | 42.1\(\pm 2.1\) | 45.6\(\pm 3.1\) |
| Adv-Base | RoBERTa | 70.3\(\pm 1.2\) | 63.3\(\pm 0.7\) |
| Adv-PTWD | RoBERTa | 69.3\(\pm 1.4\) | 64.4\(\pm 0.3\) |
| Adv-Mixout | RoBERTa | 70.6\(\pm 1.0\) | 63.9\(\pm 1.3\) |
| RIFT | RoBERTa | 73.5\(\pm 0.8\) | 66.3\(\pm 0.7\) |
.5
| Method | Model | Genetic | PWWS |
|---|---|---|---|
| Standard | BERT | 40.1\(\pm 0.7\) | 19.4\(\pm 0.4\) |
| Adv-Base | BERT | 75.7\(\pm 0.5\) | 72.9\(\pm 0.2\) |
| Adv-PTWD | BERT | 75.2\(\pm 1.0\) | 72.6\(\pm 0.5\) |
| Adv-Mixout | BERT | 76.3\(\pm 0.8\) | 73.2\(\pm 1.0\) |
| RIFT | BERT | 77.5\(\pm 0.9\) | 74.3\(\pm 1.1\) |
.5
| Method | Model | Genetic | PWWS |
|---|---|---|---|
| Standard | RoBERTa | 43.4\(\pm 1.2\) | 20.4\(\pm 1.0\) |
| Adv-Base | RoBERTa | 82.6\(\pm 0.6\) | 79.9\(\pm 0.7\) |
| Adv-PTWD | RoBERTa | 81.2\(\pm 0.8\) | 78.9\(\pm 0.7\) |
| Adv-Mixout | RoBERTa | 82.6\(\pm 0.9\) | 80.6\(\pm 0.3\) |
| RIFT | RoBERTa | 83.5\(\pm 0.8\) | 81.1\(\pm 0.4\) |
(i) Standard Fine-Tuning: The standard fine-tuning process first initializes the objective model by the pre-trained weight, and then use cross-entropy loss to fine-tune the whole model.
(ii) Adversarial Fine-Tuning Baseline (Adv-Base): We employ the state-of-the-art defense against word substitutions, ASCC-Defense [8], as the adversarial fine-tuning baseline. This method is not initially proposed for pre-trained language models but can readily extend to perform adversarial fine-tuning. During fine-tuning, adversarial example \(\hat{x}\), which is a sequence of convex combinations, is generated first and then both \(x\) and \(\hat{x}\) are used for optimization using objective defined in Eq. 7 .
(iii) Adv + Pre-Trained Weight Decay (Adv-PTWD): Pre-trained weight decay [26], [27] penalizes \(\lambda\|W_{\text{obj}}-W_{\text{pre}}\|_2\), and mitigates catastrophic forgetting [28], [48]. We combine it with the adversarial baseline for comparisons and \(\lambda\) is chosen as \(0.01\) on IMDB and \(0.005\) on SNLI for best robustness.
(iv) Adv + Mixout (Adv-Mixout): Motivated by Dropout [49] and DropConnect [50], Mixout [28] is proposed to addresses catastrophic forgetting in fine-tuning. At each iteration each parameter is replaced by its pre-trained counter-part with probability \(m\). We combine it with the adversarial fine-tuning baseline for comparisons with our method and \(m\) is chosen as \(0.6\) for best robustness.
(v) Robust Informative Fine-Tuning (RIFT): The proposed adversarial fine-tuning method. It can be deemed as the adversarial fine-tuning baseline plus the \(\mathcal{L}_{\text{r-info}}\) term. We set \(\tau\) as \(0.2\) for all score functions \(f_y\). For best robust accuracy, \(\alpha\) is chosen as \(0.1\) and \(0.7\) on IMDB and SNLI respectively. Ablation study on \(\alpha\) can be seen in Sec. 3.5.
For fair comparisons, all compared adversarial fine-tuning methods use the same \(\beta\) on a same dataset, i.e., \(\beta=10\) on IMDB and \(\beta=5\) on SNLI, both of which are chosen for the best robust accuracy. Early stopping [32] is used for all compared methods according to best robust accuracy More implementation details and runtime analysis can be found in Appendix 6.1 and 6.3.
Figure 5: Tradeoff curve between robustness and vanilla accuracy of BERT-based model on IMDB.. a — Accuracy (%) under Genetic attacks., b — Accuracy (%) under PWWS attacks.
.5
| Maximizing | Model | Genetic | PWWS |
|---|---|---|---|
| \(\mathbf{I(S;T|~Y)}\) | BERT | 77.2 | 70.1 |
| \(I(S;T)\) | BERT | 76.1 | 69.4 |
| \(\mathbf{I(S;T|~Y)}\) | RoBERTa | 73.5 | 66.3 |
| \(I(S;T)\) | RoBERTa | 72.0 | 65.3 |
.5
| Maximizing | Model | Genetic | PWWS |
|---|---|---|---|
| \(\mathbf{I(S;T|~Y)}\) | BERT | 77.5 | 74.3 |
| \(I(S;T)\) | BERT | 76.6 | 72.1 |
| \(\mathbf{I(S;T|~Y)}\) | RoBERTa | 83.5 | 81.1 |
| \(I(S;T)\) | RoBERTa | 82.5 | 79.4 |
In this section we compare our method with state-of-the-arts by robustness under attacks. As shown in Tables ¿tbl:tab:IMDB32result? and ¿tbl:tab:SNLI32result?., RIFT consistently achieves the best robust performance among state-of-the-arts in all datasets across different pre-trained language models under all attacks. For instance, on IMDB our method outperforms the RoBERTa-based runner-up method by \(2.9\%\) under Genetic attacks and \(1.9\%\) under PWWS attacks. On SNLI based on BERT, we surpass the runner-up method by \(1.2\%\) under Genetic attacks and \(1.1\%\) under PWWS attacks. In addition, RIFT consistently improves robustness upon the adversarial fine-tuning baseline, while the improvements by other methods are not stable. We observe that on SNLI, we surpass the runner-up by a relatively small margin compared to IMDB. This may relate to the dataset property: input from SNLI has a smaller attack space (on average \(6.5^{4}\) combinations of substitutions on SNLI compared to \(6^{108}\) on IMDB), and thus smaller absolute space left for improvement. For results of vanilla accuracy please refer to Appendix 6.4.
In this section we empirically validate that maximizing \(I(S;T|~Y)\) cooperates with a down-stream task better than maximizing \(I(S;T)\) does. We plot two sets of results of our method with maximizing \(I(S;T|~Y)\) and \(I(S;T)\) respectively (using similar noise contrastive loss with all other hyper-parameters the same). As shown in Table ¿tbl:tab:vs?, RIFT with maximizing \(I(S;T|~Y)\) consistently outperforms that with maximizing \(I(S;T)\) in terms of robustness under all attacks on both IMDB and SNLI, which serves as an empirical validation of Sec. 2.4.
.5
| Parameter \(\alpha\) | Vanilla | Genetic | PWWS |
|---|---|---|---|
| 0.00 | 78.1 | 74.8 | 68.3 |
| 0.05 | 78.4 | 76.5 | 69.2 |
| 0.10 | 78.3 | 77.2 | 70.1 |
| 0.30 | 78.3 | 76.2 | 69.5 |
.5
| Parameter \(\alpha\) | Vanilla | Genetic | PWWS | |
|---|---|---|---|---|
| 0.00 | 79.4 | 75.7 | 72.9 | |
| 0.50 | 80.0 | 76.9 | 73.8 | |
| 0.70 | 80.4 | 77.5 | 74.3 | |
| 1.00 | 80.6 | 77.3 | 73.2 |
In this section we conduct ablation study on \(\alpha\), which is the weight of \(\mathcal{L}_{\text{r-info}}\) in Eq. 9 . It aims to control to what extent the objective model absorbs information from the pre-trained one. As shown in Table ¿tbl:tab:on32alpha?, a good value of \(\alpha\) improves both vanilla accuracy and robust accuracy; e.g., on IMDB increasing \(\alpha\) from \(0\) to \(0.1\) results in increased vanilla accuracy by \(0.2\%\) and increased robust accuracy under Genetic attacks by \(2.4\%\). This demonstrates that RIFT does motivate the objective model to retain robust and generalizable features that are beneficial to both robustness and vanilla accuracy. When \(\alpha\) goes too large, the objective of fine-tuning would focus too much on preserving as much information from the pre-trained model as possible, and thus ignores the down-stream task.
One may wonder whether informative fine-tuning itself can improve the clean accuracy upon normal fine-tuning. The answer is yes; e.g., on IMDB using RoBERTa, informative fine-tuning improves about \(0.3\%\) clean accuracy upon normal fine-tuning baseline. The improvement is not very significant as normal fine-tuning targets at vanilla input only and thus suffers less from the forgetting problem. One common problem in contrastive loss is that the complexity grows quadratically with \(N\), but larger \(N\) contributes to tighter bound on the mutual information [38], [51]. One potential improvement is to maintain a dictionary updated on-the-fly like [52] and we will leave it for future exploration.
We finally show the trade-off curve between robustness and vanilla accuracy in Figure 5. As shown, RIFT outperforms the state-of-the-arts in terms of both robust accuracy and vanilla accuracy. It again validates that RIFT indeed helps the objective model capture robust and generalizable information to improve both robustness and vanilla accuracy, rather than trivially trading off one against another.
Pre-Trained Language Models: Language modeling aims at estimating the probabilistic density of the textual data distribution. Classicial language modeling methods vary from CBOW, Skip-Gram [53], to GloVe [54] and ELMo [55]. Recently, Transformer [30] has demonstrated its power in language modeling, e.g., GPT [56] and BERT [9]. The representations learned by deep pre-trained language models are shown to be universal and highly generalizable [57] [33], and fine-tuning pre-trained language models becomes a paradigm in many NLP tasks [9], [20].
Adversarial Robustness: DNNs have achieved success in many fields, but they are susceptible to adversarial examples [1]. In NLP, attacks algorithms include char-level modifications [58]–[63], sequence-level manipulations [64]–[67], and adversarial word substitutions [5]–[7], [12], [47], [68]. For defenses, adversarial training [1], [2], [14] generates adversarial examples on-the-fly and is currently the most effective. [13] first introduce adversarial training to NLP using \(L_2\)-ball to model perturbations, and later more geometry-aligned methods like first-order approximation[59], axis-aligned bound [46], [69], and convex hull [8], become favorable.
Fine-Tuning and Catastrophic Forgetting: The fine-tuning of pre-trained language models [9], [20] can be very unstable [9], as during fine-tuning the objective model can deviate too much from the pre-trained one, and easily over-fits to small fine-tuning sets [20]. Such phenomenon is referred as catastrophic forgetting [17], [18] in fine-tuning [20], [21], [28]. Methods to address catastrophic forgetting include pre-trained weight decay [21], [26], [27], learning rate decreasing [20], and Mixout regularization [28]. These methods focus on parameter space to constrain the distance between two models while our method addresses forgetting following an information-theoretical perspective. In continuous learning, method like rehearsal-based [70]–[73] and regularization-based [19], [74], [75] also aims at the forgetting problem, but under a different setting: they focus on balanced performance on tasks with previous data, while in our setting the pre-training corpora are not available and language modeling is not our concern.
Contrastive Learning and Mutual Information: Contrastive learning [38], [76], [77] has demonstrated its power in learning self-supervised representations [39], [52]. The contrastive loss such as InfoNCE[38], InfoMax [76], is proposed as maximizing the mutual information of features in different views, and further discussed in [41], [51], [78]–[80]. This paper shares the same perspective of information theory, but looks into a different problem, addressing forgetting for fine-tuning. Addressing forgetting motivates our objective of maximizing the mutual information between the outputs of two models, which is different from conventional contrastive learning, and cooperation with fine-tuning motivates the use of conditional mutual information, which is quite different also. [81] propose to restrict information for robustness in vision domain. Restricting information to some extent helps a model ignore spurious features, but may be at the cost of ignoring robust features as well. In contrast, the proposed method aims to retain the generic and robust features that are already captured by a pre-trained language model, and thus better fits a fine-tuning scenario.
In this paper, we proposed RIFT to fine-tune a pre-trained language model towards robust down-stream performance. RIFT addresses the forgetting problem from an information-theoretical perspective and consistently outperforms state-of-the-arts in our experiments. We hope that this work can contribute to the NLP robustness in general and thus to more reliable NLP systems in real-life applications.
This work is supported by the Singapore Ministry of Education (MOE) Academic Research Fund (AcRF) Tier 1 (S21/20) and Tier 2.
Textual sequence processing. For consistent word numbers per input under word substitution attacks, we seperate word-level tokens by space and punctuations, and then follow the original tokenizer of BERT/RoBERTa to tokenize the input sequence. The byte-level RoBERTa tokenizer is further modified to output one token per word to fit the setting of word substitution attacks. The maximum number of tokens including special tokens per input is set as \(300\) for IMDB, and \(80\) for SNLI.
Hyper-parameters and optimization details. We set \(\alpha\) as \(0.1\) for IMDB and \(0.7\) for SNLI. \(\tau\) is set as \(0.2\) for both IMDB and SNLI. Other hyper-parameters are set as the same among all compared methods for fair comparisons. For both standard fine-tuning and adversarial fine-tuning, we run for 20 epochs with batch size \(32\) for IMDB, and run for 20 epochs with batch size \(120\) for SNLI. Early stopping is used for all compared methods according to best robust accuracy. AdamW optimizer is employed with learning rate of \(0.00002\). We do not apply weight decay on an objective model, and set weight decay rate as \(0.0002\) for task-specific layers.
Model architectures. For both BERT and RoBERTa, the representation with respect to the sequence classification token of the last layer is employed as the output feature, which is later taken as the input of the task-specific layers for predictions. The task-specific layer is a MLP that has two linear layers with relu activation after the first layer and softmax after the second one.
.5
| Method | Model | Vanilla Accuracy |
|---|---|---|
| Standard | BERT | 93.1\(\) |
| Adv-Base | BERT | 74.6\(\) |
| Adv-PTWD | BERT | 76.6\(\) |
| Adv-Mixout | BERT | 77.8\(\) |
| RIFT | BERT | 78.3\(\) |
.5
| Method | Model | Vanilla Accuracy |
|---|---|---|
| Standard | RoBERTa | 94.9 |
| Adv-Base | RoBERTa | 80.1\(\) |
| Adv-PTWD | RoBERTa | 80.7\(\) |
| Adv-Mixout | RoBERTa | 79.0\(\) |
| RIFT | RoBERTa | 84.2\(\) |
.5
| Method | Model | Vanilla Accuracy |
|---|---|---|
| Standard | BERT | 89.2 |
| Adv-Base | BERT | 79.4 |
| Adv-PTWD | BERT | 78.4 |
| Adv-Mixout | BERT | 79.3 |
| RIFT | BERT | 80.5 |
.5
| Method | Model | Vanilla Accuracy |
|---|---|---|
| Standard | RoBERTa | 91.3 |
| Adv-Base | RoBERTa | 87.1 |
| Adv-PTWD | RoBERTa | 85.9 |
| Adv-Mixout | RoBERTa | 87.1 |
| RIFT | RoBERTa | 87.9 |
The loss \(\mathcal{L}_{\text{info}}\) is the categorical cross-entropy loss of identifying \(t_i\) among \(\{t_j\}_{j=1}^N\), given \(s_i\) and \(y\). Thus, the optimial \(e^{f_y(s,t)}\) that minimizes \(\mathcal{L}_{\text{info}}\) is proportional to \(\frac{p(t|s,y)}{p(t|y)}\) (refer to [38] for more details). We then insert \(\frac{p(t|s,y)}{p(t|y)}\) into \(\mathcal{L}_{\text{info}}\) and get what follows: \[\begin{align} \mathcal{L}_{\text{info}}&=\mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} -\log N\frac{\frac{p(t_i|s_i,y)}{p(t_i|y)}}{\sum_{j=1}^{N} \frac{p(t_j|s_i,y)}{p(t_j|y)} } ]\big]\\ &=\mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} \log \frac{1}{N}\frac{\frac{p(t_i|s_i,y)}{p(t_i|y)}+\sum_{j\neq i}^{N} \frac{p(t_j|s_i,y)}{p(t_j|y)}}{ \frac{p(t_i|s_i,y)}{p(t_i|y)} } ]\big]\\ &=\mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} \log \frac{1}{N} (1+{\frac{p(t_i|y)}{p(t_i|s_i,y)} }{\sum_{j\neq i}^{N} \frac{p(t_j|s_i,y)}{p(t_j|y)}} ) ]\big]\\ &=\mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} \log (\frac{1}{N}+\frac{N-1}{N}{\mathop{\mathbb{E}}_{x \sim p_{\mathcal{D}}(x|y)} [\frac{p(t_j|s_i,y)}{p(t_j|y)}] {\frac{p(t_i|y)}{p(t_i|s_i,y)} } } ) ]\big]\\ &=\mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} \log (\frac{1}{N}+\frac{N-1}{N}{\frac{p(t_i|y)}{p(t_i|s_i,y)} } ) ]\big] \tag{10}\\ &\geq \mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)} \big[ \mathop{\mathbb{E}}_{\{x_i,y\}_{i=1}^{N}} [ \frac{1}{N} \sum_{i=1}^{N} \log {\frac{p(t_i|y)}{p(t_i|s_i,y)} } ]\big] \tag{11}\\ &= \mathop{\mathbb{E}}_{y \sim p_{\mathcal{D}}(y)}\big[ {\mathop{\mathbb{E}}_{x \sim p_{\mathcal{D}}(x|y)}}[ -\log {\frac{p(t|s,y)}{p(t|y)} } ]\big]\\ &= - I(S;T|~Y). \end{align}\] Eq. 10 to Eq. 11 is by Jensen’s inequality. As such, \(-\mathcal{L}_{\text{info}}\) is a lower bound on \(I(S;T|~Y)\) and a larger \(N\) makes the bound tighter. The specific design of the score function \(f_y\) does not impact the correctness of Lemma 1: when \(-\mathcal{L}_{\text{info}}\) is maximized, \(-\mathcal{L}_{\text{info}}\) is still a lower bound on the mutual information term. However, if the capacity of \(f_y\) is limited, the bound might be loose.
All models are trained using the Nvidia A100 GPU and our implementation is based on PyTorch. As for IMDB, it takes about \(10\) GPU hours to train a BERT or RoBERTa based model using RIFT. As for SNLI, it takes about \(40\) GPU hours to train a BERT or RoBERTa based model using RIFT.
we here show the vanilla accuracy of each methods in Tabs. ¿tbl:tab:IMDB32vanilla32result? and ¿tbl:tab:SNLI32vanilla32result? as a supplement. As we can see, RIFT surpasses all other adversarial fine-tuning method in terms of vanilla accuracy. It again validates that RIFT does help retain the generalizable information learned before.
The assets and the corresponding licenses are as follows. IMDB dataset [44]: Non-Commercial Licensing. SNLI dataset [45]: Creative Commons Attribution-ShareAlike 4.0 International License. Genetic attack [5]: MIT License. PWWS attack [6]: MIT License. Certified robustness [46]: MIT License. ASCC-defense [8]: MIT License.
Our code will be available at https://github.com/dongxinshuai/RIFT-NeurIPS2021.↩︎