Learning to Compose Representations of Different Encoder Layers towards Improving Compositional Generalization

Lei Lin, Shuangtao Li, Yafang Zheng, Biao Fu, shan liu, Yidong Chen, Xiaodong Shi

Abstract

Recent studies have shown that sequence-to-sequence (seq2seq) models struggle with compositional generalization (CG), i.e., the ability to systematically generalize to unseen compositions of seen components. There is mounting evidence that one of the reasons hindering CG is that the representation of the encoder's uppermost layer is entangled—specifically, the syntactic and semantic representations of sequences are not distinct. We hypothesize that the source keys and values representations passing into different decoder layers are also entangled. To address this, we propose COMPOSITION (Compose Syntactic and Semantic Representations), an extension to seq2seq models that learns to compose representations of different encoder layers dynamically. By introducing a composed layer between the encoder and decoder, we generate specific keys and values for different decoder layers, leveraging the fact that bottom encoder layers contain more syntactic information while top layers contain more semantic information.

Introduction

Human language is inherently compositional: if you understand the meaning of "the dog" and the function of "he liked" as a modifier, you can effortlessly comprehend the novel phrase "the dog he liked." However, standard Transformer models often fail when encountering such novel combinations of familiar components.

A primary culprit is representation entanglement. In standard architectures, the decoder only receives the output of the final encoder layer—a "black box" where syntactic structure and semantic meaning are mashed together. Furthermore, this same entangled representation is passed as keys and values to every decoder layer, ignoring the possibility that different stages of decoding might require different types of linguistic information.

The COMPOSITION Framework

To alleviate these issues, we introduce COMPOSITION, an architectural bridge that dynamically blends information from across the encoder hierarchy.

The architecture of COMPOSITION, featuring the "Composed Layer" that aggregates representations from all encoder sub-layers to provide layer-specific keys and values to the decoder.

Methodology

  1. Information Collection: Instead of relying solely on the final layer, COMPOSITION collects representations from every sub-layer (Self-Attention and Feed-Forward) across all $M$ encoder layers.
  2. The Composed Layer: We introduce learnable weights that allow the model to calculate a unique weighted sum of these collected representations for each of the $N$ decoder layers.
  3. Dynamic Keys and Values: This process creates layer-specific Keys ($K$) and Values ($V$) for the decoder’s cross-attention mechanism. This allows the model to dynamically "pick and choose" information; for instance, a decoder layer focused on structural reordering can prioritize bottom encoder layers (rich in syntax), while a layer focused on lexical choice can prioritize top layers (rich in semantics).

Experimental Setup

We evaluated COMPOSITION on two comprehensive benchmarks designed to test compositional generalization:

  • CoGnition: An English-to-Chinese (En→Zh) translation dataset featuring a dedicated "CG-test set" with 10,800 sentences containing novel compounds.
  • CFQ (Compositional Freebase Questions): A semantic parsing benchmark mapping natural language to SPARQL queries, using "Maximum Compound Divergence" (MCD) splits to ensure high structural variance between training and test sets.

Results and Analysis

Performance Gains

On the CoGnition benchmark, COMPOSITION achieved a 19.2% reduction in instance-level error and a 50.2% reduction in aggregate-level error compared to the base Transformer, outperforming specialized models like Dangle. On CFQ, the method boosted accuracy from 43.4% to 59.4% when combined with pre-trained RoBERTa models.

Decoder Diversity and Entanglement

Visualization of the learned weights confirms our hypothesis: different decoder layers prefer different encoder layers. They do not all converge on the "top" layer; instead, many prioritize mid-level or low-level information to perform their specific tasks.

Heatmaps showing the learned weights for the Key and Value representations across different decoder layers. The diversity in weights confirms that different decoder layers prioritize different levels of encoder information.

Furthermore, we observed that the performance gap between COMPOSITION and standard Transformers widens as sentence complexity increases, particularly when adding postpositive modifiers (e.g., "the dog he liked"). This suggests that providing unique "views" of the encoder to each decoder layer effectively mitigates the negative impacts of representation entanglement.

Conclusion

COMPOSITION demonstrates that compositional generalization can be significantly improved by breaking the reliance on a single, entangled encoder representation. By dynamically composing representations from all layers, seq2seq models can better navigate the complex interplay between syntax and semantics. Future work will explore the application of these dynamic composition weights in deeper Transformer architectures and decoder-only models.

Summary updated

BibTeX
@inproceedings{lin2023learning,
  author = {Lei Lin and Shuangtao Li and Yafang Zheng and Biao Fu and shan liu and Yidong Chen and Xiaodong Shi},
  title = {{Learning to Compose Representations of Different Encoder Layers towards Improving Compositional Generalization}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=vMpmabFTFw},
}
Figure