In-Context Compositional Learning via Sparse Coding Transformer

Wei Chen, Jingxi Yu, Zichen Miao, Qiang QiuarXiv 2025 · DOI

Introduction

Transformers have revolutionized pattern recognition, yet they frequently struggle with in-context compositional learning. When presented with examples following a logical rule—such as "A + B → C"—standard models often fail to apply that same logic to novel inputs. This limitation stems from the "dense" nature of standard attention mechanisms, which tend to aggregate information across the entire context, leading to a "blurring" of distinct, sparse rules.

Researchers from Purdue University have proposed a novel solution: the Sparse Coding Transformer. By reformulating the attention mechanism through the lens of sparse coding, the model explicitly encodes and transfers compositional rules rather than just performing statistical pattern matching.

Overview of the Sparse Coding Transformer pipeline, illustrating how data is encoded into sparse coefficients using a dictionary to solve compositional reasoning tasks like RAVEN.

The Challenge of Compositional Generalization

The core issue is a lack of structural inductive bias. Humans can observe a few instances of a rule (e.g., "the third shape is the sum of the first two") and immediately generalize it. Standard Multi-Head Attention (MHA) relies on the Softmax function, which produces dense weights and results in "indiscriminate global mixing." When a Transformer predicts a target based on context, it often generates a blurry average of inputs rather than a precise, rule-governed output.

Methodology: Rethinking Attention as Sparse Coding

The Sparse Coding Transformer replaces the standard attention block with a mechanism that decomposes inputs into sparse, rule-based coefficients.

1. Dictionary Reformulation

The attention mechanism is factorized into two learned, input-dependent dictionaries: an encoding dictionary ($ϕ$) and a decoding dictionary ($ψ$). This allows the model to represent data as a combination of specific "atoms" from these dictionaries.

2. Sparsity-Promoting Nonlinearity

Instead of Softmax, the model employs a sparsity-promoting nonlinearity, such as soft-thresholding. This forces the representation into a sparse combination of dictionary atoms, effectively capturing the underlying compositional structure of the task.

3. The Lifting Scheme (Coefficient Transfer)

To solve for a target in a prompt, the model estimates the target's coefficients as a linear combination of the coefficients derived from the context examples. This "lifting" process allows the model to extract the rule from examples and apply it directly to the target prediction.

Comparison between (a) standard attention and (b) the proposed sparse coding attention, alongside (c) the sparsification and rule-sharing process.

Experimental Evaluation

The researchers evaluated the Sparse Coding Transformer across several reasoning benchmarks:

  • S-RAVEN (Symbolic RAVEN): A benchmark governed by hidden symbolic rules (shape, color, size).
  • RAVEN: A visual reasoning task requiring the generation of a 9th panel in a 3x3 grid.
  • Im-promptu: A suite including 3D Shapes and CLEVr objects.
  • Llama-7B Fine-tuning: Testing scalability on commonsense reasoning tasks like BoolQ and PIQA.

Key Results

  • Superior Accuracy: On S-RAVEN, the Sparse Coding Transformer achieved 82.7% accuracy, significantly outperforming the standard Transformer (65.1%) and the previous state-of-the-art HYLA (79.3%).
  • Visual Fidelity: In RAVEN experiments, while standard Transformers produced blurry images, the sparse coding approach generated sharp, structurally correct target panels, maintaining high quality (PSNR > 40) in 30% of cases where the baseline failed completely.
  • Parameter Efficiency: When applied to Llama-7B, the method added only 128 learnable parameters yet provided a +1.4% average performance boost, proving more efficient than traditional methods like LoRA.

Discussion and Future Work

The study demonstrates that sparsity acts as a powerful regularizer, forcing models to "pick a rule" rather than hedging with dense averages. By treating rules as coefficients, the model can perform mathematical operations on the logic itself.

While highly effective on synthetic and reasoning-heavy benchmarks, future work will focus on scaling this approach to massive, multi-billion parameter pre-training runs. Integrating sparse coding attention into general-purpose LLMs from the outset may significantly enhance their inherent reasoning and compositional capabilities.

Summary updated

BibTeX
@misc{chen2025context,
  author = {Wei Chen and Jingxi Yu and Zichen Miao and Qiang Qiu},
  title = {{In-Context Compositional Learning via Sparse Coding Transformer}},
  year = {2025},
  eprint = {2511.20194},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2511.20194},
}
Figure