Deep neural networks have been successfully applied to many domains. However, these black-box models are generally difficult to analyze and their behavior is not guaranteed. Moreover, it has been shown that the predictions of deep networks become unreliable and unstable when tested in unseen situations, e.g., in the presence of small adversarial perturbations to the input (szegedy2013intriguing; goodfellow2014explaining; lin2019art). Therefore, neural network verification has become an important tool for analyzing and understanding the behavior of neural networks, with applications in safety-critical applications (katz2017reluplex; julian2019verifying; lin2019art), model explanation (shih2018symbolic) and robustness analysis (tjeng2017evaluating; wang2018formal; gehr2018ai2; wong2018provable; singh2018fast; weng2018towards; zhang2018efficient).
Formally, a neural network verification algorithm aims to provably characterize the prediction of a network within some input space. For example, given a -way classification model , where stands for the predicted score of class
, we can verify some linear specification (defined by a vector) as below:
where is a predefined input space. In the robustness verification problem, is defined as some small -ball around the original example , and setting up
enables us to verify whether the logit output of classis always greater than another class for any input within . This is a nonconvex optimization problem which makes computing the exact solution challenging, and thus several algorithms are recently proposed to find the lower bounds of Eq. (1) in order to efficiently obtain a safety guarantee (gehr2018ai2; weng2018towards; zhang2018efficient; singh2019abstract). Moreover, extensions of these algorithms can be used for verifying properties beyond robustness, such as rotation or shift invariant (singh2019abstract), conservation of energy (qin2019verification) and model correctness (yang2019correctness).
However, most of existing verification methods focus on relatively simple neural network architectures, such as feed-forward and recurrent neural networks, while they cannot handle complex structures. In this paper, we develop the first robustness verification algorithm for Transformers(vaswani2017attention)
with self-attention layers. Transformers have been widely used in natural language processing(devlin2018bert; yang2019xlnet; liu2019roberta) and many other domains (parmar2018image; kang2018self; li2019visualbert; su2019vl; li2019semi). For frames under perturbation in the input sequence, we aim to compute a lower bound such that when these frames are perturbed within -balls centered at the original frames respectively and with a radius of , the model prediction is certified to be unchanged. To compute such bounds efficiently, we adopt the linear-relaxation framework (weng2018towards; zhang2018efficient)
– we recursively propagate and compute linear lower and upper bounds for each neuron w.r.t the input within the perturbation space.
We resolve several particular challenges in verifying Transformers. First
, Transformers with self-attention layers have a complicated architecture. Unlike simpler networks, they cannot be written as multiple layers of affine transformations or element-wise activation functions. Therefore, we need to propagate linear bounds differently for self-attention layers.Second, dot products, softmax, and weighted summation in self-attention layers involve multiplication or division of two variables both under perturbation, namely cross-nonlinearity, which is not present in feed-forward networks. ko2019popqorn proposed a gradient descent based approach to find linear bounds, however it is inefficient and poses a computational challenge for Transformer verification since self-attention is the core of Transformers. In contrast, we derive closed-form linear bounds that can be computed in complexity. Third, in the computation of self-attention, output neurons in each position depend on all input neurons from different positions (namely cross-position dependency), unlike the case in recurrent neural networks where outputs depend on only the hidden features from the previous position and the current input. Previous works (zhang2018efficient; weng2018towards; ko2019popqorn) have to track all such dependency and thus is costly in time and memory. To tackle this, we introduce an efficient bound propagating process in a forward manner specially for self-attention layers, enabling the tighter backward bounding process for other layers to utilize bounds computed by the forward process. In this way, we avoid cross-position dependency in the backward process which is relatively slower but produces tighter bounds. Combined with the forward process, the complexity of the backward process is reduced by for input length , while the computed bounds remain comparably tight. Our contributions are summarized below:
We propose an effective and efficient algorithm for verifying the robustness of Transformers with self-attention layers. To our best knowledge, this is the first method for verifying Transformers.
We resolve key challenges in verifying Transformers, including cross-nonlinearity and cross-position dependency. Our bounds are significantly tighter than those by adapting Interval Bound Propagation (IBP) (mirman2018differentiable; gowal2018effectiveness).
We quantitatively and qualitatively show that the certified bounds computed by our algorithm consistently reflect the importance of input words in sentiment analysis, which justifies that these bounds are meaningful in practice and they shed light on interpreting Transformers.
2 Related Work
Robustness Verification for Neural Networks.
Given an input and a small region , the goal of robustness verification is to verify whether the prediction of the neural network is unchanged within this region. This problem can be mathematically formulated as Eq. (1). If Eq. (1) can be solved optimally, then we can derive the minimum adversarial perturbation of by conducting binary search on . Equivalently, we obtain the maximum such that any perturbation within cannot change the predicted label.
Several works focus on solving Eq. (1
) exactly and optimally, using mixed integer linear programming (MILP)(tjeng2017evaluating; dutta2018output), branch and bound (BaB) (bunel2018unified), and satisfiability modulo theory (SMT) (ehlers2017formal; katz2017reluplex). Unfortunately, due to the nonconvexity of model , solving Eq. (1
) is NP-hard even for a simple ReLU network(katz2017reluplex). Therefore, we can only expect to compute a lower bound of Eq. (1) efficiently by using relaxations. Many algorithms can be seen as using convex relaxations for non-linear activation functions (salman2019convex), including using duality (wong2018provable; dvijotham2018dual), abstract domains (gehr2018ai2; singh2018fast; mirman2018differentiable; singh2019abstract), layer-by-layer reachability analysis (wang2018efficient; weng2018towards; zhang2018efficient; gowal2018effectiveness) and semi-definite relaxations (raghunathan2018semidefinite; dvijothamefficient2019). Additionally, robustness verification can rely on analysis on local Lipschitz constants (hein2017formal; zhang2019recurjac). However, existing methods are mostly limited to verifying networks with relatively simple architectures, such as feed-forward networks and RNNs (wang2018verification; akintunde2019verification; ko2019popqorn), while none of them are able to handle Transformers.
Transformers and Self-Attentive Models.
Transformers (vaswani2017attention) based on self-attention mechanism, further with pre-training on large-scale corpora, such as BERT (devlin2018bert), XLNet (yang2019xlnet), RoBERTa (liu2019roberta)
, achieved state-of-the-art performance on many NLP tasks. Self-attentive models are also useful beyond NLP, including VisualBERT on vision and language applications(li2019visualbert; su2019vl), image transformer for image generation (parmar2018image), acoustic models for speech recognition, sequential recommendation (kang2018self) and graph embedding (li2019semi).
The robustness of NLP models has been studied, especially many methods have been proposed to generate adversarial examples (papernot2016crafting; jia2017adversarial; zhao2017generating; alzantot2018generating; cheng2018seq2sick; ebrahimi2018hotflip; shi2019adversarial). In particular, hsieh2019robustness showed that Transformers are more robust than LSTMs. However, there is not much work on robustness verification for NLP models. ko2019popqorn verified RNN/LSTM. jia2019certified; huang2019achieving used Interval Bound Propagation (IBP) for certified robustness training of CNN and LSTM. In this paper, we propose the first verification method for Transformers.
We aim to verify the robustness of a Transformer whose input is a sequence of frames . We take binary text classification as a running example, where is a word embedding and the model outputs a score for each class (). Nevertheless, our method for verifying Transformers is general and can also be applied in other applications.
For a clean input sequence
correctly classified by the model, letbe the set of perturbed positions, where is the number of perturbed positions. Thus the perturbed input belongs to . Assuming that is the gold class, the goal of robustness verification is to compute
If , the output score of the correct class is always larger than the incorrect one for any input within . As mentioned previously, computing the exact values of is NP-hard, and thus our goal is to efficiently compute a lower bound .
3.1 Base Framework
We obtain by computing the bounds of each neuron when is perturbed within (
can be regarded as a final neuron). A Transformer layer can be decomposed into a number of sub-layers, where each sub-layer contains neurons after some operations. These operations can be categorized into three categories: 1) linear transformations, 2) unary nonlinear functions, and 3) operations in self-attention. Each sub-layer containspositions in the sequence and each position contains a group of neurons. We assume that the Transformer we verify has sub-layers in total, and the value of the -th neuron at the -th position in the -th sub-layer is , where is a vector for the specified sub-layer and position. Specially, taking . We aim to compute a global lower bound and a global upper bound of for .
We compute bounds from the first sub-layer to the last sub-layer. For neurons in the -th layer, we aim to represent their bounds as linear functions of neurons in a previous layer, the -th layer:
where and are parameters of linear lower and upper bounds respectively. Using linear bounds enables us to efficiently compute bounds with a reasonable tightness. We initially have and . Thereby the right-hand-side of Eq. (2) equals to when . Generally, we use a backward process to propagate the bounds to previous sub-layers, by substituting with linear functions of previous neurons. It can be recursively conducted until the input layer . Since is constant, we can regard the bounds as linear functions of the perturbed embeddings , and take the global bounds for :
where with . These steps resemble to CROWN (zhang2018efficient) which is proposed to verify feed-forward networks. We further support verifying self-attentive Transformers which are more complex than feed-forward networks. Moreover, unlike CROWN that conducts a fully backward process, we combine the backward process with a forward process (see Sec. 3.3) to reduce the computational complexity of verifying Transformers.
3.2 Linear Transformations and Unary Nonlinear Functions
Linear transformations and unary nonlinear functions are basic operations in neural networks. We show how bounds Eq. (2) at the -th sub-layer are propagated to the -th layer.
If the -th sub-layer is connected with the -th sub-layer with a linear transformation where are parameters of the linear transformation, we propagate the bounds to the -th layer by substituting :
where “” means that the equations hold for both lower bounds and upper bounds respectively.
Unary Nonlinear Functions
If the -th layer is obtained from the -th layer with an unary nonlinear function , to propagate linear bounds over the nonlinear function, we first bound with two linear functions of :
where are parameters such that the inequation holds true for all within its bounds computed previously. Such linear relaxations can be done for different functions, respectively. We provide detailed bounds for functions involved in Transformers in Appendix B.
We then back propagate the bounds:
where and mean to retain positive and negative elements in vector respectively and set other elements to 0.
3.3 Self-Attention Mechanism
Self-attention layers are the most challenging parts for verifying Transformers. We assume that is the input to a self-attention layer. We describe our method for computing bounds for one attention head, and bounds for different heads of the multi-head attention in Transformers can be easily concatenated. is first linearly projected to queries , keys , and values with different linear projections, and their bounds can be obtained as described in Sec. 3.2. We also keep their linear bounds that are linear functions of the perturbed embeddings. For convenience, let , where indicates vector concatenation, and thereby we represent the linear bounds as linear functions of :
where and mean that the inequation holds true for queries, keys and values respectively. We then bound the output of the self-attention layer starting from , , .
Bounds of Multiplications and Divisions
We bound multiplications and divisions in the self-attention mechanism with linear functions. We aim to bound bivariate function or with two linear functions and , where are bounds of obtained previously. For , we derive optimal parameters: , , , , . We provide a proof in Appendix C. However, directly bounding is tricky; fortunately, we can bound it indirectly by first bounding a unary function and then bounding the multiplication .
A Forward Process
For the self-attention mechanism, instead of using the backward process like CROWN (zhang2018efficient), we compute bounds with a forward process which we will show later that it can reduce the computational complexity. Attention scores are computed from and : where is the dimension of and . For each multiplication , it is bounded by:
We then obtain the bounds of :
In this way, linear bounds of and are forward propagated to
. Attention scores are normalized into attention probabilities with a softmax, i.e., where is a normalized attention probability. is an unary nonlinear function and can be bounded by . So we forward propagate bounds of to bound with , where:
By summing up bounds of each , linear bounds can be further propagated to . With bounds of and ready, we forward propagate the bounds to with a division similarly to bounding . The output of the self-attention is obtained with a summation of weighted by attention probability : , which can be regarded as a dot product of and , where whose bounds can be obtained from those of with a transposing. Therefore, bounds of and can be forward propagated to similarly to bounding . In this way, we obtain the output bounds of the self-attention:
Recall that is a concatenation of . We can split into vectors with equal dimensions, , such that Eq. (5) becomes
Backward Process to Self-Attention Layers
When computing bounds for a later sub-layer, the -th sub-layer, using the backward process, we directly propagate the bounds at the the closest previous self-attention layer assumed to be the -th layer, to the input layer, and we skip other previous sub-layers. The bounds propagated to the -th layer are as Eq. (2). We substitute with linear bounds in Eq. (6):
Advantageous of Combining the Backward Process with a Forward Process
Introducing a forward process can significantly reduce the complexity of verifying Transformers. With the backward process only, we need to compute and , where the major cost is on and there are such matrices to compute. The factor is from the dependency between all pairs of positions in the input and output respectively, which makes the algorithm inefficient especially when the input sequence is long. In contrast, the forward process represents the bounds as linear functions of the perturbed positions only instead of all positions by computing and . Imperceptible adversarial examples may not have many perturbed positions (gao2018black; ko2019popqorn), and thus we may assume that the number of perturbed positions, , is small. The major cost is on while there are only such matrices and the sizes of and are relatively comparable for a small . We combine the backward process and the forward process. The number of matrices in the forward process is , and for the backward process, since we do not propagate bounds over self-attention layers and there is no cross-position dependency in other sub-layers, we only compute such that , and thus the number of matrices is reduced to . Therefore, the total number of matrices and we compute is and is times smaller than when only the backward process is used. Moreover, the backward process makes bounds tighter compared to solely the forward one, as we explain in Appendix D.
To demonstrate the effectiveness of our algorithm, we compute certified bounds for several sentiment classification models and perform an ablation study to show the advantage of combining the backward and forward processes. We also demonstrate the meaningfulness of our certified bounds with an application on identifying important words.
4.1 Datasets and Models
We use two datasets: Yelp (zhang2015character) and SST (socher2013recursive). Yelp consists of 560,000/38,000 examples in the training/test set and SST consists of 67,349/872/1,821 examples in the training/development/test set. Each example is a sentence or a sentence segment (for the training data of SST only) labeled with a binary sentiment polarity.
We verify the robustness of Transformers trained from scratch. For the main experiments, we consider -layer models (
), with 4 attention heads, hidden sizes of 256 and 512 for self-attention and feed-forward layers respectively, and we use ReLU activations for feed-forward layers. We remove the variance related terms in layer normalization, making Transformers verification bounds tighter while the clean accuracies remain comparable (see AppendixE for discussions). Although our method can be in principal applied to Transformers with any number of layers, we do not use large-scale pre-trained models such as BERT because they are too challenging to be tightly verified with the current technologies.
4.2 Certified Bounds
|Dataset||Acc.||Upper||Lower (IBP)||Lower (Ours)||Ours vs Upper|
|Lower (IBP)||Lower (Ours)||Lower (IBP)||Lower (Ours)|
We compute certified lower bounds for different models on different datasets. We include 1-position perturbation constrained by -norms and 2-position perturbation constrained by -norm. We compare our lower bounds with those computed by the Interval Bound Propagation (IBP) (gowal2018effectiveness) baseline. For 1-position perturbation, we also compare with upper bounds computed by enumerating all the words in the vocabulary and finding the word closest to the original one such that the word substitution alters the predicted label. This method has an exponential complexity with respect to the vocabulary size and can hardly be extended to perturbations on 2 or more positions; thus we do not include upper bounds for 2-position perturbation. For each example, we enumerate possible options of perturbed positions (there are options), and we integrate results from different options by taking the minimum or average respectively. We report the average results on 10 correctly classified random test examples with sentence lengths no more than 32 for 1-position perturbation and 16 for 2-position perturbation. Table 1 and Table 2 present the results for 1-position and 2-position perturbation respectively. Our certified lower bounds are significantly larger and thus tighter than those by IBP. For 1-position perturbation, the lower bounds are consistently smaller than the upper bounds, and the gap between the upper bounds and our lower bounds are reasonable compared with that in previous work on verification of feed-forward networks, e.g., in (weng2018towards; zhang2018efficient) the upper bounds are in the order of 10 times larger than lower bounds. This demonstrates that our proposed method can compute robustness bounds for Transformers in a similar quality to the bounds of simpler neural networks.
4.3 Effectiveness of Combining the Backward Process with a Forward Process
|Dataset||Acc.||Fully-Forward||Fully-Backward||Backward & Forward|
In the following, we show the effectiveness of combining the backward process with a forward process. We compare our proposed method (Backward & Forward) with two variations: 1) Fully-Forward propagates bounds in a forward manner for all sub-layers besides self-attention layers; 2) Fully-Backward computes bounds for all sub-layers including self-attention layers using the backward bound propagation and without the forward process. We compare the tightness of bounds and computation time of the three methods. We use smaller models with the hidden sizes reduced by 75%, and we use 1-position perturbation only, to accommodate Fully-Backward with large computational cost. Experiments are conducted on an NVIDIA TITAN X GPU. Table 3 presents the results. Bounds by Fully-Forward are significantly looser while those by Fully-Backward and Backward & Forward are comparable. Meanwhile, the computation time of Backward & Forward is significantly shorter than that of Fully-Backward. This demonstrates that our method of combining the backward and forward processes can compute comparably tight bounds much more efficiently.
4.4 Identifying Words Important to Prediction
The certified lower bounds can reflect how sensitive a model is to the perturbation of each input word. Intuitively, if a word is more important to the prediction, the model is more sensitive to its perturbation. Therefore, the certified lower bounds can be used to identify important words. In the following, we conduct an experiment to verify whether important words can be identified by our certified lower bounds. We use a 1-layer Transformer classifier under 1-position perturbation constrained by
-norm. We compare our method with two baselines that also estimate local vulnerability: 1)Upper uses upper bounds; 2) Gradient identifies the word whose embedding has the largest -norm of gradients as the most important and vice versa.
Quantitative Analysis on SST
SST contains sentiment labels for all phrases on parse trees, where the labels range from very negative (0) to very positive (4), and 2 for neutral. For each word, assuming its label is , we take , i.e., the distance to the neutral label, as the importance score, since less neutral words tend to be more important for the sentiment polarity of the sentence. We evaluate on 100 random test input sentences and compute the average importance scores of the most or least important words identified from the examples. In Table 4, compared to the baselines (“Upper” and “Grad”), the average importance score of the most important words identified by our lower bounds are the largest, while the least important words identified by our method have the smallest average score. This demonstrates that our method identifies the most and least important words more accurately compared to baseline methods.
Qualitative Analysis on Yelp
We further analyze the results on a larger dataset, Yelp. Since Yelp does not provide per-word sentiment labels, importance scores cannot be computed as on SST. Thus, we demonstrate a qualitative analysis. We use 10 random test examples and collect the words identified as the most and least important word in each example. In Table 4, most words identified as the most important by certified lower bounds are exactly the words reflecting sentiment polarities (boldfaced words), while those identified as the least important words are mostly stopwords. Baseline methods mistakenly identify more words containing no sentiment polarity as the most important. This again demonstrates that our certified lower bounds identify word importance better than baselines and our bounds provide meaningful interpretations in practice. While gradients evaluate the sensitivity of each input word, this evaluation only holds true within a very small neighborhood (where the classifier can be approximated by a first-order Taylor expansion) around the input sentence. Our certified method gives valid lower bounds that hold true within a large neighborhood specified by a perturbation set , and thus it provides more accurate results.
We propose the first robustness verification method for Transformers, and tackle key challenges in verifying Transformers, including cross-nonlinearity and cross-position dependency. Our method computes certified lower bounds that are significantly tighter than those by IBP. Quantitative and qualitative analyses further show that our bounds are meaningful and can reflect the importance of different words in sentiment analysis.
This work is jointly supported by Tsinghua Scholarship for Undergraduate Overseas Studies, Tsinghua University Initiative Scientific Research Program, Tsinghua University DCST Student Academic Training Program, and NSF IIS1719097 and IIS1927554.
Appendix A Illustration of Different Bounding Processes
Figure 1 illustrates a comparison of the Fully-Forward, Fully-Backward and Backward & Forward processes, for a 2-layer Transformer as an example. For Fully-Forward, there are only forward processes connecting adjacent layers and blocks. For Fully-Backward, there are only backward processes, and each layer needs a backward bound propagation to all the previous layers. For our Backward & Forward algorithm, we use backward processes for the feed-forward parts and forward processes for self-attention layers, and for layers after self-attention layers, they no longer need backward bound propagation to layers prior to self-attention layers. In this way, we resolve the cross-position dependency in verifying Transformers while still keeping bounds comparably tight as those by using fully backward processes. Empirical comparison of the three frameworks are presented in Sec. 4.3.
Appendix B Linear Bounds of Unary Nonlinear Functions
We show in Sec. 3.2 that linear bounds can be propagated over unary nonlinear functions as long as the unary nonlinear functions can be bounded with linear functions. Such bounds are determined for each neuron respectively, according to the bounds of the input for the function. Specifically, for a unary nonlinear function , with the bounds of obtained previously as , we aim to derive a linear lower bound and a linear upper bound , such that
where parameters are dependent on and designed for different functions respectively. We introduce how the parameters are determined for different unary nonlinear functions involved in Transformers such that the linear bounds are valid and as tight as possible. Bounds of ReLU and tanh has been discussed by zhang2018efficient, and we further derive bounds of , , , . and are only used when the layer normalization is not modified for experiments to study the impact of our modification. For the following description, we define the endpoints of the function to be bounded within range as and . We describe how the lines corresponding to the linear bounds of different functions can be determined, and thereby parameters can be determined accordingly.
For ReLU activation, . ReLU is inherently linear on segments and respectively, so we make the linear bounds exactly for or ; and for , we take the line passing the two endpoints as the upper bound; and we take when and when as the lower bound, to minimize the gap between the lower bound and the original function.
For activation, . is concave for , and thus we take the line passing the two endpoints as the lower bound and take a tangent line passing as the upper bound. For , is convex, and thus we take the line passing the two endpoints as the upper bound and take a tangent line passing as the lower bound. For , we take a tangent line passing the right endpoint and as the lower bound, and take a tangent line passing the left endpoint and