{'fraction_non_alphanumeric': 0.03037458409359236, 'fraction_numerical': 0.009802869307001538, 'mean_word_length': 4.930829620199448, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Improving the Adversarial Robustness of NLP Models by Information Bottleneck

Existing studies have demonstrated that adversarial examples can be directly attributed to the presence of non-robust features, which are highly predictive, but can be easily manipulated by adversaries to fool NLP models. In this study, we explore the feasibility of capturing task-specific robust features, while eliminating the non-robust ones by using the information bottleneck theory. Through extensive experiments, we show that the models trained with our information bottleneck-based method are able to achieve a significant improvement in robust accuracy, exceeding performances of all the previously reported defense methods while suffering almost no performance drop in clean accuracy on SST-2, AGNEWS and IMDB datasets.


Introduction
Recently, a number of studies (Han et al., 2020;Shafahi et al., 2019) have revealed the fact that the performance of deep neural networks (DNNs) can be severely undermined by adversarial examples. In the text domain, these adversarial examples are crafted by semantic-preserving perturbations to inputs with word synonym substitution (Ebrahimi et al., 2018;Ren et al., 2019;Alzantot et al., 2018) and character-level transformations (Gao et al., 2018;Zang et al., 2020). The vulnerability of DNN models results in inferior performances under adversarial attacks in many NLP tasks including text classification, natural language inference (NLI), question answering (QA), etc. To resolve this problem, researchers have proposed various methods to defend against adversarial attacks (Goodfellow et al., 2014;Szegedy et al., 2013;Jia and Liang, 2017;Kang et al., 2018;Zhou et al., 2021).
In particular,  and Ilyas et al. (2019) showed that the vulnerability of computer * Equal contribution vision models can be attributed to "non-robust features," which are features in the representation space that are sensitive to adversarial attacks and can be easily manipulated by attackers. The presence of these features will weaken the robustness of deep learning models. Therefore, a potential defense strategy is to filter out such non-robust features in the inputs.
In this paper, we posit that the robustness of text classification models can be improved by filtering out the non-robust features. However, unlike the continuous input in the computer vision domain, input in the NLP domain is a sequence of words, which makes it difficult to model its distribution. We thus research into means to filter out the nonrobust features in language model inputs.
Inspired by (Li and Eisner, 2019;Wang et al., 2021a), we propose to use the information bottleneck method (Tishby et al., 2000) in text classification tasks. Specifically, we plug in an information bottleneck layer (IB layer) 1 between BERT (Devlin et al., 2018) output layer and the text classifier to preserve only task-specific features.
Since the IB layer trades off between minimizing preserved information and model prediction performance, features that are not robust under the targeted task are filtered out. Therefore, our approach is able to focus more on the robust features and achieve an improvement in its robustness.
We conduct extensive experiments on three text classification benchmarks: SST-2 (Socher et al., 2013), AGNEWS (Gulli, 2004) and IMDB (Maas et al., 2011). Results have shown that our approach achieves a great improvement on model robustness compared with traditional defense methods, while only suffering little or even no performance drop on clean accuracy. We also provide a visualization to interpret how the information bottleneck layer works to keep robust features, in order to justify our proposed approach.
In summary, we propose a new information bottleneck-based approach to improve the robustness of DNN language models. We demonstrate that our approach is effective in improving models' adversarial performance while maintaining their performance on clean data. In addition, experimental results show that our approach can also be combined with existing adversarial training methods like FreeLB  to further improve models' robustness under adversarial attacks.

Related Work
In response to the discovery of DNN's vulnerability to adversarial examples and the emergence of adversarial attacks, many defense methods have also been proposed to improve the robustness of DNN models.
Among these methods, adversarial training is one of the most effective and widely used to defend against adversarial examples. In adversarial training, the model is trained to correctly classify both adversarial examples and normal examples. Goodfellow et al. (2014) first propose a fast gradient sign method (FGSM) to generate adversarial examples for adversarial training in the image domain. In textual domain, many researchers tried to add perturbation to input word embedding to generate adversarial examples. Zhang and Yang (2018) applied several types of noises, such as Gaussian and Bernoulli, to perturb the input embeddings while  proposed FreeLB, which minimizes the resultant adversarial loss inside different regions around input samples through adding adversarial perturbations to word embeddings. However, they all focus on the generalization of model, not the robustness. Wang et al. (2019) and  proposed to replace certain words in the training dataset with their synonyms for the purpose of data augmentation. However, this kind of methods are specific for the defense against synonym substitution attack, and may be weak when facing other kind of adversarial attack methods, such as character-level attack.
Apart from empirical methods, a set of certified robustness training methods is introduced recently, which have proven to be effective in improving a model's robustness against a specific type of attacks.  and Jia et al. (2019) used interval bound propagation (IBP) to propose certi-fied robustness training methods that can limit the loss of the worst-case perturbations. These methods are provably robust to word substitution attacks. However, certified robustness training sacrifices the model's clean accuracy and is not generalized to all types of attacks.
The information bottleneck method was proposed by (Tishby et al., 2000), aiming to provide a quantitative notion of "relevant information". Although the method has been utilized in many NLP tasks such as rationale extraction (Paranjape et al., 2020), sentence summarization (West et al., 2019) and parsing , only few of them focus on combining information bottleneck with adversarial defense methods. Wang et al. (2021b) proposes infoBERT, which applied information bottleneck to the embedding layer of pre-trained language models to suppress noisy information contained in word embeddings. The implicit assumption of this approach is that the embedding layer contains enough information for the model to make predictions. However, different word combinations can have different meanings, so the semantics of a sentence can not be fully expressed without taking contextual information into consideration.
Therefore, we propose a completely different implementation of the information bottleneck. The information bottleneck is utilized to extract taskrelated features from the output of the last layer of BERT, which is pre-trained and thus be capable of generating a contextualized representation for the input sequence, and calculated by using the variational inference method.
Besides, InfoBERT uses the gradient information of each word to find local anchored features and aims at increasing the mutual information between the global representation and them, while in our approach, robust features are extracted from the global representation without additional steps.

Method
Derived from information theory, the information bottleneck method (Tishby et al., 2000) was proposed and has been used as a training objective as well as a theoretical framework (Tishby and Zaslavsky, 2015) in machine learning. The method of information bottleneck can be statistically formulated as follows: denote the input random variables as X, which could be sentences or paragraphs, and the output as Y. Denote the joint distribution of X and Y as P (X, Y). The purpose of information  Figure 1: We use the IB layer to filter out non robust features, which are denoted by the gray circle dots in the figure, flow from Bert output. I(·; ·) denotes the mutual information and the jagged line denotes the compression process. By Maximizing the mutual information between the final prediction Y and the hidden features T while minimizing the mutual information between input X and T through IB layer, we are able to obtain a T that has all the non-robust features filtered out.
bottleneck is to learn a distribution p θ (t|x) from X to a compressed hidden feature T. To simplify the notation, we will omit θ in the subscript when we mention p(t|x). The information bottleneck objective (IB objective) used for optimization is as follows: where I(·; ·) denotes the mutual information. The intuitive explanation for optimizing the information bottleneck objective Eq.
(1) is that we want to compress all information given by the input X, while still maintaining enough knowledge for the model to give the correct prediction outcome Y. This can be achieved through finding the minimum value of L IB . In the equation, parameter β controls how much information we want to preserve among all the information extracted from the input X. By increasing β, we can narrow the "neck", thus allowing less information from X to be transmitted to the hidden feature T. Inspired by (Ilyas et al., 2019)'s theory about robustness of features, we utilize the information bottleneck method to help the DNN models filter out "non-robust features" and only preserve "robust features" from model input. Since "Robust features" contribute to model's prediction, they contain semantic information of the input sequence. Taking this into account, our goal would be to filter out task-unrelated information while keeping the loss of task-related information to a minimum. This way, our method would be able to help improve model's robustness without diminishing its clean performance for the prediction task. By plugging in the IB layer right after BERT output, we can leverage the ability of pre-trained models on extracting contextualized features, preventing the possible loss of "robust features" after compression of information.
Specifically, given an input X and output Y, we want to obtain specific hidden features T from X that only contain information which contributes to the final prediction Y. By minimizing the IB objective in Eq.(1), the IB layer filters out the taskunrelated information in BERT output, which is extracted from input X, and obtain the required T.
To minimize the IB objective, we maximize the mutual information I(Y; T). Since the purpose of maximizing I(Y; T) is to enforce T contain enough information for the model's prediction, we choose to minimize the loss function of the original task to approximate the maximization of I(Y; T). Taking classification tasks as an example, our method would be to minimize the cross entropy function L CE .
The mutual information I(X; T) can be calculated by the Kullback-Leibler distance between the distributions of P (T|X) and P (T) as follows: To calculate the Kullback-Leibler divergence between P (T|X) and P (T), we need knowledge of their probability distributions. The P (T|X) term can be sampled empirically. However, the P (T) term is difficult to be estimated. To resolve this challenge, we expand the Eq.2 to get the following equation: where the marginal distribution of T, p(t) = p(t|x)p(x)dx. Since the original Tishby et al. (2000) relied on the iterative Blahut Arimoto algorithm to opitimize the IB objective, which is infeasible to apply to deep neural networks, many researchers try to use variational inference to approximate this problem (Alemi et al., 2017;Chechik et al., 2005). Inspired by previous studies, we replace p(t) with a variational approximation q(t) = N (µ X , σ 2 X ), which is a Gaussian distribution with the mean µ X and standard deviation σ 2 X . Since the Kullback-Leibler divergence is defined to be non-negative, which means p(t) log p(t)dt ≥ p(t) log q(t)dt, we derive the following upper bound: We want to reduce the mutual information between X and T so that more task-unrelated information can be filtered out, which can help us retain more robust features for the final prediction. To achieve this goal in practice, we minimize the upper bound of I(X; T) derived in Eq.(4). We achieve this through adjusting the parameters in Q(T) in order to minimize the Kullback-Leibler divergence between P (T|X) and Q(T), which will lower the upper bound of I(X; T). Combined with the optimization goal of the term I(Y; T) we explained in the former chapter, the final loss function is: By using the loss function Eq.(5) to optimize our model, our approach would be able to filter out the non-robust features for the classification task among all the inputs.

Experiments
In order to validate our assumption, we conduct several experiments to evaluate the effectiveness of our approach. We first compare our model and the baseline models both on their clean accuracy and accuracy under attack. Furthermore, in exploration of the ability of our model to work in conjunction with adversarial training methods-such as FreeLBto achieve complementary effects, we also evaluate the performance of the combined model. In addition, we try to interpret and further analyze our approach through several additional experiments.

Dataset
We evaluate our approach on three widely-used classification benchmark datasets: IMDB dataset (Maas et al., 2011), SST-2 (Socher et al., 2013) dataset, and AGNEWS dataset (Gulli, 2004). Both IMDB and SST-2 are sentimental classification datasets with two classes, while AGNEWS is a topic classification dataset with four classes.

Baseline Models
Because our model can be viewed as an enhanced variant of the BERT models, we choose to use BERT base (Devlin et al., 2018) as one of the baseline models. We also establish a comparison with InfoBERT (Wang et al., 2021a), a method that is very similar to our approach, to verify the effectiveness of the proposed way of injecting an IB layer.
Apart from these two baseline models, we also compare our approach with four adversarial training methods: PGD (Madry et al., 2018), which is a classic and representative method, as well as FreeLB , SMART  and TAVAT (Li and Qiu, 2021), which are three state-of-the-art defense methods.

Robustness Evaluation
We evaluate models' accuracy under four different attack algorithms, including both word-level attacks and character-level attacks. Textfooler  Textfooler ranks the importance of words by the drop of true class probability after deleting words from the original text. By leveraging the similarity of word embeddings, it builds a candidate word set and selects the word that minimizes the predictive probability of the true class label. Textbugger (Li et al., 2018) Textbugger contains both word-level and character-level perturbations by inserting, removing, swapping and substituting letters or replacing words. BERT-Attack (Li et al., 2020) BERT-Attack uses the masked language model (MLM) of BERT to replace words with other words that fit the context. In addition to achieving high attack success rate, high perturbation percentage and relatively low calculation costs, BERT-Attack also ensures fluency and semanticality of adversarial samples. Deepwordbug (Gao et al., 2018) Deepwordbug designs a score system to find the rank the importance of tokens to the prediction and perturb the top k important tokens by swap, substitution, deletion and insertion.

Implementation Details
We train 10 epochs of the models on AGNEWS and SST-2 datasets, and 20 epochs on the IMDB dataset and provide experimental results averaged on three different random seeds: 0, 1, and 2.
For each attack, we take 1000 attack examples on  the SST-2 and AGNEWS datasets, and 200 attack examples on the IMDB dataset due to the excessive number of queries. We also set a default restriction of 15% on the maximum modify ratio for each attack algorithm. Further details for implementation would be discussed in the following sections.

Hyperparameter
There are two main hyperparameters in our experiments: hidden dimension (hd) and β. The hidden dimension controls the dimension of the IB layer and β controls the trade-off between better prediction performance and restriction of the information flow. We experiment with different sizes of hd ranging from 100 to 700 and different values of β from 0.05 to 0.3. Based on model performance, we finally choose to use hd = 100 on SST-2, AG-NEWS, and hd = 200 on IMDB. We used β = 0.1 on all three datasets. All hyperparameters are chosen based on the experimental results on the corresponding development dataset.

Results
Table 1 reports the detailed results of our experiment. As shown in Table 1, our model suffers little or even no performance drop in clean accuracy on all of the three datasets. On the IMDB and AG-NEWS datasets, our model achieves around the same clean accuracy as the baseline model, while on the SST-2 dataset, clean accuracy of our model in fact outperforms the baseline model by a small margin. This implies that even with an added information bottleneck layer, the proposed method still ensures sufficient information flowing through the information bottleneck for the model to make accurate predictions. Besides the clean performance, Table 1 also provides concrete experimental results on the robustness of all the models under four different types of attacks. The robustness accuracy result shows our model not just demonstrates significant improvement in adversarial robustness compared to BERT-base model, but is also very competitive with other defense methods under both word-level and character-level attacks. In particular, our model outperforms all baseline models by a great margin under the attack of TextFooler and TextBugger on IMDB dataset. Furthermore, combining our method with FreeLB (a kind of adversarial training method) can further improve models' adversarial robustness. We achieve the highest adversarial accuracy under all circumstances in our experiments and only suffer a small drop in clean accuracy compared to the original FreeLB method. This implies that adversarial training methods and our approach are   This series of figures (from left to right) shows a progression from moderate compression to too much compression. As the β increases, the boundary between the two classes gradually disappears.
complementary to each other. We also evaluate the accuracy of models under adversarial attack methods whose constraint is relaxed. Specifically, the maximum modify ratio is not constrained for all attack methods, which means that relatively stronger adversarial examples can be generated. The result in Table 2 shows that our method can still outperform all the baseline methods in this setting.

Discussion
In this section, we study how the implementation of IB layer affects the model's robustness. Furthermore, we seek to find a reasonable explanation for this effect.

Quantitative Analysis
First, we discuss the effect of the two hyperparameters in our model: hidden dimension (hd) and β. The size of the hidden dimension controls the dimension of the information bottleneck layer, thus limiting the amount of total information that flows through the information bottleneck. We test the impact of 10 different hidden dimension sizes ranging from 50 to 600 on the SST-2 dataset. As shown in Figure 3, a small hidden dimension size helps our model achieve better performance under adversar- ial attacks, while only suffering little drop in clean accuracy. This indicates that choosing small hidden dimension size works best in helping the model filter out task-unrelated information. However, when the size of the hidden dimension gets too small, we observe a significant drop in accuracy under attack. This may indicate that when the hidden dimension size is too small, the information bottleneck layer would compress too much such that task-related information is also filtered out.
We also test the influence of different values of βs from 0 to 6. Recall that the hyperparameter β controls the trade off between better prediction performance and restriction of the information flow through the bottleneck. Therefore, adjusting the value of β could also help with controlling the amount of information that flows through the information bottleneck layer. Specifically, a smaller β will allow more information to flow from X to hidden representation T, while a larger β would "narrow" the bottleneck, allowing less information to flow through. When β is set to 0, the information bottleneck layer is equal to a linear layer. Results of experiments with different values of β on the SST-2 dataset are shown in Figure 3. At first, as the value of β increases, less information are able to flow through the bottleneck, forcing the information bottleneck to filter out non-robust features. This results in an enhancement of model robustness performance. However, the robust accuracy decrease as β increase further. This might indicates that if the information bottleneck is too "narrow", some robust features would also be filtered out by the IB layer. We further visualize the influence of β by using t-SNE. As shown in Figure 2, when β is equal to 1.0, the samples can be clearly divided into two clusters corresponding to their labels. As the value of β increases, more information are filtered out, including those useful for the sentiment classification task. It can be observed from the figure that samples from the two categories become close to each other when the value of β is 5.0. Therefore, a small perturbation may cause the model to make a false prediction. The decision boundary becomes unclear when β is set to 10.0 and the clean accuracy of the model decreases significantly in this case.

Interpretation
As we have discussed in former sections, researchers such as Tsipras et al. and Ilyas et al. have proposed the idea that features contributing to deep learning tasks can be divided into robust features and non-robust features. In our assumption, the information bottleneck layer that we plug in after BERT output works to filter out non-robust features while retaining the robust features from all the information that flows out from BERT. By only preserving task-specific robust features, our model is able to attain an improvement in adversarial robustness, while minimizing the drop in clean  accuracy performance at the same time.

Significance Score
In order to verify our assumption, we specifically conduct an experiment on the SST-2 dataset to see if our model is able to capture robust task-related features. In this experiment, a score s i is calculated for each word x i in the input sentence X to measure the influence of x i when predicting the sentiment of the sentence. We denote the embedding of the input sentence X as E = {e 1 , ..., e n }, and X \x i = {e 1 , ..., e i−1 , 0, e i+1 , ..., e n } denotes setting the embedding of word x i to zero. The normalized significance score s i is defined as follows.
where F Y (X) denotes the probability that our model gives a prediction of the label Y , and Rescale(·) is defined by dividing the significance score by the sum of the absolute values of significance scores of all words in the sentence. Here, s i denotes the change in models' predictions before and after deleting word x i . A positive value indicates that the word helps the model make correct predictions, and a negative value means that the word leads to incorrect predictions of the model. We note that the SST-2 dataset provides sentiment labels for each word which are annotated manually. In order to identify sentiment words in sentences, we make use of these labels provided as the golden truth. Since the words that are consistent with the sentimental tendency of the sentence are important for sentiment classification, we sum the significance scores of these words and denote this sum as Sig%.
As shown in Table 3, the baseline model is not able to correctly classify most sentences under textual adversarial attacks on the SST-2 dataset. Note that here Sig% is a negative value, meaning that the words that the baseline model considers "harmful" to making classification predictions in fact have a counter-effect on the model. This means that the baseline model fails to correctly attention to important features in the input sentences. By definition, Sig% increases when the models' classification accuracy improves, indicating their correlation. Since our model achieves a higher Sig%, it can be inferred that our model is better at extracting the robust features that are not likely to be perturbed under textual adversarial attacks. In order to better illustrate how our model succeeds in extracting robust features, specifically for classification tasks, we visualize two examples from the SST-2 dataset in Figure 4. In the first sentence, our model better attends to the word "joyless", which clearly expresses the emotional tendency in the sentence. In contrast, the attention of the baseline model is distracted by words such as "perhaps" and "the", which are almost irrelevant to the sentimental classification task. In the second sentence, "slipshod" is a sentiment word which is helpful for predicting the sentiment of the sentence. As demonstrated by the figure, our model succeeds in accurately capturing the importance of this sentiment word. The baseline model, however, failed to attend to this word and thus is unable to classify the sentence correctly. The figure shows that our model accurately captures robust features related to important sentimental words in inputs, while the baseline model fails to do the same thing. This further validates the assumption that our model is able to extract robust task-related features for classification.

Plugging Information Bottleneck into Different Layers
Features in the deep layer model have been observed to transit from general to task-specific as the position of the layer gets higher (Yosinski et al., 2014;Howard and Ruder, 2018). More specifically, the first layers of deep neural networks may contain more general and static knowledge of the input sequences, while the higher layers contain more knowledge related to the task. Since the information bottleneck layer is used to extract robust, task-specific features, we assume that applying it to higher layers instead of the embedding layer, which is used in InfoBERT, would be a more reasonable implementation. To verify this, we add the Information Bottleneck layer to the output of different layers of BERT model and calculate an additional loss(Eq.(5)) as regularization. We train the models to minimize the total loss. Experimental results show that as the layer we choose to apply the information bottleneck layer becomes higher, model's classification accuracy and accuracy under attack both improve, which validates our assumption.

Conclusion
We propose a novel implementation of the information bottleneck method on BERT-base models to improve its adversarial robustness. Our method is proven to be successful in filtering out nonrobust feature and keeping task-specific robust features, thus improving the adversarial robustness of models. Experimental results have shown that our method outperforms four widely used defense methods across three datasets with both sentencelevel and character-level attack algorithms. We also validate our method through a comprehensive anal-ysis on experimental results as well as quantitative interpretation of our model's performance under adversarial attacks.