How Do In-Context Examples Affect Compositional Generalization?

Shengnan An, Zeqi Lin, Qiang Fu, Bei Chen, Nanning Zheng, Jian-Guang Lou, Dongmei Zhang

Abstract

While Large Language Models (LLMs) have demonstrated remarkable few-shot learning capabilities, their ability to perform compositional generalization—combining known concepts in novel ways—remains fragile. This paper investigates whether this fragility is an inherent limitation of the models or a consequence of suboptimal in-context example selection. By introducing the COFE benchmark and a systematic selection strategy based on structural similarity, diversity, and simplicity, the authors demonstrate that LLMs can significantly outperform fine-tuned models on complex compositional tasks.

Introduction

Compositional generalization (CG) is a fundamental aspect of human cognition, allowing us to understand and generate an infinite array of novel expressions from a finite set of known components. For instance, a human who understands the phrase "the cat sat on the mat" and the concept of a "wombat" can immediately comprehend "the wombat sat on the mat."

In the era of Large Language Models, the paradigm for achieving such generalization has shifted from extensive fine-tuning to In-Context Learning (ICL), where models are prompted with a few examples at inference time. However, early research suggested a significant performance gap: massive models like GPT-3 often performed worse on CG tasks than much smaller, task-specific fine-tuned models. This paper addresses a critical question: Is the failure of LLMs in compositional tasks due to a lack of reasoning capacity, or are we simply providing the wrong examples in our prompts?

The COFE Benchmark

To rigorously evaluate this, the researchers introduced COFE (COmpositional generalization with FEw-shot examples), a benchmark derived from the well-known COGS dataset. COGS focuses on semantic parsing—mapping natural language sentences to their underlying logical forms.

COFE adapts these tasks for the few-shot setting, categorizing challenges into five distinct levels of difficulty:

  • Low-level Tasks: Including Primitive Substitution and Structural Alternation.
  • High-level Tasks: Including Phrase Recombination, Longer Chains (recursion), and Deeper Nesting.

A Systematic Strategy for Example Selection

The core contribution of the paper is a framework for selecting in-context examples based on three primary factors:

  1. Structural Similarity: This involves matching the underlying "hidden" syntax (parse trees) of the examples to the test case. The authors hypothesize that providing examples with similar logical structures helps the model identify the correct transformation rules.
  2. Diversity: To prevent the model from becoming biased toward a single repetitive pattern, the selection process ensures a variety of structures are represented in the prompt. This is particularly vital for tasks requiring the recombination of phrases.
  3. Complexity: Counter-intuitively, the authors found that simpler, shallower examples are often more effective. High-complexity examples can introduce noise, making it harder for the model to extract the necessary logic.

These factors were formalized into a matching score used by a greedy search algorithm to select the optimal 10 examples from a candidate bank for any given test case.

Key Results and Insights

Structure Over Scale

The research reveals that the quality of example selection is more important than the sheer size of the model. When examples were chosen randomly, the massive davinci model (175B parameters) lagged behind a fine-tuned GPT-2 (0.7B parameters) by over 24%. However, when using structurally similar examples, the LLMs not only closed the gap but often surpassed the fine-tuned baselines.

Performance Gains

By moving from casual selection to structural similarity, code-davinci-002 saw its average accuracy jump from 60.9% to 75.6%. On low-level tasks like Primitive Substitution, the models achieved near-perfect accuracy (>99%). Most notably, on the "LongChain" task—which requires extending recursive logic—the best ICL setup achieved 87.0% accuracy, vastly outperforming the fine-tuned baseline's 5.4%.

The Role of Diversity and Simplicity

Diversity proved to be the "aha" moment for Phrase Recombination. Without a diverse set of examples, models tended to hallucinate or copy-paste patterns from the prompt rather than performing true logical recombination. Furthermore, the preference for simplicity suggests that LLMs benefit from a "cleaner" signal to understand the underlying rules of a task.

The Fictional Word Barrier

A significant finding was the models' reliance on pre-trained semantic knowledge. When the authors replaced common words with "fictional" random tokens, performance plummeted. This indicates that LLMs have not yet fully decoupled logical structure from lexical meaning; they are still "cheating" by using semantic priors rather than engaging in pure symbolic reasoning.

Conclusion and Future Directions

The study concludes that LLMs possess a latent capacity for compositional generalization that can be unlocked through strategic example selection. However, the current method relies on an "Oracle"—knowing the parse tree of the test case beforehand to find similar examples.

Future work will need to focus on developing structure-aware embedding models or pre-trained parsers that can automatically select high-quality examples in real-world scenarios where the ground truth is unknown. Bridging the gap between semantic reliance and symbolic reasoning remains the next great challenge for achieving robust AI generalization.

Summary updated

BibTeX
@inproceedings{an2023how,
  author = {Shengnan An and Zeqi Lin and Qiang Fu and Bei Chen and Nanning Zheng and Jian-Guang Lou and Dongmei Zhang},
  title = {{How Do In-Context Examples Affect Compositional Generalization?}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-long.618},
}
Figure