Inducing Systematicity in Transformers by Attending to Structurally Quantized Embeddings

Yichen Jiang, Xiang Zhou, Mohit Bansal

Abstract

Transformers generalize to novel compositions of structures and entities after being trained on a complex dataset, but easily overfit on datasets of insufficient complexity. We observe that when the training set is sufficiently complex, the model encodes structurally equivalent sentences using a systematic attention pattern. Inspired by this observation, we propose SQ-Transformer (Structurally Quantized) that explicitly encourages systematicity in the embeddings and attention layers even with low-complexity data. At the embedding level, we introduce Structure-oriented Vector Quantization (SoVQ) to cluster word embeddings into several classes of structurally equivalent entities. At the attention level, we devise the Systematic Attention Layer (SAL) and an alternative, Systematically Regularized Layer (SRL) that operate on the quantized word embeddings so that sentences of the same structure are encoded with invariant or similar attention patterns. Empirically, we show SQ-Transformer achieves stronger compositional generalization than the vanilla Transformer on multiple low-complexity semantic parsing and machine translation datasets. In our analysis, we show SoVQ indeed learns a syntactically clustered embedding space, and SAL/SRL induces generalizable attention patterns, altogether leading to improved systematicity.

Introduction

While Transformers are the backbone of modern AI, they suffer from a significant "data hunger" problem regarding compositional generalization. Humans can effortlessly understand the phrase "jump twice" if they already know how to "walk twice" and understand the meaning of "jump." However, a standard Transformer trained on a small or low-complexity dataset often fails to parse such novel combinations because it tends to overfit to specific word-structure pairs rather than learning underlying grammatical rules.

Previous research indicates that Transformers can learn these general rules, but typically only when exposed to massive, highly diverse datasets. The authors of this paper identify a "smoking gun" for successful generalization: models that generalize well exhibit nearly identical attention patterns for sentences with the same syntactic structure, regardless of the specific tokens used. The SQ-Transformer aims to induce this "systematic" behavior architecturally, removing the need for millions of additional training examples.

Methods: The SQ-Transformer

The SQ-Transformer introduces two primary components to bake structural awareness into the model architecture: Structure-oriented Vector Quantization (SoVQ) and Systematic Attention mechanisms.

Structure-oriented Vector Quantization (SoVQ)

Instead of relying solely on raw word embeddings, the model learns to "quantize" (cluster) words into a fixed number of structural classes. This is achieved via a Variational Brown Clustering objective, which groups words based on their context. For instance, "walk," "run," and "jump" are clustered together because they occupy similar syntactic positions (verbs), allowing the model to treat them as structurally equivalent entities.

Systematic Attention Mechanisms

The authors propose two distinct ways to utilize these quantized clusters within the attention mechanism:

  1. Systematic Attention Layer (SAL): This layer uses the quantized "role" embeddings as the Queries ($Q$) and Keys ($K$) for the attention mechanism. This enforces a "hard invariance"—the model becomes mathematically incapable of distinguishing between two words in the same cluster when determining attention weights.
  2. Systematically Regularized Layer (SRL): A "softer" alternative where the model performs standard attention but is penalized (via an $L_2$ loss) if its output deviates significantly from the quantized version. This allows the model to retain word-specific semantic nuance while remaining structurally grounded.
The architecture of the Systematic Attention Layer (SAL) and the Systematically Regularized Layer (SRL).

Experimental Evaluation

The SQ-Transformer was evaluated across several benchmarks designed to test out-of-distribution compositional reasoning:

  • SCAN (ADDJUMP, AROUNDRIGHT): Synthetic tasks requiring the model to map commands (e.g., "jump around left") to action sequences.
  • COGS: A semantic parsing dataset testing deep recursion and novel word-role combinations.
  • CoGnition: An English-to-Chinese translation dataset focused on novel compound expressions.
  • WMT17/14: Large-scale natural language translation datasets used to test scalability.

Key Results

The SQ-Transformer significantly outperformed vanilla Transformers across all metrics:

  • SCAN ADDJUMP: Accuracy improved from 40.0% to 99.4%.
  • SCAN AROUNDRIGHT: Accuracy rose from 69.5% to 99.6%.
  • CoGnition: The model reduced the Compound Translation Error Rate (CTER) from 29.6% to 18.1%, demonstrating superior handling of never-before-seen word combinations.
  • WMT Translation: Even on large-scale data, the SRL variant achieved higher BLEU scores, proving that structural regularization is beneficial even when data is relatively plentiful.

Analysis and Insights

Attention Invariance

The core finding of the paper is that systematicity is a byproduct of the model treating different entities with the same syntactic role identically. By forcing or encouraging the model to use invariant attention patterns, the SQ-Transformer avoids the "memorization" trap of standard Transformers.

Comparison of attention patterns. The SQ-Transformer induces more systematic, invariant patterns across structurally similar inputs compared to vanilla models.

Hard vs. Soft Constraints

The research highlights a trade-off between SAL and SRL. "Hard" invariance (SAL) is extremely effective for synthetic, rule-based tasks like SCAN where the grammar is rigid. However, for natural language translation, "Soft" invariance (SRL) is superior. Real-world language is not perfectly compositional; it requires understanding idioms, pragmatics, and subtle semantic differences that hard quantization might erase.

Limitations and Future Work

While a significant step forward, the SQ-Transformer has limitations. Currently, SoVQ only clusters individual words, meaning the model still struggles with "deep recursion" involving complex nested phrases. Future work may explore quantizing entire phrases or tree structures. Additionally, because quantization occurs at the embedding level, polysemous words (e.g., "bank") might be incorrectly forced into a single cluster. The authors suggest that quantizing the outputs of intermediate Transformer layers could mitigate this issue in future iterations.

Summary updated

BibTeX
@inproceedings{jiang2024inducing,
  author = {Yichen Jiang and Xiang Zhou and Mohit Bansal},
  title = {{Inducing Systematicity in Transformers by Attending to Structurally Quantized Embeddings}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=ZAFad3poTm},
}
Figure