Context-Aware Self-Attention Networks.

Baosong Yang, Jian Li, Derek F. Wong, Lidia S. Chao, Xing Wang, Zhaopeng Tu

TL;DR

This paper introduces "Context-Aware Self-Attention Networks" (CASANs) that enhance standard self-attention mechanisms by incorporating contextual information directly into the query and key transformations. This approach improves performance on machine translation tasks without sacrificing the efficiency and flexibility of self-attention.

What Problem Does This Paper Address?

Self-attention networks (SANs), like those in the Transformer architecture, excel at capturing long-range dependencies in sequences due to their parallelizable nature. However, a key limitation is that they treat each token's representation in isolation when calculating attention scores. This means the model doesn't inherently consider the surrounding context of a word when determining its relationship with other words. The authors argue that this lack of contextual awareness hinders performance, especially in tasks where understanding the broader meaning of a sentence or document is crucial. They aim to bridge this gap by enriching the self-attention mechanism with contextual information.

Methods

The core innovation is to "contextualize" the transformations of the query (Q) and key (K) vectors in the self-attention mechanism. Instead of simply transforming the input representations, they augment these transformations with context vectors. This is achieved by adding a context vector $C$ (which can be derived in various ways) to the standard linear transformations for Q and K, modulated by learned gating scalars $\lambda_Q$ and $\lambda_K$.

The paper proposes three main types of context vectors:

  • Global Context: A single vector summarizing the entire input sequence (e.g., using a mean operation). This captures the overall meaning of the sentence.
  • Deep Context: A concatenation of representations from lower layers of the network. This aims to capture syntactic and semantic information learned at different levels of abstraction.
  • Deep-Global Context: A combination of both global and deep contexts, allowing for a richer representation of context across different layers and the entire sequence.

These context vectors are integrated into the standard self-attention formula, allowing the model to learn how much importance to assign to the context for both queries and keys. Crucially, this is done by modifying the query and key transformations, not the attention function itself, preserving flexibility.

Datasets

The proposed methods were evaluated on the following machine translation datasets:

  • WMT14 English⇒German (En⇒De): Used for training and validation.
  • WMT17 Chinese⇒English (Zh⇒En): Used for training and validation.

The models were validated on newstest2013 and newsdev2017 and tested on newstest2014 and newstest2017 respectively.

Key Insights and Ideas

  • Context is Crucial for Self-Attention: The experiments strongly suggest that incorporating contextual information significantly improves self-attention network performance, validating the authors' core hypothesis.
  • Global and Deep Contexts are Complementary: Combining global context (overall sentence meaning) and deep context (hierarchical linguistic features) yields better results than using either in isolation, indicating they capture different, valuable aspects of context.
  • Function Words Benefit Most from Context: Linguistic analysis reveals that function words (like prepositions, determiners) benefit more from contextual information than content words (nouns, verbs), suggesting that context helps disambiguate their roles.
  • Keys Need More Context than Queries: The learned weights indicate that keys generally require more contextual information than queries, likely because keys are involved in the normalization step of the softmax, requiring them to consider relationships with all other items.
  • Encoder Benefits More Than Decoder: Applying context-aware mechanisms to both encoder and decoder did not yield as significant improvements as applying them solely to the encoder. This is attributed to the Transformer's existing encoder-decoder attention mechanism already leveraging encoder representations, which implicitly contain contextual information.

Key Results

  • Consistent Improvements: The proposed context-aware self-attention networks consistently improved BLEU scores on both En⇒De and Zh⇒En translation tasks compared to the baseline Transformer models.
  • Efficiency Gains: The context-aware Transformer-BASE model achieved comparable performance to the standard Transformer-BIG model while using nearly half the parameters and being twice as fast in training.
  • Specific Gains: For example, on the WMT14 En⇒De task, the "deep-global context + deep context" strategy (model #5) achieved a BLEU score of 28.26, outperforming the baseline's 27.31.
  • Gating Mechanism Effectiveness: The learned gating scalars ($\lambda_Q, \lambda_K$) were shown to be crucial, with experiments indicating a significant drop in performance if they were removed.

Limitations and Future Work

  • Encoder-Decoder Interaction: The paper notes that applying context-aware mechanisms to both encoder and decoder did not yield further improvements, suggesting that the standard encoder-decoder attention might already implicitly leverage contextual information from the encoder. This implies a need for more sophisticated ways to integrate context into the decoder.
  • Computational Overhead: While the proposed method aims to maintain simplicity and flexibility, incorporating context vectors does add a slight computational overhead, though it's managed effectively by the gating mechanism.
  • Future Directions: The authors suggest exploring the model's effectiveness on other NLP tasks like reading comprehension and stance classification. They also propose designing more linguistically informed context-aware techniques, such as incorporating phrase-level or syntactic category information, and combining their approach with other advanced techniques.

Summary updated

BibTeX
@inproceedings{yang2019context,
  author = {Baosong Yang and Jian Li and Derek F. Wong and Lidia S. Chao and Xing Wang and Zhaopeng Tu},
  title = {{Context-Aware Self-Attention Networks.}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2019},
  doi = {10.1609/aaai.v33i01.3301387},
}
Figure