In-Context Learning with Iterative Demonstration Selection

Chengwei Qin, Aston Zhang, Chen Chen, Anirudh Dagar, Wenming Ye

Abstract

In-Context Learning (ICL) is highly sensitive to the choice of demonstration examples. While existing methods focus on either semantic similarity or diversity, the optimal selection strategy often varies by task. This paper proposes Iterative Demonstration Selection (IDS), a framework that leverages a model's own zero-shot reasoning paths to dynamically select the most effective few-shot examples. By iteratively refining the selection and applying majority voting, IDS achieves a robust balance between similarity and diversity. Experimental results across six reasoning and classification datasets demonstrate that IDS consistently outperforms traditional selection baselines, providing an average accuracy boost of 1.7%.

Introduction

Large Language Models (LLMs) exhibit remarkable capabilities through In-Context Learning (ICL), where a few examples in the prompt guide the model to solve new tasks. However, ICL performance is notoriously "brittle," fluctuating significantly based on the specific demonstrations provided. Current selection strategies typically prioritize either similarity (e.g., K-Nearest Neighbors) or diversity.

The challenge lies in the task-specific nature of these dimensions: similarity often benefits topic classification, whereas diversity is more effective for complex commonsense reasoning. There is a lack of a unified, dynamic approach that adapts to these varying requirements. This paper introduces IDS to bridge this gap by using reasoning paths as the primary anchor for demonstration selection.

Methodology: Iterative Demonstration Selection (IDS)

The IDS framework moves beyond raw input text similarity by focusing on the logical structure of the problem. The process is divided into four main stages:

  1. Zero-shot Initialization: The model generates an initial reasoning path ($R$) for a test query using a "Zero-shot-CoT" trigger (e.g., "Let's think step by step").
  2. Reasoning-based KNN Selection: Instead of querying training examples similar to the raw input, IDS identifies examples whose content aligns with the generated reasoning path $R$ using Sentence-BERT embeddings.
  3. Iterative Refinement: The selected examples are used as context for the test query. The model then generates a new answer and a more refined reasoning path. This cycle can be repeated for several iterations.
  4. Majority Voting: To mitigate noise from individual iterations, the final output is determined by majority voting across all generated answers.
The IDS framework workflow, illustrating the iterative process from zero-shot initialization to final majority voting.

Experimental Setup

The authors evaluated IDS using GPT-3.5-turbo as the primary backbone, with additional testing on GPT-4 and Llama-2 models (7B to 70B). The evaluation spanned six datasets across five categories: Mathematical Reasoning (GSM8K, MATH), Commonsense Reasoning (CommonsenseQA), Logical Reasoning (LogiQA), Question Answering (BoolQ), and Topic Classification (AGNews).

Results and Discussion

IDS demonstrates consistent superiority over standard baselines, including Top-$k$-Consistency and Random-Voting.

Performance Gains

As shown in the results table, IDS achieves the highest accuracy across all tested datasets. The improvement is particularly notable in complex mathematical reasoning tasks, where it provides a relative boost of approximately 4% over similarity-based selection.

Comparison of IDS against various demonstration selection baselines across six datasets. IDS consistently achieves state-of-the-art performance.

Balancing Similarity and Diversity

Analysis reveals that IDS effectively finds a "middle ground." It selects examples that are less redundant than pure KNN but more relevant than random selection. By using the reasoning path, IDS identifies examples that share the same problem-solving structure as the query, regardless of surface-level textual differences. This task-agnostic nature allows IDS to adapt automatically to the specific needs of classification versus reasoning tasks.

Conclusion

Iterative Demonstration Selection (IDS) provides a robust, model-driven approach to optimizing ICL prompts. By anchoring selection in reasoning paths rather than raw text, it overcomes the limitations of static similarity or diversity heuristics. While the iterative nature of IDS increases inference costs, its consistent performance gains across diverse models and tasks mark a significant step toward more reliable and automated prompt engineering.

Summary updated

BibTeX
@inproceedings{qin2024context,
  author = {Chengwei Qin and Aston Zhang and Chen Chen and Anirudh Dagar and Wenming Ye},
  title = {{In-Context Learning with Iterative Demonstration Selection}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.438},
}
Figure