DropAttention: A Regularization Method for Fully-Connected Self-Attention Networks
Variants dropout methods have been designed for the fully-connected layer, convolutional layer and recurrent layer in neural networks, and shown to be effective to avoid overfitting. As an appealing alternative to recurrent and convolutional layers, the fully-connected self-attention layer surprisingly lacks a specific dropout method. This paper explores the possibility of regularizing the attention weights in Transformers to prevent different contextualized feature vectors from co-adaption. Experiments on a wide range of tasks show that DropAttention can improve performance and reduce overfitting.
As an effective and easy-to-implement regularization method, Dropout has been first designed for fully-connected layers in neural models Srivastava et al. (2014). Over the past few years, a host of variants of dropout have been introduced. For recurrent neural networks (RNNs), dropout is only applied to the input layers before the successful attempt in Krueger et al. (2016); Semeniuta et al. (2016); Gal and Ghahramani (2016). Also, a dozen of dropout methods for convolutional neural networks (CNNs) have been proposed in Tompson et al. (2015); Huang et al. (2016); Larsson et al. (2016); Gastaldi (2017); Ghiasi et al. (2018); Zoph et al. (2018); Yamada et al. (2018). On the other hand, fully-connected self-attention neural networks, such as Transformers Vaswani et al. (2017), have emerged as a very appealing alternative to RNNs and CNNs when dealing with sequence modelling tasks.
Although Transformers incorporate dropout operators in their architecture, the regularization effect of dropout in the self-attention has not been thoroughly analyzed in the literature.
The success of the adaption of dropout for fully-connected, convolutional and recurrent layers gives us a tantalizing hint that a more specific dropout for self-attentional operators might be needed. Additionally, the original publicly code111https://github.com/tensorflow/tensor2tensor of Transformer Vaswani et al. (2017) with Dropout trick also provides the evidence for this work, although it’s less understood why it works and how it might be extended. In this paper, we demonstrate the benefit of dropout in self-attention layers (DropAttention) with two key distinctions compared with the dropout used in fully-connected layers and recurrent layers. The first is that DropAttention randomly sets attention weights to zero, which can be interpreted as dropping a set of neurons along different dimensions. Specifically, DropAttention aims to encourage the model to utilize the full context of the input sequences rather than relying solely on a small piece of features. For example, for sentiment classification, the prediction is usually dominated by one or several emotional words, ignoring other informative patterns. This can make the model overfit some specific patterns. In fully-connected and recurrent layers, dropout discourages the complex co-adaptation of different units in the same layer, while DropAttention prevents different contextualized feature vectors from co-adapting, learning features which are generally helpful for task-specific prediction. Secondly, in addition to dropping out individual attentional units, we also explore the possibility of operating in contiguous regions. It is inspired by DropBlock Ghiasi et al. (2018) where units in a contiguous region of a convolutional feature map are discarded together. It is a more effective way of dropping for attention layers, since a semantic unit are usually composed of several spatially consecutive words. Experiments on a wide range of tasks with different-scale datasets show that DropAttention can improve performance and reduce overfitting.
2 Related Work
|Dropout Srivastava et al. (2014)||Unit||Hidden||FCN|
|DropConnect Wan et al. (2013)||Weight||Hidden||FCN|
|SpatialDropout Kalchbrenner et al. (2014)||Unit||Hidden||CNN|
|Cutout DeVries and Taylor (2017)||Unit||Input||CNN|
|DropEmb Gal and Ghahramani (2016)||Weight||Input||Lookup|
|Variational Dropout (Gal and Ghahramani, 2016)||Unit||Hidden||RNN|
|Zoneout Krueger et al. (2016)||Unit||Hidden||RNN|
|DropBlock Ghiasi et al. (2018)||Region of Units||Hidden||CNN|
|DropAttention||Region of Weights||Input& Hidden||Self-Attention|
We present a summary of existing models by highlighting differences among dropped object, spaces and layers as shown in Table 1. The original idea of Dropout is proposed by Srivastava et al. (2014) for fully-connected networks, which is regarded as an effective regularization method. After that, many dropout techniques for specific network architectures, such as CNNs and RNNs, have been proposed. For CNNs, most successful methods require the noise to be structured Tompson et al. (2015); Huang et al. (2016); Larsson et al. (2016); Gastaldi (2017); Ghiasi et al. (2018); Zoph et al. (2018); Yamada et al. (2018). For example, SpatialDropoutKalchbrenner et al. (2014) is used to address the spatial correlation problem. DropConnect Wan et al. (2013) sets a randomly selected subset of weights within the network to zero. For RNNs, Variational Dropout Gal and Ghahramani (2016) and ZoneOut Krueger et al. (2016) are most widely used methods. In Variational Dropout, dropout rate is learned and the same neurons are dropped at every timestep. In ZoneOut, it stochastically forces some hidden units to maintain their previous values instead of dropping. Different from these methods, in this paper, we explore how to drop information on self-attention layers.
3.1 Transformer Architecture
The typical fully-connected self-attention architecture is the Transformer Vaswani et al. (2017), which uses the scaled dot-product attention to model the intra-interactions of a sequence. Given a sequence of vectors , where and represent the length of and the dimension of the input sequence, the self-attention projects into three different matrices: the query matrix , the key matrix and the value matrix vector , and uses scaled dot-product attention to get the output representation.
where are learnable parameters and is performed row-wise.
To enhance the ability of self-attention, multi-head self-attention is introduced as an extension of the single head self-attention, which jointly model the multiple interactions from different representation spaces,
where are learnable parameters. Transformer consists of several stacked multi-head self-attention layers and fully-connected layers. Assuming the input of the self-attention layer is , its output is calculated by
where represents the layer normalization Ba et al. (2016) .
Besides, since the self-attention ignores the order information of a sequence, a positional embedding is used to represent the positional information.
Dropout Srivastava et al. (2014) is a popular regularization form for fully-connected neural network. It breaks up co-adaptation between units, therefore it can significantly reduce overfitting and improve test performance. Each unit of a hidden layer is dropped with probability by setting it to 0.
where is a binary mask vector with each element drawn independently from , and denotes element-wise production.
DropConnect Wan et al. (2013) is a generalization of Dropout. It randomly drops hidden layers weights instead of units. Assume is a binary mask matrix drawn from , is the hidden layer weights. Then DropConnect can be formulated as,
Dropout essentially drops the entire column of the weight matrix. Therefore, Dropout can be regarded as a special case of DropConnect, where a whole column weight is dropped.
Since Dropout and DropConnect achieve great success on fully-connected layer, a natural motivation is whether a specific dropout method is needed for the fully-connected self-attention networks. Experiments conducted shows that a new dropout method designed for fully-connected self-attention networks can also reduce overfitting and obtain improvements.
In this section, we will introduce our attention regularization method: DropAttention.
Given a sequence of vectors , the fully-connected self-attention layer can be reformulated into
The output of -th position is
where is the -th row vector of and is the -th row vector of . is the entry of .
With this formulation, we can connect the self-attention layer to the fully-connected layer with two differences. The first difference is the weight matrix is dynamically generated. The second difference is that the basic unit is a vector rather than a neuron.
Due to the similarity between fully-connected layer and self-attention layer, we can introduce the popular dropout methods for FCN to self-attention mechanism. In detail, we propose two dropout methods for the fully-connected self-attention layer: DropAttention(c) and DropAttention(e).
1) DropAttention(c) means to randomly drop “column” in attention weight matrix, which is a simple method similar to the standard Dropout Srivastava et al. (2014). We randomly drop the unit . Note that here is a vector instead of a single neuron.
2) DropAttention(e) means to randomly drop “element” in attention weight matrix, which is a more generalized method of the DropAttention(c). Similar to DropConnect Wan et al. (2013), DropAttention(e) randomly drops elements in attention weights matrix . DropAttention(c) can be regarded as a special case of DropAttention(e) in which a whole column of is dropped.
Besides the basic dropping strategies, we also augment the DropAttentions with two functions.
4.1 Dropping Contiguous Region
Inspired by DropBlock Ghiasi et al. (2018), we drop contiguous region of the attention weights matrix instead of independent random units. The behind motivation is based on distributional hypothesis Harris (1954): words that are used and occur in the same contexts tend to purport similar meanings. In Transformer Vaswani et al. (2017) where multi-layer structure is used, when dropping independent random units, information correlated with the dropped input can still be restored in the next layer through surrounding words, which may cause the networks overfitting. Dropping the whole semantic unit consisting of several words can be a more effective way of dropping out.
Therefore, there are two hyperparameters in DropAttention: window size and drop rate . The window size is the length of contiguous region to be dropped, while controls how many units to drop. In standard Dropout Srivastava et al. (2014), the binary mask is sampled from the Bernoulli distribution with the probability of . Since DropAttention will expand every zero entry in the binary mask to be window with size . Therefore, we just require to drop windows.
4.2 Normalized Rescaling
To ensure that the sum of attention weights to remain 1 after applying DropAttention, we re-normalize the attention weights after dropout operations. While traditional Dropout also has rescaling operation where neuron weights are divided by , there is no guarantee that the sum of attention weights after rescaling remains 1. Experiments on classification task (see sec. 5.1) show that DropAttention with normalized rescaling outperforms traditional dropout rescaling. And in practice with normalized rescale, training process can be more steady compared to traditional rescaling.
We evaluate the effectiveness of DropAttentions on 4 different tasks: Text Classification, Sequence Labeling, Textual Entailment and Machine Translation. We also conduct a set of analytical experiments to validate properties of the networks.
5.1 Text Classification
We first evaluate the effectiveness of DropAttention on a couple of classification datasets ranging from small, medium and large scale. Statistics of datasets are listed in Table 2. All datasets are split into training, development and testing sets.
Yelp13 reviews: collected from the Yelp Dataset Challenge in 2013, which have 5 levels of ratings from 1 to 5. We use the same Yelp datasets slitted and tokenized in Tang et al. (2015). MR: Movie reviews with two classes Pang and Lee (2005). SUBJ: Subjectivity dataset containing subjective and objective instance. It is also a 2 classes dataset Pang and Lee (2004). CR: Customer reviews of various products with positive and negative sentiments. AG’s News: A news topic classification with 4 classes created by Zhang et al. (2015). QC: The TREC questions dataset involves six different question types Li and Roth (2002).
|p=0.4,w=2||p=0.2,w=3||p=0.3,w=2||p=0.3 w=1||p=0.4 w=1||p=0.4 w=1|
Detail model configurations are given in Appendix. We use accuracy as evaluation metrics. Results of all datasets are listed in Table 3. It shows that DropAttentions can significantly improve performance on a wide range of datasets of small, medium and large scale. Besides, note that when comparing normalized rescaling with traditional rescaling under the same DropAttention hyperparameters, Table 3 shows that normalized rescaling can generally obtain better performance.
For classification tasks, we find that larger dropout rate and smaller window size are preferred for DropAttention(c) while smaller dropout rate and larger window size are preferred for DropAttention(e). And DropAttention(c) can generally obtain higher performances than DropAttention(e) in classification tasks.
5.2 Sequence Labeling
We also evaluate the effectiveness of DropAttention on sequence labeling. We conducted experiments by following the same settings as Yang et al. (2016). We use the following benchmark datasets in our experiments: Penn Treebank (PTB) POS tagging, CoNLL 2000 chunking, CoNLL 2003 English NER. The statistics of the datasets are described in Table 4.
We process sentences with Transformer encoder. After encoding, we feed the output vector into a fully-connected layer. Detail model hyperparameters are given in Appendix.
Results are shown in Table 6. In Table 6, all best results are under the hyperparameters of except for DropAttention(e) in POS task with . It shows that both DropAttention(c) and DropAttention(e) can obtain significant improvements. Our model achieve 0.29 accuracy, 0.40 F1 score, 1.76 F1 score improvements in POS, NER and Chunking respectively. And we find that larger dropout rate and larger window size are generally preferred.
|p=0.3 w=3||p=0.3 w=3||p=0.3 w=3|
5.3 Textual Entailment
We use the biggest textual entailment dataset, SNLI Bowman et al. (2015) corpus to evaluate the effectiveness of DropAttention on this task. SNLI is a collection of sentence pairs labeled for entailment, contradiction, and semantic independence. A pair of sentences called premise and hypothesis will be fed to the model, and the model will be asked to tell the relation of two sentences. It is also a classification task, and we measure the performance by accuracy.
We process the hypothesis and premise with the same Transformer encoder, which means that the hypothesis encoder and the premise encoder share the same parameters. We use max pooling to create a simple vector representation from the output of transformer encoder. After processing two sentences respectively, we use the two outputs to construct the final feature vector, which consisting of the concatenation of two sentence vectors, their difference, and their elementwise product Bowman et al. (2016). We then feed the final feature vector into a 2-layer ReLU MLP to map the hidden representation into classification result. Detail model hyperparameters are given in Appendix.
Results are listed in Table 8. For full results with different hyperparameters please refer to Appendix. Experiments show that DropAttention(c) and DropAttention(e) can significantly improve performances.
5.4 Machine Translation
We further demonstrate the effectiveness of DropAttention on translation tasks. We conduct experiments on WMT’ 16 En-De dataset which consists of 4.5M sentence pairs. We follow Ott et al. (2018) by reusing the preprocessed data, where Ott et al. (2018) validates on newstest13 and tests on newstest14, and uses a vocabulary of 32K symbols based on a joint source and target byte pair encoding (BPE; Sennrich et al. (2015)). We measure case-sensitive tokenized BLEU. We use the fairseq-py toolkit 222https://github.com/pytorch/fairseq re-implementation of Transformer Vaswani et al. (2017) model. We follow the configuration of original Transformer base model Vaswani et al. (2017). See detail configuration in Appendix. DropAttention with different hyperparameters is applied to attention weights.
Table 6 shows the BLEU score for DropAttention with different hyperparameters. The results show that DropAttention can generally obtain higher performance compared with baseline without DropAttention. With DropAttention(e) of , the model can outperform the baseline by a large margin, reaching a BLEU score of 28.32. For DropAttention(c), the model also reaches the best BLEU score with .
There are two insights from this experiment. The first is that a regularization of self-attention works to improve the generalization ability even for the large-scale data. The second is that the DropAttention is complementary to the standard dropout.
5.5 Complementarity to stardard Dropout
We also explore the effect of DropAttention combining with standard Dropout. We conduct experiments on classification tasks and machine translation tasks. We choose AG’s News as classification dataset and WMT’ 16 En-De as Machine Translation dataset. Same hyperparameters as 5.1 and 5.4 are used. Table 8 shows that when combining DropAttention with Dropout, models can obtain higher performances compared to implementing Dropout or DropAttention alone. It implys that DropAttention is complementary to stardard Dropout.
|+ Standard Dropout||88.43||27.3|
In this section, we study the impact of DropAttention on the behavior of model quantitatively. We use three metrics to evaluate the model based on the attention weights: Div, Disagreement and Entropy.
Div Suppose is the attention weights matrix, where every row i corresponds to the attention weights vector produced by the attention head. Div is defined as,
where represents the Frobenius norm of a matrix and stands for identity matrix. It was first introduced by Lin et al. (2017) as a penalization term which encourages the diversity of weight vectors across different heads of attention. If Div gets large, it means multi-heads attention weights distributions have large overlap.
Disagreement We use the same notations above. stands for the row of the attention matrix, then the Disagreement is expressed as,
where denotes the number of heads. It was proposed by Li et al. (2018), which also expects to encourage the diversity of the model. The Disagreement is defined as calculating the cosine similarity between the attention weights vector pair produced by two different heads. The smaller score is, the more diverse different attention heads are.
Entropy is used to evaluate the diversity within one head. is the element of the attention weights vector produced by head. Entropy of attention weights is defined as,
If entropy gets small, it represents that the head focus on a small fraction of words.
6.1 Effect on Intra-Diversity
We first observe the impact of DropAttention on intra-diversity, namely attention distribution within one head. Figure (a)a shows the multi-head entropy of models for classification task. When the drop rate and window size increasing, the entropy increase accordingly. This suggests DropAttention can effectively smoothen the attention distribution, making the model utilize more context. This can subsequently increase robustness of the model.
6.2 Effect on Inter-Diversity
We further study the impact of DropAttention on inter-diversity, namely the difference between multi heads. Figure (b)b and (c)c show the Disagreement and Diveristy of multi heads, respectively. It shows that with larger drop rate and window size, Div and Disagreement are larger accordingly. Note that large Diversity and Disagreement means that the difference of attention distribution between heads is small. This is due to the smoother attention distribution within one head. With less sharply different multi-heads, the model does not have to rely on a single head to make predictions, which means that all heads have a smoother contribution to the final predictions. This can increase robustness of the model.
6.3 Effect on Sparsity
Similar to Srivastava et al. (2014), we also observe the effect of DropAttention on sparsity. Since the attention weights are summed up to 1, we only collect the largest attention weights of all heads. To eliminate the effect of sentence length, attention weights are multiplied by the sentence length. Figure 3 shows the distribution of largest attention weights, where model with DropAttention has smaller attention weights compared to model without DropAttention. This phenomenon is consistent with Srivastava et al. (2014) where model with dropout tends to allocate smaller activation weights compared to model without dropout.
7 Conclusion and Discussion
In this paper, we introduce DropAttention, a variant of Dropout designed for fully-connected self-attention network. Experiments on a wide range of tasks demonstrate that DropAttention is an effective technique for improving generalization and reducing overfitting of self-attention networks. Several analytical statistics give the intuitive impacts of DropAttention, which show that applying DropAttention can help model utilize more context, subsequently increasing robustness.
- Ba et al. (2016) Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E Hinton. Layer normalization. arXiv preprint arXiv:1607.06450, 2016.
- Bowman et al. (2015) Samuel R Bowman, Gabor Angeli, Christopher Potts, and Christopher D Manning. A large annotated corpus for learning natural language inference. arXiv preprint arXiv:1508.05326, 2015.
- Bowman et al. (2016) Samuel R Bowman, Jon Gauthier, Abhinav Rastogi, Raghav Gupta, Christopher D Manning, and Christopher Potts. A fast unified model for parsing and sentence understanding. arXiv preprint arXiv:1603.06021, 2016.
- DeVries and Taylor (2017) Terrance DeVries and Graham W Taylor. Improved regularization of convolutional neural networks with cutout. arXiv preprint arXiv:1708.04552, 2017.
- Gal and Ghahramani (2016) Yarin Gal and Zoubin Ghahramani. A theoretically grounded application of dropout in recurrent neural networks. In Advances in neural information processing systems, pages 1019–1027, 2016.
- Gastaldi (2017) Xavier Gastaldi. Shake-shake regularization. arXiv preprint arXiv:1705.07485, 2017.
- Ghiasi et al. (2018) Golnaz Ghiasi, Tsung-Yi Lin, and Quoc V Le. Dropblock: A regularization method for convolutional networks. In Advances in Neural Information Processing Systems, pages 10750–10760, 2018.
- Harris (1954) Zellig S Harris. Distributional structure. Word, 10(2-3):146–162, 1954.
- Huang et al. (2016) Gao Huang, Yu Sun, Zhuang Liu, Daniel Sedra, and Kilian Q Weinberger. Deep networks with stochastic depth. In European conference on computer vision, pages 646–661. Springer, 2016.
- Kalchbrenner et al. (2014) Nal Kalchbrenner, Edward Grefenstette, and Phil Blunsom. A convolutional neural network for modelling sentences. In Proceedings of ACL, 2014.
- Krueger et al. (2016) David Krueger, Tegan Maharaj, János Kramár, Mohammad Pezeshki, Nicolas Ballas, Nan Rosemary Ke, Anirudh Goyal, Yoshua Bengio, Aaron Courville, and Chris Pal. Zoneout: Regularizing rnns by randomly preserving hidden activations. arXiv preprint arXiv:1606.01305, 2016.
- Larsson et al. (2016) Gustav Larsson, Michael Maire, and Gregory Shakhnarovich. Fractalnet: Ultra-deep neural networks without residuals. arXiv preprint arXiv:1605.07648, 2016.
- Li and Roth (2002) Xin Li and Dan Roth. Learning question classifiers. In Proceedings of the 19th international conference on Computational linguistics-Volume 1, pages 1–7. Association for Computational Linguistics, 2002.
- Li et al. (2018) Jian Li, Zhaopeng Tu, Baosong Yang, Michael R Lyu, and Tong Zhang. Multi-head attention with disagreement regularization. arXiv preprint arXiv:1810.10183, 2018.
- Lin et al. (2017) Zhouhan Lin, Mo Feng, Yu, Bing Xiang, Bowen Zhou, and Yoshua Bengio. A structured self-attentive sentence embedding. arXiv preprint arXiv:1703.03130, 2017.
- Ott et al. (2018) Myle Ott, Sergey Edunov, David Grangier, and Michael Auli. Scaling neural machine translation. CoRR, abs/1806.00187, 2018.
- Pang and Lee (2004) Bo Pang and Lillian Lee. A sentimental education: Sentiment analysis using subjectivity summarization based on minimum cuts. In Proceedings of the 42nd annual meeting on Association for Computational Linguistics, page 271. Association for Computational Linguistics, 2004.
- Pang and Lee (2005) Bo Pang and Lillian Lee. Seeing stars: Exploiting class relationships for sentiment categorization with respect to rating scales. In Proceedings of the 43rd annual meeting on association for computational linguistics, pages 115–124. Association for Computational Linguistics, 2005.
- Semeniuta et al. (2016) Stanislau Semeniuta, Aliaksei Severyn, and Erhardt Barth. Recurrent dropout without memory loss. arXiv preprint arXiv:1603.05118, 2016.
- Sennrich et al. (2015) Rico Sennrich, Barry Haddow, and Alexandra Birch. Neural machine translation of rare words with subword units. arXiv preprint arXiv:1508.07909, 2015.
- Srivastava et al. (2014) Nitish Srivastava, Geoffrey Hinton, Alex Krizhevsky, Ilya Sutskever, and Ruslan Salakhutdinov. Dropout: a simple way to prevent neural networks from overfitting. The Journal of Machine Learning Research, 15(1):1929–1958, 2014.
- Tang et al. (2015) Duyu Tang, Bing Qin, and Ting Liu. Document modeling with gated recurrent neural network for sentiment classification. In Proceedings of the 2015 conference on empirical methods in natural language processing, pages 1422–1432, 2015.
- Tompson et al. (2015) Jonathan Tompson, Ross Goroshin, Arjun Jain, Yann LeCun, and Christoph Bregler. Efficient object localization using convolutional networks. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pages 648–656, 2015.
- Vaswani et al. (2017) Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000–6010, 2017.
- Wan et al. (2013) Li Wan, Matthew Zeiler, Sixin Zhang, Yann Le Cun, and Rob Fergus. Regularization of neural networks using dropconnect. In International Conference on Machine Learning, pages 1058–1066, 2013.
- Yamada et al. (2018) Yoshihiro Yamada, Masakazu Iwamura, Takuya Akiba, and Koichi Kise. Shakedrop regularization for deep residual learning. arXiv preprint arXiv:1802.02375, 2018.
- Yang et al. (2016) Zhilin Yang, Ruslan Salakhutdinov, and William Cohen. Multi-task cross-lingual sequence tagging from scratch. arXiv preprint arXiv:1603.06270, 2016.
- Zhang et al. (2015) Xiang Zhang, Junbo Zhao, and Yann LeCun. Character-level convolutional networks for text classification. In Advances in neural information processing systems, pages 649–657, 2015.
- Zoph et al. (2018) Barret Zoph, Vijay VasudSentiment analysis using subjectivity summarizationevan, Jonathon Shlens, and Quoc V Le. Learning transferable architectures for scalable image recognition. In Proceedings of the IEEE conference on computer vision and pattern recognition, pages 8697–8710, 2018.