arXiv(opens in a new tab)Computer ScienceMay 2019Cited by 4 papers on PaperVantage

Manuscript Offprint

arXiv:1905.10650

Are Sixteen Heads Really Better than One?

Paul Michel, Omer Levy, Graham Neubig

Abstract

Attention is a powerful and ubiquitous mechanism for allowing neural models to focus on particular salient pieces of information by taking their weighted average when making predictions. In particular, multi-headed attention is a driving force behind many recent state-of-the-art natural language processing (NLP) models such as Transformer-based MT models and BERT. These models apply multiple attention mechanisms in parallel, with each attention ‘‘head’’ potentially focusing on different parts of the input, which makes it possible to express sophisticated functions beyond the simple weighted average. In this paper we make the surprising observation that even if models have been trained using multiple heads, in practice, a large percentage of attention heads can be removed at test time without significantly impacting performance. In fact, some layers can even be reduced to a single head. We further examine greedy algorithms for pruning down models, and the potential speed, memory efficiency, and accuracy improvements obtainable therefrom. Finally, we analyze the results with respect to which parts of the model are more reliant on having multiple heads, and provide precursory evidence that training dynamics play a role in the gains provided by multi-head attention.

1 Introduction

Transformers (Vaswani et al., 2017) have shown state of the art performance across a variety of NLP tasks, including, but not limited to, machine translation (Vaswani et al., 2017; Ott et al., 2018), question answering (Devlin et al., 2018), text classification (Radford et al., 2018), and semantic role labeling (Strubell et al., 2018). Central to its architectural improvements, the Transformer extends the standard attention mechanism (Bahdanau et al., 2015; Cho et al., 2014) via multi-headed attention (MHA), where attention is computed independently by NhN_{h} parallel attention mechanisms (heads). It has been shown that beyond improving performance, MHA can help with subject-verb agreement (Tang et al., 2018) and that some heads are predictive of dependency structures (Raganato and Tiedemann, 2018). Since then, several extensions to the general methodology have been proposed (Ahmed et al., 2017; Shen et al., 2018).

However, it is still not entirely clear: what do the multiple heads in these models buy us? In this paper, we make the surprising observation that – in both Transformer-based models for machine translation and BERT-based (Devlin et al., 2018) natural language inference – most attention heads can be individually removed after training without any significant downside in terms of test performance (§3.2). Remarkably, many attention layers can even be individually reduced to a single attention head without impacting test performance (§3.3).

Based on this observation, we further propose a simple algorithm that greedily and iteratively prunes away attention heads that seem to be contributing less to the model. By jointly removing attention heads from the entire network, without restricting pruning to a single layer, we find that large parts of the network can be removed with little to no consequences, but that the majority of heads must remain to avoid catastrophic drops in performance (§4). We further find that this has significant benefits for inference-time efficiency, resulting in up to a 17.5% increase in inference speed for a BERT-based model.

We then delve into further analysis. A closer look at the case of machine translation reveals that the encoder-decoder attention layers are particularly sensitive to pruning, much more than the self-attention layers, suggesting that multi-headedness plays a critical role in this component (§5). Finally, we provide evidence that the distinction between important and unimporant heads increases as training progresses, suggesting an interaction between multi-headedness and training dynamics (§6).

References (37)

  • Weighted transformer network for machine translation (opens arXiv in a new tab)
  • Structured pruning of deep convolutional neural networks
  • Neural machine translation by jointly learning to align and translate
  • Layer-wise relevance propagation for neural networks with local renormalization layers
  • Report on the 11 th iwslt evaluation campaign , iwslt 2014
  • Long short-term memory-networks for machine reading
  • Learning phrase representations using rnn encoder–decoder for statistical machine translation
  • Transformer-XL: Attentive Language Models beyond a Fixed-Length Context
  • Bert: Pre-training of deep bidirectional transformers for language understanding
  • Dolan and Chris Brockett
  • Learning both weights and connections for efficient neural network
  • Second order derivatives for network pruning: Optimal brain surgeon
  • Sequence-level knowledge distillation
  • Moses: Open source toolkit for statistical machine translation
  • Optimal brain damage
  • Pruning Filters for Efficient ConvNets
  • Effective approaches to attention-based neural machine translation
  • MTNT: A testbed for machine translation of noisy text
  • Pruning convolutional neural networks for resource efficient inference
  • Auto-sizing neural networks: With applications to n-gram language models
  • compare-mt: A tool for holistic comparison of language generation systems (opens arXiv in a new tab)
  • Scaling neural machine translation
  • A decomposable attention model for natural language inference
  • A deep reinforced model for abstractive summarization
  • Improving language understanding with unsupervised learning
  • Language models are unsupervised multitask learners
  • An analysis of encoder representations in transformer-based machine translation
  • Compression of neural machine translation models via pruning
  • Disan: Directional self-attention network for rnn/cnn-free language understanding
  • Opening the black box of deep neural networks via information (opens arXiv in a new tab)
  • Recursive deep models for semantic compositionality over a sentiment treebank
  • Linguistically-informed self-attention for semantic role labeling
  • Why self-attention? a targeted evaluation of neural machine translation architectures
  • Attention is all you need
  • Analyzing multi-head self-attention: Specialized heads do the heavy lifting, the rest can be pruned
  • Neural network acceptability judgments (opens arXiv in a new tab)
  • A broad-coverage challenge corpus for sentence understanding through inference