Analyzing the Structure of Attention in a Transformer Language Model
Total Page:16
File Type:pdf, Size:1020Kb
Analyzing the Structure of Attention in a Transformer Language Model Jesse Vig Yonatan Belinkov Palo Alto Research Center Harvard John A. Paulson School of Machine Learning and Engineering and Applied Sciences and Data Science Group MIT Computer Science and Interaction and Analytics Lab Artificial Intelligence Laboratory Palo Alto, CA, USA Cambridge, MA, USA [email protected] [email protected] Abstract attention in NLP models, ranging from attention matrix heatmaps (Bahdanau et al., 2015; Rush The Transformer is a fully attention-based et al., 2015; Rocktaschel¨ et al., 2016) to bipartite alternative to recurrent networks that has graph representations (Liu et al., 2018; Lee et al., achieved state-of-the-art results across a range of NLP tasks. In this paper, we analyze 2017; Strobelt et al., 2018). A visualization tool the structure of attention in a Transformer designed specifically for multi-head self-attention language model, the GPT-2 small pretrained in the Transformer (Jones, 2017; Vaswani et al., model. We visualize attention for individ- 2018) was introduced in Vaswani et al.(2017). ual instances and analyze the interaction be- We extend the work of Jones(2017), by visu- tween attention and syntax over a large cor- alizing attention in the Transformer at three lev- pus. We find that attention targets different parts of speech at different layer depths within els of granularity: the attention-head level, the the model, and that attention aligns with de- model level, and the neuron level. We also adapt pendency relations most strongly in the mid- the original encoder-decoder implementation to dle layers. We also find that the deepest layers the decoder-only GPT-2 model, as well as the of the model capture the most distant relation- encoder-only BERT model. ships. Finally, we extract exemplar sentences In addition to visualizing attention for individ- that reveal highly specific patterns targeted by ual inputs to the model, we also analyze attention particular attention heads. in aggregate over a large corpus to answer the fol- 1 Introduction lowing research questions: Contextual word representations have recently • Does attention align with syntactic depen- been used to achieve state-of-the-art perfor- dency relations? mance across a range of language understanding tasks (Peters et al., 2018; Radford et al., 2018; • Devlin et al., 2018). These representations are Which attention heads attend to which part- obtained by optimizing a language modeling (or of-speech tags? similar) objective on large amounts of text. The underlying architecture may be recurrent, as in • How does attention capture long-distance re- ELMo (Peters et al., 2018), or based on multi-head lationships versus short-distance ones? self-attention, as in OpenAI’s GPT (Radford et al., 2018) and BERT (Devlin et al., 2018), which are We apply our analysis to the GPT-2 small pre- based on the Transformer (Vaswani et al., 2017). trained model. We find that attention follows de- Recently, the GPT-2 model (Radford et al., 2019) pendency relations most strongly in the middle outperformed other language models in a zero- layers of the model, and that attention heads tar- shot setting, again based on self-attention. get particular parts of speech depending on layer An advantage of using attention is that it can depth. We also find that attention spans the great- help interpret the model by showing how the est distance in the deepest layers, but varies signif- model attends to different parts of the input (Bah- icantly between heads. Finally, our method for ex- danau et al., 2015; Belinkov and Glass, 2019). tracting exemplar sentences yields many intuitive Various tools have been developed to visualize patterns. 63 Proceedings of the Second BlackboxNLP Workshop on Analyzing and Interpreting Neural Networks for NLP, pages 63–76 Florence, Italy, August 1, 2019. c 2019 Association for Computational Linguistics 2 Related Work multi-head setting, the queries, keys, and values are linearly projected h times, and the attention Recent work suggests that the Transformer im- operation is performed in parallel for each repre- plicitly encodes syntactic information such as de- sentation, with the results concatenated. pendency parse trees (Hewitt and Manning, 2019; Raganato and Tiedemann, 2018), anaphora (Voita et al., 2018), and subject-verb pairings (Goldberg, 4 Visualizing Individual Inputs 2019; Wolf, 2019). Other work has shown that RNNs also capture syntax, and that deeper layers In this section, we present three visualizations of in the model capture increasingly high-level con- attention in the Transformer model: the attention- structs (Blevins et al., 2018). head view, the model view, and the neuron view. In contrast to past work that measure a model’s Source code and Jupyter notebooks are avail- syntactic knowledge through linguistic probing able at https://github.com/jessevig/ tasks, we directly compare the model’s atten- bertviz, and a video demonstration can be tion patterns to syntactic constructs such as de- found at https://vimeo.com/339574955. pendency relations and part-of-speech tags. Ra- A more detailed discussion of the tool is provided ganato and Tiedemann(2018) also evaluated de- in Vig(2019). pendency trees induced from attention weights in a Transformer, but in the context of encoder-decoder 4.1 Attention-head View translation models. The attention-head view (Figure1) visualizes at- 3 Transformer Architecture tention for one or more heads in a model layer. Self-attention is depicted as lines connecting the Stacked Decoder: GPT-2 is a stacked decoder attending tokens (left) with the tokens being at- Transformer, which inputs a sequence of tokens tended to (right). Colors identify the head(s), and and applies position and token embeddings fol- line weight reflects the attention weight. This view lowed by several decoder layers. Each layer ap- closely follows the design of Jones(2017), but has plies multi-head self-attention (see below) in com- been adapted to the GPT-2 model (shown in the bination with a feedforward network, layer nor- figure) and BERT model (not shown). malization, and residual connections. The GPT-2 small model has 12 layers and 12 heads. Self-Attention: Given an input x, the self- attention mechanism assigns to each token xi a set of attention weights over the tokens in the input: Attn(xi) = (αi;1(x); αi;2(x); :::; αi;i(x)) (1) where αi;j(x) is the attention that xi pays to xj. The weights are positive and sum to one. Attention in GPT-2 is right-to-left, so αi;j is defined only for j ≤ i. In the multi-layer, multi-head setting, α is specific to a layer and head. The attention weights αi;j(x) are computed from the scaled dot-product of the query vector of xi and the key vector of xj, followed by a softmax operation. The attention weights are then used to produce a weighted sum of value vectors: Figure 1: Attention-head view of GPT-2 for layer 4, head 11, which focuses attention on previous token. QKT Attention(Q; K; V ) = softmax( p )V (2) This view helps focus on the role of specific at- d k tention heads. For instance, in the shown example, using query matrix Q, key matrix K, and value the chosen attention head attends primarily to the matrix V , where dk is the dimension of K. In a previous token position. 64 5 Analyzing Attention in Aggregate In this section we explore the aggregate proper- ties of attention across an entire corpus. We ex- amine how attention interacts with syntax, and we compare long-distance versus short-distance rela- tionships. We also extract exemplar sentences that reveal patterns targeted by each attention head. 5.1 Methods 5.1.1 Part-of-Speech Tags Past work suggests that attention heads in the Transformer may specialize in particular linguis- Figure 2: Model view of GPT-2, for same input as in tic phenomena (Vaswani et al., 2017; Raganato Figure1 (excludes layers 6–11 and heads 6–11). and Tiedemann, 2018; Vig, 2019). We explore whether individual attention heads in GPT-2 target 4.2 Model View particular parts of speech. Specifically, we mea- sure the proportion of total attention from a given The model view (Figure2) visualizes attention head that focuses on tokens with a given part-of- across all of the model’s layers and heads for a speech tag, aggregated over a corpus: particular input. Attention heads are presented in tabular form, with rows representing layers and jxj i P P P 1 columns representing heads. Each head is shown αi;j(x)· pos(xj )=tag x2X i=1 j=1 in a thumbnail form that conveys the coarse shape Pα(tag) = (3) jxj i of the attention pattern, following the small multi- P P P αi;j(x) ples design pattern (Tufte, 1990). Users may also x2X i=1 j=1 click on any head to enlarge it and see the tokens. This view facilitates the detection of coarse- where tag is a part-of-speech tag, e.g., NOUN, x is grained differences between heads. For example, a sentence from the corpus X, αi;j is the attention several heads in layer 0 share a horizontal-stripe from xi to xj for the given head (see Section3), pattern, indicating that tokens attend to the current and pos(xj) is the part-of-speech tag of xj. We position. Other heads have a triangular pattern, also compute the share of attention directed from showing that they attend to the first token. In the each part of speech in a similar fashion. deeper layers, some heads display a small number of highly defined lines, indicating that they are tar- 5.1.2 Dependency Relations geting specific relationships between tokens.